Trying to transparent an image php - php

I'm again. I need rlly help, i need put transparent an img php
But i can't get it. I'm rlly newbie on PHP and I want learn. I was reading on PHP.net and I get only remove some problems.
Here's the code:
<?php
header('Content-type: image/jpeg');
$im = file_get_contents("http://fthw.cf/count/index.php?dt=2016-02-13/10:00:00");
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagepng($im, '$img');
imagedestroy($im);
?>
ERROR LOG:
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolorallocate() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 4
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolortransparent() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 5
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagepng() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 6
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagedestroy() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 7
Greetings!
PS: help me pls

<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'file.png';
copyTransparent($png,"png.png");
?>
Here is a function you can use to make a image transparent

Related

PHP saves the png file as a 0 byte to disk

My intention is save the generated qr code image on my local.
I have already checked out whole stackoverflow question about it. However they didn't help me to solve this bug.
<?php
header('Content-type: image/png');
$filename = "./qrs/qr-6234/qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$sizex$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefrompng ($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
?>
I don't know why but, it gives me zero byte png file.
Edit: Thanks to ishagg, I got my error logs. These are ;
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ./qr/qr_txt_test.php on line 10
Warning: file_get_contents(http://chart.googleapis.com/chart?chs=250&cht=qr&chld=L|0&chl=https%3A%2F%2Fstackoverflow.com&choe=UTF-8): failed to open stream: no suitable wrapper could be found in ./qr/qr_txt_test.php on line 10
Warning: imagecreatefromstring(): Empty string or invalid image in ./qr/qr_txt_test.php on line 11
Warning: imagecopy() expects parameter 2 to be resource, boolean given in ./qr/qr_txt_test.php on line 13
Warning: imagepng(): gd-png error: no colors in palette in ./qr/qr_txt_test.php on line 14
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ./qr/qr_txt_test.php on line 15
If you remove the header() call, you'll be able to see the errors in the script.
In yours, there are two:
Variable $sizex, used in $qr_url is undefined.
To create a new image from an external resource, you need to use imagecreatefromstring().
With these two mistakes fixed, the code works correctly:
<?php
header('Content-type: image/png');
$filename = "qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefromstring($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
Result:

imagerotate fails for small image

I have a 1x2px image, when I use imagerotate I get this error
Warning: imagerotate(): gd warning:
one parameter to a memory allocation multiplication is negative or zero,
failing operation gracefully in file#line
Sample code
header('Content-type: image/png');
$angle = 320;
$image = imagecreatefrompng('small.png');
$bg_color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$temp_image = imagerotate($image, -$angle, $bg_color);
imagesavealpha($image, 1);
imagepng($image);
Is there a workaround for this issue?
PHP 5.5.12
WAMP

imagecreatefromjpeg parameter not working

I have used imagecreatefromjpeg("1.jpg") it is showing
Warning: imagecopy() expects parameter 1 to be resource, string given in E:\wamp\www
I am not understanding why it is not working and it tacks as a string, though it is a existing image name on the same file.
$new_image = "assets/small/i/m/".imagecreatetruecolor($size_width, $size_height);
$file = "upl_13541718681354171868The-best-top-desktop-dolphin-wallpapers-hd-dolphins-wallpaper-3.jpg";
$image = imagecreatefromjpeg($file);
imagecopy($new_image, $image, 0, 0, 0, 0, $size_width, $size_height);
imagejpeg($imageOutput, $outputPath, 100);
imagecreatefromjpeg
Returns an image resource identifier on success, FALSE on errors.
so you are probably passing false to
imagecopy
edit
sorry, parameter 1 is the destination. your're passing a string instead of a resource:
From docs:
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

Why my image created by php is not displaying?

I have a problem , I have a software that gets pc tech specs and post them online and we are offering a signature image for each specs, this is created by php , but is not working anymore;
Demo link : http://checkmyspecs.co.uk/button.php?id=646725 or go to any specs page , example : http://www.checkmyspecs.co.uk/display2.php?id=646725 and down on the page is grab button code.
The image must be created by button.php , here is the code :
<?php
include "db.php";
function getdata($viewerid) {
$query = "SELECT * FROM data where viewerid = '$viewerid'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$data = array();
array_push($data,$row['bootmethod'],$row['ComputerCaption'],$row['Infrared'],$row['DayLight'],$row['ManufacturerBox'],$row['Model'],$row['cores'],$row['memory'],$row['monitor'],$row['resolution'],$row['pixels'],$row['cpuvoltage'],$row['clockspeed'],$row['AddressWidth'],$row['SocketDesignation'],$row['cpuname'],$row['loadpercent'],$row['applications'],$row['videocardname'],$row['refreshrate'],$row['videodriver'],$row['installed'],$row['hddata'],$row['directx']);
return $data;
}
function LoadPNG($imgname,$cur)
{
$getvalues = getdata($cur);
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
// Removes white background (made from the original transparency)
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background transparent
//Generate the text to write onto the image (the php Version).
//Don't know much about this
// Add some shadow to the text
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$blue = imagecolorallocate($im, 0, 173, 238);
$darkblue = imagecolorallocate($im, 54, 154, 191);
$font = '/fonts/arialbd.ttf';
$ramo = $getvalues[7];
$cpuo = $getvalues[15];
$gpuo = $getvalues[18];
$screeno = $getvalues[9];
$ram = "$ramo MB";
$cpu = "$cpuo";
$gpu = "$gpuo ";
$screen = "$screeno";
imagettftext($im, 11, 0, 40, 23, $black, $font, $cpu);
imagettftext($im, 11, 0, 40, 53, $black, $font, $gpu);
imagettftext($im, 11, 0, 40, 83, $black, $font, $screen);
imagettftext($im, 11, 0, 40, 113, $black, $font, $ram);
// write text
$textcolor = imagecolorallocate($im, 0, 3, 0);
return $im;
}
header('Content-Type: image/png');
$button = '/images/button.png';
$img = LoadPNG($button,$_GET['id']);
imagepng($img);
?>
Your image isn't an image. Here's what is really being output:
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 24
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 33
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 34
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 35
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 36
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/checkmys/public_html/button.php on line 37
Fatal error: Call to undefined function imagettftext() in /home/checkmys/public_html/button.php on line 53
You can use tools such as Fiddler to see this, even when your content-type is set as an image. If you would remove that # symbol, we could see the error describing why your image could not be created.
Also, you are currently wide open to SQL injection. You should use prepared queries with PDO to avoid this problem.

getting jpeg error

<?php
function LoadPNG()
{
/* Attempt to open */
//require_once 'resizex.php';
$imgname="/home2/puneetbh/public_html/prideofhome/wp-content/uploads/268995481image_11.png";
//$im = #imagecreatefrompng($imgname);
$img= imagecreatefromstring(file_get_contents($imgname));
//$im=imagecreatefrompng('images/frame.png');
$im= imagecreatefromjpeg('images/frame.jpeg');
//imagealphablending($img, false);
//imagesavealpha($img, true);
//$img=resizex("$url",60,65,1);
imagecopymerge($im,$img,105,93,0, 0,275,258,100);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
$img = LoadPNG();
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
imagedestroy($img);
?>
i am getting error
arning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home2/puneetbh/public_html/prideapp/frame.php on line 11
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'images/frame.jpeg' is not a valid JPEG file in /home2/puneetbh/public_html/prideapp/frame.php on line 11
Warning: imagecopymerge(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at /home2/puneetbh/public_html/prideapp/frame.php:11) in /home2/puneetbh/public_html/prideapp/frame.php on line 34
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 35
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home2/puneetbh/public_html/prideapp/frame.php on line 36
The root of the problem is
'images/frame.jpeg' is not a valid JPEG file
maybe the file is broken, maybe it is a CMYK image.
You should check whether imagecreatefromjpeg() returns false, and stop execution of the script in that case and maybe output an error message.

Categories