|
|
 |
imagecreatefrompng (PHP 3 >= 3.0.13, PHP 4, PHP 5) imagecreatefrompng -- Create a new image from file or URL Descriptionresource imagecreatefrompng ( string filename )
imagecreatefrompng() returns an image identifier
representing the image obtained from the given filename.
imagecreatefrompng() returns an empty string
on failure. It also outputs an error message, which unfortunately
displays as a broken link in a browser. To ease debugging the
following example will produce an error PNG:
Example 1.
Example to handle an error during creation (courtesy
vic at zymsys dot com)
|
<?php
function LoadPNG($imgname)
{
$im = @imagecreatefrompng($imgname); if (!$im) { $im = imagecreate(150, 30); $bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
?>
|
|
Tip: You can use a URL as a
filename with this function if the fopen wrappers have been enabled.
See fopen() for more details on how to specify
the filename and Appendix L for a list of supported
URL protocols.
| Warning | Windows versions of PHP
prior to PHP 4.3.0 do not support accessing remote files via this function, even if
allow_url_fopen is enabled.
|
User Contributed Notes
imagecreatefrompng
rf at i---i dot org
02-Nov-2004 05:47
***CORRECTION*** to 07-Jun-2004 01:14
imageAlphaBlending must be false, NOT TRUE
$imgPng = imageCreateFromPng($strImagePath);
imageAlphaBlending($imgPng, false); <<<FALSE
imageSaveAlpha($imgPng, true);
07-Jun-2004 06:14
If you're trying to load a translucent png-24 image but are finding an absence of transparency (like it's black), you need to enable alpha channel AND save the setting. I'm new to GD and it took me almost two hours to figure this out.
<?php
$imgPng = imageCreateFromPng($strImagePath);
imageAlphaBlending($imgPng, true);
imageSaveAlpha($imgPng, true);
header("Content-type: image/png");
imagePng($imgPng);
?>
csaba at alum dot mit dot edu
01-Jun-2004 12:57
This is a another case of someone (IE) thinking they know better than you what you want. If you are of a mind to generate a complicated (read takes a long time) image incrementally, you might do it by having an IFRAME which calls to the image generating PHP script. That munches for a bit, saves the file, returns to the client (remembering we're in the iframe so the user doesn't see all this) (with appropriate state information in <input type=hidden ...> fields) which kicks off an <body onLoad='docInit()' ...> whose purpose is to set the src attribute of the png image to the saved file (so the user sees the changes), and then resubmit the iframe (the image isn't in the iframe so the iframe can be hidden) to repeat the process. Very nifty.
However, IE 6 has decided that it is far better to use a cached version of an image even on a local machine, where it can readily see that it changed, rather than to go to the source. And, since it's a file, you can't so easily set headers on it. This means that your incremental updates may not show any updates.
I solved this by (1) having Apache rewrite every .png request to a single .php file:
RewriteRule ^(.*).png$ pngPassThru.php?file=$1.png [QSA,L,NC]
(2) All pngPassThru.php does is to return the .png file with "don't cache me" headers. Avoid leading/trailing spaces in pngPassThru.php. The headers might be simplified - I've collected them from various sources and am not sure which ones could be eliminated. But this is working for me on IE 6:
<?php
header("Content-type: image/png");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$file=@$_GET['file'];
$size= filesize($file);
header("Content-Length: $size bytes");
readfile($file);
?>
Csaba Gabor
aniakovas at yahoo dot co dot uk
27-Apr-2004 08:13
Hey guys,
It has been my experience that the image type of a png file is best set to:-
image/x-png
This may be a peculiarity of the server I'm working on, which is running Apache/FreeBSD
Ania
polak_sg at hotmail dot com
05-Nov-2002 09:02
don't forget to write the header into your function file!!
header ("Content-type: image/png");
wyvern at greywyvern.com
05-Oct-2002 10:45
Make sure when coding image serving scripts that you avoid using the full "http://" URL of the image whenever possible. Use "../imagename" etc.
Loading URL's from your own site will double your used bandwidth, as the script grabbing the image will count against it as well as sending it to the browser.
robross at comcast dot net
26-Feb-2002 06:47
This color table issue is actually a bit more complicated.
From my testing, it would appear that whatever colors you have specified in the color table for your picture in the first nth number of spots, those colors will automatically be given to the first "n" colors you attempt to use in your CreateImagefromPNG.
So, for example, if you need to create dynamic black and red text in your image, the first three spots in the color table for your background image should be white, black and red (in this order).
This can be a pain, but it is the only way to ensure that your images look as intended in all browsers.
netfiend at mediaone dot net
23-May-2001 06:04
I've noticed a couple of things with the ImageCreateFromPng function that may be useful if your having problems.
1st thing I noticed is that the PNG file you are creating from needs to be indexed and less than 256 colors.
Secondly, you need to reduce the color depth by 1 for each color you plan on generating in php... I.E. if you plan on drawing 50 different colors on the screen with php you need to allocate space for them by reducing the origional image's color depth to 205 colors (I was pulling my hair out on this one for a while because the wrong colors were showing up in the output).
Also, if you are saving the image as a PNG you need to allocate the 1st color as white.. The reason for this is that Internet Explorer seems to interpret the first color in an indexed PNG as white no matter what color it is... (Figured this out when all my pretty black text was turning up as white in I.E. and was normal in Netscape; allocated the first color in the image as white and the problem went away.)
-- hope this helps.
| |