Imagecreatefrompng() function in php

In summary, The speaker was experimenting with the imagecreatefrompng() function in php and tried modifying it to use an image called full.png in their img folder. They also discussed some issues they encountered while trying to use the function and the difficulties of working with it. The speaker received some advice but still had not solved their problem. They were considering trying the jpeg version of the function instead.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
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
 
  • #3
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:

1. What does the Imagecreatefrompng() function do?

The Imagecreatefrompng() function is a built-in function in PHP that creates a new image from a PNG file. It reads the PNG file and returns an identifier representing the image in PHP's memory.

2. What are the parameters of the Imagecreatefrompng() function?

The Imagecreatefrompng() function takes in one required parameter, which is the path to the PNG file. This can be either a local file path or a URL. Additionally, there are two optional parameters for specifying the image's width and height.

3. What is the return value of the Imagecreatefrompng() function?

The Imagecreatefrompng() function returns an identifier representing the image in PHP's memory. This identifier is used in other PHP image functions to manipulate the image.

4. Can the Imagecreatefrompng() function handle transparent images?

Yes, the Imagecreatefrompng() function can handle transparent images. It preserves alpha channel information in the PNG file, allowing for transparent pixels in the image.

5. Are there any limitations to using the Imagecreatefrompng() function?

Yes, there are a few limitations to using the Imagecreatefrompng() function. It only supports PNG images, so it cannot be used for other image formats. Additionally, it may not work with extremely large or complex PNG files, as it relies on PHP's memory limit to load the image.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
8K
Back
Top