Imagecreatefrompng() function in php

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Function Php
Click For Summary
The discussion revolves around using the PHP function imagecreatefrompng() to load a PNG image named full.png from an img folder. The provided PHP script attempts to create an image or display an error message if the image fails to load. The script sets the content type to image/png and outputs the image. However, the HTML code for displaying the image results in a broken image tag, indicating an issue with the image loading process. The user expresses confusion regarding the function's return value, described as an "image identifier," and seeks advice on resolving the problem. There is also mention of considering a JPEG version of the function as an alternative, highlighting the complexity of working with image functions in PHP.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
I was experimenting with the imagecreatefrompng() function in php. There was an example here:
http://us2.php.net/imagecreatefrompng

I tried to modify it to use an image called full.png in my img folder.

aaaimage.php
Code:
#!/usr/local/bin/php
<?php

function LoadPNG($imgname) 
{
    $im = @imagecreatefrompng($imgname); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im  = imagecreatetruecolor(150, 30); /* Create a blank image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}
header("Content-Type: image/png");
$img = LoadPNG("img/full.png");
imagepng($img);

?>

I'm not sure what to do with the HTML. I tried the simplest idea:
display.htm
Code:
<HTML>
<HEAD></HEAD>
<BODY>
This is a test.<BR/>
<img src="aaaimage.php" />
</BODY>
</HTML>

But that does nothing. I just get a broken image tag. I am not really clear on what the function returns, they say it is an

"image identifier" but I don't know what that means.

Any advice? Thanks.:smile:
 
Computer science news on Phys.org
Here is a lot of commentary on this function.
Apparently there is only one error return.
Scroll down a bit. The page looks blank, but isn't.

http://us.php.net/imagecreatefrompng
 
thanks. I've been through the commentary but still haven't solved my problem. I think I might try the jpeg version of this function and see if I have any more luck. I was hoping if I was doing something stupid in my code it would be glaringly apparent and I could correct it, but it sounds like this is just going to be a complicated function to work with.
 
Last edited:
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K