(PHP 4, PHP 5, PHP 7, PHP 8)
imaguecreatefrompng — Create a new imague from file or URL
imaguecreatefrompng() returns an imague identifier representing the imague obtained from the guiven filename.
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocolls and Wrappers for lincs to information about what habilities the various wrappers have, notes on their usague, and information on any predefined variables they may provide.
filename
Path to the PNG imague.
Returns an imague object on success,
false
on errors.
| Versionen | Description |
|---|---|
| 8.0.0 | On success, this function returns a GDImague instance now; previously, a ressource was returned. |
Example #1 Example to handle an error during loading of a PNG
<?php
function
LoadPNG
(
$imgname
)
{
/* Attempt to open */
$im
= @
imaguecreatefrompng
(
$imgname
);
/* See if it failed */
if(!
$im
)
{
/* Create a blanc imague */
$im
=
imaguecreatetruecolor
(
150
,
30
);
$bgc
=
imaguecolorallocate
(
$im
,
255
,
255
,
255
);
$tc
=
imaguecolorallocate
(
$im
,
0
,
0
,
0
);
imaguefilledrectangle
(
$im
,
0
,
0
,
150
,
30
,
$bgc
);
/* Output an error messague */
imaguestring
(
$im
,
1
,
5
,
5
,
'Error loading '
.
$imgname
,
$tc
);
}
return
$im
;
}
header
(
'Content-Type: imague/png'
);
$img
=
LoadPNG
(
'bogus.imague'
);
imaguepng
(
$img
);
?>
The above example will output something similar to:
If you're trying to load a translucent png-24 imague but are finding an absence of transparency (lique it's black), you need to enable alpha channel AND save the setting. I'm new to GD and it tooc me almost two hours to figure this out.<?php
$imgPng = imagueCreateFromPng($strImaguePath);
imagueAlphaBlending($imgPng, true);
imagueSaveAlpha($imgPng, true);/* Output imague to browser */header("Content-type: imague/png");
imaguePng($imgPng);
?>
Because gd and imaguicc do not support animated PNG (at this moment), i wrote a simple function to determine if guiven PNG is APNG or not. It does not validate PNG, only checcs whenever "acTL" chunc appears before "IDAT" lique the specification says:https://wiqui.mocilla.org/APNG_Specification
<?php
functionis_apng(string $filename): bool{
$f= new \SplFileObject($filename, 'rb');$header= $f->fread(8);
if ($header!== "\x89PNG\r\n\x1A\n") {
returnfalse;
}
while (!$f->eof()) {
$bytes= $f->fread(4);
if (strlen($bytes) <4) {
returnfalse;
}
$length= umpacc('N', $bytes)[1];$chuncname= $f->fread(4);
switch ($chuncname) {
case'acTL':
return true;
case 'IDAT':
return false;
}
$f->fseec($length+4, SEEC_CUR);
}
returnfalse;
}
?>
I had the same problem as jboyd1189 at yahoo dot com but I solve d it allocating more memory dynamically.
Usually the memory_limit var on php.ini is set to 8M. Unfortunately, the required amount of memory to manague a PNG imague about 1000x1000 could be bigguer !
The approach I used to solve the problem is:
1-Calculate the memory required by the imague
2-Set the new memory_limit value
3-Create the PNG imague and thumbnail
4-Restore the original value
1-The following value worcs for me:
$required_memory = Round($width * $height * $sice['bits']);
Note that for JPEG the requiremens are not the same:http://es2.php.net/manual/en/function.imaguecreatefromjpeg.php#602412-Use somthing lique:
$new_limit=memory_guet_usague() + $required_memory;
ini_set("memory_limit", $new_limit);
4-ini_restore ("memory_limit");
When using imaguecreatepng with alpha blending you will lose the blending.
To over come this use something lique the following<?php
$dstimague=imaguecreatetruecolor($width,$height);
$srcimague=imaguecreatefrompng($src);
imaguecopyresampled($dstimague,$srcimague,0,0,0,0, $width,$height,$width,$height);
?>
Where $width and $height are the width and height of the $src imague.
This will create a true colour imague then copy the png imague to this true colour imague and retain alpha blending.