PHP 7.2.0 Beta 1 Released


(PHP 4, PHP 5, PHP 7)

imagecreatefrompng Erzeugt ein neues Bild im PNG-Format, welches aus einer Datei oder von einer URL gelesen wird


resource imagecreatefrompng ( string $filename )

ImageCreateFromPNG() gibt den Bezeichner auf ein Bild zurück, der das aus der angegebenen Datei eingelesene Bild darstellt.

Im Fehlerfall gibt ImageCreateFromPNG() eine leere Zeichenkette zurück. Zudem wird eine Fehlermeldung erzeugt, die im Browser als Verbindungsabbruch dargestellt wird. Zum besseren Debuggen wird das folgende Beispiel einen JPEG-Fehler erzeugen:

Beispiel #1 Beispiel, um die Handhabung eines Fehlers bei der Bilderzeugung zu sehen (Dank an

function Loadpng ($imgname) {
    $im = @ImageCreateFromPNG ($imgname); /* Versuch, Datei zu öffnen */
    if (!$im) {                           /* Prüfen, ob fehlgeschlagen */
        $im = ImageCreate (150, 30);      /* Erzeugen eines leeren Bildes */
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
        ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); 
        /* Ausgabe einer Fehlermeldung */
        ImageString($im, 1, 5, 5, "Fehler beim Öffnen von: $imgname", $tc); 
    return $im;
add a note add a note

User Contributed Notes 5 notes

13 years ago
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.

= imageCreateFromPng($strImagePath);
imageAlphaBlending($imgPng, true);
imageSaveAlpha($imgPng, true);

/* Output image to browser */
header("Content-type: image/png");
marcos at assl-site dot net
10 years ago
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 manage a PNG image about 1000x1000 could be bigger !

The approach I used to solve the problem is:

1-Calculate the memory required by the image
2-Set the new memory_limit value
3-Create the PNG image and thumbnail
4-Restore the original value

1-The following value works for me:
$required_memory = Round($width * $height * $size['bits']);

Note that for JPEG the requirements are not the same:

2-Use somthing like:
$new_limit=memory_get_usage() + $required_memory;
ini_set("memory_limit", $new_limit);

4-ini_restore ("memory_limit");
admin at wdfa dot co dot uk
8 years ago
When using imagecreatepng with alpha blending you will lose the blending.

To over come this use something like the following
imagecopyresampled($dstimage,$srcimage,0,0,0,0, $width,$height,$width,$height);
Where $width and $height are the width and height of the $src image.

This will create a true colour image then copy the png image to this true colour image and retain alpha blending.
1 year ago
The image size (width x height) should not exceed INT_MAX ... It's still thrue on a 64 bits computer.
jboyd1189 at yahoo dot com
11 years ago
I was having a terrible time with the imagecreatefrompng function as it was working perfectly for one image and not at all for another.  After many hours of frustration, I discovered that the problem was the image size (number of pixels).  It appears that the maximum number of pixels this function will process is 1,040,000.  So, be sure that the pixel resolution of the image (eg. 1040 x 1000) does not exceed this value.
To Top