Imagecreatefrompng() function in php

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Function Php
Click For Summary
SUMMARY

The discussion centers on the use of the imagecreatefrompng() function in PHP for loading PNG images. The user implemented a function called LoadPNG to handle image loading and error management, creating a blank image with an error message if the loading fails. The user encountered issues displaying the image in HTML, resulting in a broken image tag. The function returns an "image identifier," which is crucial for further image manipulation and output.

PREREQUISITES
  • Understanding of PHP scripting language
  • Familiarity with image processing functions in PHP
  • Basic knowledge of HTML for embedding images
  • Experience with error handling in PHP
NEXT STEPS
  • Research the imagecreatefromjpeg() function for JPEG image handling
  • Learn about PHP error handling techniques to improve debugging
  • Explore the use of imagepng() for outputting images in PHP
  • Investigate the concept of "image identifiers" in PHP image processing
USEFUL FOR

Web developers, PHP programmers, and anyone working with image processing in PHP will benefit from this discussion, particularly those facing challenges with the imagecreatefrompng() function.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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:

Similar threads

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