This question already has answers here:
Fatal error: Call to undefined function: imagecreate()
(11 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 5 days ago.
Can somebody help me to show the problem of my code? This is the output when I write this code:
In this case I have installed GD on php
enter image description here
<?php
//create the canvas
$myImage = ImageCreate(150,150);
//setup some colors
$black = ImageColorAllocate($myImage, 0, 0, 0);
$white = ImageColorAllocate($myImage, 255, 255, 255);
$red = ImageColorAllocate($myImage, 255, 0, 0);
$green = ImageColorAllocate($myImage, 0, 255, 0);
$blue = ImageColorAllocate($myImage, 0, 0, 255);
//draw some rectangles
ImageRectangle($myImage, 15, 15, 55, 85, $red);
ImageRectangle($myImage, 55, 85, 125, 135, $white);
// output the image to the browser
header("Content-type: image/gif");
ImageGif($myImage);
//clean up after yourself
ImageDestroy($myImage);
?>
Expected Output:
enter image description here
Can you show me how to solve it? Thank you!
Related
Can someone help me to draw single pixel using php GD library?
I have such code:
$img = imagecreatetruecolor(100, 100);
$c = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagesetpixel($img, 0, 0, $c);
imagejpeg($img, 'created.jpg');die();
But the output is really weird (it is approximate image in top-left corner):
Is it possible to draw just one red pixel at x-0 y-0 position?
I'm making my own 'captcha' form and I now have a page that generates the image:
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 160, 160, 160);
imagefilledrectangle($im, 0, 0, 200, 50, $white);
$captcha = "SOMErandomTEXT";
$font = 'Chewy.ttf';
imagettftext($im, 20, 0, 0, 20, $gray, $font, $captcha);
imagepng($im);
imagedestroy($im);
?>
Now I also have another page that shows this image, inside the form. Now I want to get the value $captcha from the page shown above on the other page. How can I do it?
Using sessions did the job for me.
PHP Sessions
I am trying to make a gamecard for a RSPS. This is my first time using PHP to generate text on images and when trying to apply this weird glitch happens:
But, when the original photo looks like this:
As you can see the corners are not curved like they should be. But, in the original it comes out fine. I am not talking about the location of the numbers.
Here is the code I am using:
<?php
header('Content-Type: image/png');
// Image Creation
$image_file = "SoulSplitCard.png";
$im = imagecreatefrompng($image_file);
//Colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$rscol = imagecolorallocate($im, 23, 113, 183);
// Levels (for now)
$text = '99';
$text2 = '87';
// Font
$font = 'arial.ttf';
//Text Applications
imagettftext($im, 15, 0, 150, 35, $rscol, $font, $text);
imagettftext($im, 15, 0, 150, 81, $rscol, $font, $text2);
// Using imagepng() instead of imagejpeg()
imagepng($im);
imagedestroy($im);
?>
What is causing this to happen?
The imagesetthickness() on the PHP will change the thickness of all lines on the image. Is there any way to select what line to be associated with this function? For Example in following image I would like to ONLY change the thickness of GREEN lines to 5.
<?php
$image = ImageCreate(130, 170);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$green = ImageColorAllocate($image, 82, 128, 8);
ImageFill($image, 0, 0, $white);
ImageSetThickness ($image , 5);
ImageLine($image,60,40,60,100,$black);
ImageLine($image,25,25,100,25,$green);
ImagePng($image, "flag.png");
ImageDestroy($image);
?>
You just need to set the thickness to the desired value before calling imageline.
<?php
$image = ImageCreate(130, 170);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$green = ImageColorAllocate($image, 82, 128, 8);
ImageFill($image, 0, 0, $white);
// set to 3 for the next call
ImageSetThickness ($image , 3);
ImageLine($image,60,40,60,100,$black);
// set to 5 for the next call
ImageSetThickness ($image , 5);
ImageLine($image,25,25,100,25,$green);
// Set back to 3 for any future calls calls
ImageSetThickness ($image , 3);
ImagePng($image, "flag.png");
ImageDestroy($image);
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).