PHP invalid image's and error handling - php

Using PHP's Image and GD functions you can use the following method to finally output the php image
imagepng($image);
Sometimes, for whatever reason the image may not be displayed typically the error is not with the image but with the actual php functions not executing successfully. However this causes a blank image to be returned which doesn't help me.
What I want to know is, is there a way to detect a blank or an invalid image and create a new image, write the errors to the new image using imagestring() and then display this new (debug) image instead.
for example, a successfully displayed image with no errors:
$image = imagecreate(256, 256); //create image
imagecolortransparent($image, $BLUE); //set transparent
imagefilledrectangle($image, 0, 0, 256, 256, $BLUE); //fill with 'transparent colour'
//Draw a border round the image
imageline($image, 0, 0, 0, 255, $Black);
imageline($image, 0, 0, 255, 0, $Black);
imageline($image, 255, 0, 255, 255, $Black);
imageline($image, 0, 255, 255, 255, $Black);
imagestring($image, 1, 10, 10, "I am an image!", $Black);
imagepng($image);
imagedestroy($image);
but if I then introduce some errors in the php script that may or may not be to do with the actual image creation then the php script fails and the image will not be visible...
$image = imagecreate(256, 256); //create image
imagecolortransparent($image, $BLUE); //set transparent
imagefilledrectangle($image, 0, 0, 256, 256, $BLUE); //fill with 'transparent colour'
//Draw a border round the image
imageline($image, 0, 0, 0, 255, $Black);
imageline($image, 0, 0, 255, 0, $Black);
imageline($image, 255, 0, 255, 255, $Black);
imageline($image, 0, 255, 255, 255, $Black);
imagestring($image, 1, 10, 10, "I am an image!", $Black);
/* I am here to cause problems with the php script
** and cause the execution to fail, I am a function
** that does't exist...
**
** and I am missing a semi colon! ;)*/
non_existant_function()
imagepng($image);
imagedestroy($image);
At this point I want to create a new image like above but in replacement of the I am an image! text I would put the actual error that has occured.

What you want to do is catch PHP errors, not detect a "blank image". You can use set_error_handler() to define a custom callback that's called when an error occurs.
Things such as parse errors are something you should debug before publishing your code, but this should help you detect random errors (database connections dying, whatnot).

Related

PHP/GD : Getting a small white square on white background when creating any image

I am starting on php and am trying to create an image using php-gd, but keep getting a weird output.
I am using xampp.
Here's my code: (a black line should be seen into the screen)
<?php
header ("Content-type: image/png");
$image = imagecreate(200, 50);
$black = imagecolorallocate($image, 0, 0, 0);
ImageLine ($image, 30, 30, 120, 120, $black);
imagepng($image);
?>
I tried with many samples found in the internet but i keep getting
, for anything I'm trying to do. (from showing text to creating a watermark). I use examples already tested by others, so the code isn't probably the problem.
Could you please say me what is wrong?
Edit : I am actually on the right window
(my file is stored at C:\xampp\htdocs\projet_30_03_21\template.php)
Edit : Ctrl+F5 doesn't work either. Also tried creating a new window.
You could use imagefill() to define a background before to add a black line, because the background is already black.
It's also recommanded to use imagecreatetruecolor() instead of imagecreate().
$image = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
imageline($image, 30, 30, 120, 120, $black);
header("Content-type: image/png");
imagepng($image);
Give the following image
Found the problem (https://www.php.net/manual/fr/install.windows.legacy.index.php#install.windows.legacy.building).
I had to uncomment this line of code :
;extension=gd
to
extension=gd.

Captcha image is generated but numbers are not generated

This is generate.php code:
<?php
ob_start();
session_start();
header('content-type:image/jpeg');
$text=$_SESSION['secure'];
$font_size=30;
$width=100;
$height=40;
$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);
$font='Consolas.ttf';
for($x=1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $text);
$imgSrc="out.png";
imagepng($image, $imgSrc);
?>
This is HTML code to display an image, here out.png is successfully generated and loaded into the page but numbers cannot be found.
<div class="form-group mx-auto text-center">
<input type="hidden" name="cap_hidden" value="<?php echo $_SESSION['secure']; ?>">
<img src="out.png">
</div>
I have found one similar question but it didnt help me, thats why I have posted it here.
The problem is the image is generated with lines only but no numbers are visible.
Looks like the font didn't load correctly.
Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path. PHP: imagettftext - Manual
I suppose there is Text on the Image but it is white.
Perhaps you could change the Colors of the Text.
http://php.net/manual/de/function.imagecolorallocate.php
These parameters are integers between 0 and 255 or hexadecimals
between 0x00 and 0xFF.
0 would be Black and 255 would be White.
You might want to look at the Example at
http://php.net/manual/en/function.imagettftext.php
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
You also might have a look at your Script at:
$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);
The Result of imagecolorallocate($image, 0, 0, 0); isnt assigned anywhere.

How can I draw a flipped image and original image side by side in PHP?

Here is the code that I am using currently.
$image = imagecreatetruecolor(400, 300);
imagesavealpha($image, true);
imagesetthickness($image, 2);
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 0);
imagefill($image, 0, 0, $transparent);
imagerectangle($image, 2, 2, 398, 298, $red);
imagedashedline($image, 0, 150, 400, 150, $blue);
imagedashedline($image, 200, 0, 200, 300, $blue);
imagepolygon($image, array( 10, 180, 10, 250, 110, 250), 3, $red);
// imageflip($image, IMG_FLIP_VERTICAL);
ob_start();
imagepng($image);
printf('<img src="data:image/png;base64,%s"/>', base64_encode(ob_get_clean()));
imagedestroy($image);
The attached image is result of running the above code.
I want to flip the triangle in the bottom right corner and draw it along with the original image. I tried to use imageflip() but it draws over the original image.
I know that imageflip() is flipping the rectangle as well as dotted lines but they are symmetric so it is no big deal.
Setting the fill color to transparent did not work. Any help would be appreciated.
My aim is to take original image, flip it along the horizontal line and then flip the resulting image along vertical line.
Once I know how to flip along the horizontal line without loosing the original image, I can try to do the rest on my own.
if you want you can use javascript if you know it to get the style of it then add a new one but that can be complicated. I can walk you through it, although that will take quite a bit of time. I can help if you have a github
If you want to preserve original image before flip, then use imagecopy() to duplicate image then call imageflip() using new duplicate image.
$flipImage = imagecreatetruecolor(400, 300);
imagecopy($flipImage, $image, 0, 0, 0, 0, 400, 300);
imageflip($flipImage, IMG_FLIP_HORIZONTAL);

Copy one image on top of the next, while retaining transparency

I'm struggling with PHP's GDLib just a little bit here. I'm trying to overlay two .PNG images on top of eachother, which so far works fine.
The one problem I run into is that sometimes, the overlay image comes with a white background. I can make this transparent (using the imagecolortransparent function), but this transparency isn't saved when I copy this image onto a new one.
// Load the background image first
$background = imagecreatefrompng($this->background);
// Load the overlaying image next, and set white as a transparent color
$overlay = imagecreatefrompng($this->image);
imagecolortransparent($overlay, imagecolorallocate($overlay, 255, 255, 255));
// So far, this all works. But when I create a new image,
// and paste both $background and $overlay into it,
// $overlay loses transparency and reverts to a white fill.
$image = imagecreatetruecolor(16, 16);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $trans_colour);
imagecopyresampled($image, $background, 0, 0, 0, 0, 16, 16, 16, 16);
imagecopyresampled($image, $overlay, 0, 0, 0, 0, 16, 16, 16, 16);
#mkdir(dirname($file), 0777, true);
imagepng($image, $file);
// The new $image is now mostly white. The transparency on $overlay
// was lost, meaning that the $background image is completely invisible.
How can I keep the transparency when copying $overlay into a new image?
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";
$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);
imagecopy($im, $im2, (imagesx($im) / 2) - (imagesx($im2) / 2), (imagesy($im) / 2) - (imagesy($im2) / 2), 0, 0, imagesx($im2), imagesy($im2));
// Save alpha for the destination image.
imagesavealpha($im, true);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);
// Save final image after placing one on another image
imagepng($im, "output.png", 0);
imagedestroy($im);
imagedestroy($im2);

I can't use transparent background with imagecopymerge

I am calling imagecopymerge($dst_r, $logo, 0, 0, 0, 0, $LogoX, $LogoY, 100); where $logo is a png file with transparent background. From some reason the background comes out white instead.
What am I doing wrong?
Thanks.
You need to use imagealphablending($dst_r, TRUE); to allow copying with retaining the transparent colors. Many more comments (...) in the manual suggest using imagecopy instead, because imagecopymerge was never intended to be used with transparency. If you use pct=100 anyway, then the normal imagecopy might be an option.
This is for text, but you can get the point. It would be more helpful if you post entire code.
$font = 25;
$string = "Hello";
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "font.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

Categories