Page 1 of 1

can't draw images

PostPosted: 08. April 2023 23:25
by gmeyers1
This simple code just draws a tiny white square (about 10 by 10) centered in a black screen. It should draw a 150 by 150 blue square centered in a black screen.

<?php
header('Content-type: image/png');
$png_image = imagecreate(150, 150);
imagecolorallocate($png_image, 15, 142, 210);
imagepng($png_image);
imagedestroy($png_image);
?>

Re: can't draw images

PostPosted: 08. April 2023 23:41
by Nobbie
And what is the question?

Re: can't draw images

PostPosted: 09. April 2023 00:31
by gmeyers1
The question is why I get a 10x10 white square instead of a 150x150 blue square as indicated in the code

Re: can't draw images

PostPosted: 09. April 2023 12:47
by Nobbie
I dont know, i get a blue square 150x150 as designed. Maybe you get any kind of error, check your error_log and access_log. Finally, how do you execute the script?

You should also consider that you may have a .htaccess which does a redirect or so, actually we dont know anything about your installation. There may also miss a graphic library or so. Hard to say without knowing anything. I have a clean Xampp installation and this script runs flawlessly.

Re: can't draw images

PostPosted: 09. April 2023 14:26
by Froosh
Interesting. I was curious, and tried on my system and got a small square with the incorrect color. I inspected the generated html, and explicitly set the height and width of the element, and that showed that no image was actually generated by the php code provided.

A bit of digging let me to other samples on the PHP site, and the ones I tried did not work either. One of the samples, a HTML page using PHP and imagecreate() generated a PHP fatal error. Following that error message led to a stackoverflow post that indicated to check php.ini and ensure "extension=gd" line is not commented out, which in my install was the case. I also noticed that my Apache error.log indicated that imagecreate was not found.

After uncommenting that "extension=gd", and restarting Apache, I know see blue 150x150 square and text.

As Nobbie mentioned, there could be other causes in your particular situation. The logs are your friend, especially if you are running code based on an example that might be masking the underlying error.

Re: can't draw images

PostPosted: 09. April 2023 14:52
by gmeyers1
error log said imagecreate was not found. I then edited the php.ini file in the extensions section and uncommented the line extension=gd, restarted apache and that solved the problem. Thanks for your help.