imagecreatefrompng

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecreatefrompng β€” Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠ· Ρ„Π°ΠΉΠ»Π° ΠΈΠ»ΠΈ URL-адрСса

ОписаниС

function imagecreatefrompng(string $filename): GdImage|false

Ѐункция imagecreatefrompng() Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ изобраТСния, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ»ΠΈ ΠΏΠΎ названию Ρ„Π°ΠΉΠ»Π°.

Подсказка

ВмСсто Π½Π°Π·Π²Π°Π½ΠΈΠΉ Ρ„Π°ΠΉΠ»ΠΎΠ² функция ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ URL-адрСса, Ссли Π²ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρƒ fopen wrappers. ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅ ΠΎΠ± ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠΈ названия Ρ„Π°ΠΉΠ»Π° рассказываСт описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ fopen(). Π Π°Π·Π΄Π΅Π» Β«ΠŸΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»Ρ‹ ΠΈ ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠΈΒ» Ρ‚Π°ΠΊΠΆΠ΅ Π΄Π°Ρ‘Ρ‚ ссылки Π½Π° ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΡ‹Ρ… ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ°Ρ…, содСрТит замСчания ΠΏΠΎ Ρ€Π°Π±ΠΎΡ‚Π΅ с ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ°ΠΌΠΈ ΠΈ ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΡ‚ список ΠΏΡ€Π΅Π΄ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½Ρ‹Ρ… ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΎΠ½ΠΈ Π΄Π°ΡŽΡ‚.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

filename

ΠŸΡƒΡ‚ΡŒ ΠΊ PNG-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ изобраТСния, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса GDImage, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ; Ρ€Π°Π½ΡŒΡˆΠ΅ возвращался рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ошибки ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅ изобраТСния Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ PNG

<?php

function LoadPNG($imgname)
{
/* ΠŸΡ‹Ρ‚Π°Π΅ΠΌΡΡ ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ */
$im = @imagecreatefrompng($imgname);

/* Если Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ */
if (!$im) {
/* Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ пустоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ */
$im = imagecreatetruecolor(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, 'Ошибка Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ ' . $imgname, $tc);
}

return
$im;
}

header('Content-Type: image/png');

$img = LoadPNG('bogus.image');

imagepng($img);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования imagecreatefrompng()
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
85
Anonymous ΒΆ
22 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.

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

/* Output image to browser */
header("Content-type: image/png");
imagePng($imgPng); 
?>
up
5
sageptr at gmail dot com ΒΆ
4 years ago
Because gd and imagick do not support animated PNG (at this moment), i wrote a simple function to determine if given PNG is APNG or not. It does not validate PNG, only checks whenever "acTL" chunk appears before "IDAT" like the specification says: https://wiki.mozilla.org/APNG_Specification

<?php

function is_apng(string $filename): bool
{
    $f = new \SplFileObject($filename, 'rb');
    $header = $f->fread(8);
    if ($header !== "\x89PNG\r\n\x1A\n") {
        return false;
    }
    while (!$f->eof()) {
        $bytes =  $f->fread(4);
        if (strlen($bytes) < 4) {
            return false;
        }
        $length = unpack('N', $bytes)[1];
        $chunkname = $f->fread(4);
        switch ($chunkname) {
            case 'acTL':
                return true;
            case 'IDAT':
                return false;
        }
        $f->fseek($length + 4, SEEK_CUR);
    }
    return false;
}

?>
up
11
marcos at assl-site dot net ΒΆ
19 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:
http://es2.php.net/manual/en/function.imagecreatefromjpeg.php#60241

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

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

To over come this use something like the following
<?php
$dstimage=imagecreatetruecolor($width,$height);
$srcimage=imagecreatefrompng($src);
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.