imagecreatefromwebp

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

imagecreatefromwebpCrée une nouvelle image depuis un fichier ou une URL

Description

imagecreatefromwebp(string $filename): GdImage|false

imagecreatefromwebp() retourne un identifiant d'image représentant l'image obtenue depuis le fichier fourni. Notez que les fichiers WebP animés ne peuvent pas être lus.

Astuce

Vous pouvez utiliser une URL comme nom de fichier avec cette fonction, si le gestionnaire fopen a été activé. Voyez fopen() pour plus de détails sur la façon de spécifier le nom du fichier. Reportez-vous aux Liste des protocoles et des gestionnaires supportés pour plus d'informations sur les capacités des différents gestionnaires, les notes sur leur utilisation, ainsi que les informations sur les variables prédéfinies qu'elles fournissent.

Liste de paramètres

filename

Chemin vers l'image WebP.

Valeurs de retour

Retourne un objet d'image en cas de succès, ou false si une erreur survient.

Historique

Version Description
8.0.0 En cas de succès, cette fonction retourne désormais une instance de GDImage ; auparavant, une resource était retournée.

Exemples

Exemple #1 Convertit une image WebP en une image jpeg en utilisant la fonction imagecreatefromwebp()

<?php
// Charge le fichier WebP
$im = imagecreatefromwebp('./example.webp');

// On la convertit en un fichier jpeg avec une qualité à 100%
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
?>

add a note add a note

User Contributed Notes 1 note

up
0
kawewong at gmail dot com
2 years ago
PHP GD and WebP support:

Normal WebP (VP8): supported since PHP 5.4
Transparent WebP or alpha transparency (VP8X, VP8L): supported since PHP 7.0
Animated WebP (VP8X): not supported at all.

You can use the images from here https://developers.google.com/speed/webp/gallery2
here https://ezgif.com/help/alternative-animated-image-formats
and here https://developers.google.com/speed/webp/gallery1

Test with imagecreatefromwebp('your-image.webp'); and see the errors.

You can detect animated or transparent webp using this code.

<?php
/**
* Get WebP file info.
*
* @link https://www.php.net/manual/en/function.pack.php unpack format reference.
* @link https://developers.google.com/speed/webp/docs/riff_container WebP document.
* @param string $file
* @return array|false Return associative array if success, return `false` for otherwise.
*/
function webpinfo($file) {
    if (!
is_file($file)) {
        return
false;
    } else {
       
$file = realpath($file);
    }

   
$fp = fopen($file, 'rb');
    if (!
$fp) {
        return
false;
    }

   
$data = fread($fp, 90);

   
fclose($fp);
    unset(
$fp);

   
$header_format = 'A4Riff/' . // get n string
       
'I1Filesize/' . // get integer (file size but not actual size)
       
'A4Webp/' . // get n string
       
'A4Vp/' . // get n string
       
'A74Chunk';
   
$header = unpack($header_format, $data);
    unset(
$data, $header_format);

    if (!isset(
$header['Riff']) || strtoupper($header['Riff']) !== 'RIFF') {
        return
false;
    }
    if (!isset(
$header['Webp']) || strtoupper($header['Webp']) !== 'WEBP') {
        return
false;
    }
    if (!isset(
$header['Vp']) || strpos(strtoupper($header['Vp']), 'VP8') === false) {
        return
false;
    }

    if (
       
strpos(strtoupper($header['Chunk']), 'ANIM') !== false ||
       
strpos(strtoupper($header['Chunk']), 'ANMF') !== false
   
) {
       
$header['Animation'] = true;
    } else {
       
$header['Animation'] = false;
    }

    if (
strpos(strtoupper($header['Chunk']), 'ALPH') !== false) {
       
$header['Alpha'] = true;
    } else {
        if (
strpos(strtoupper($header['Vp']), 'VP8L') !== false) {
           
// if it is VP8L, I assume that this image will be transparency
            // as described in https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
           
$header['Alpha'] = true;
        } else {
           
$header['Alpha'] = false;
        }
    }

    unset(
$header['Chunk']);
    return
$header;
}
// webpinfo
?>

Reference: https://stackoverflow.com/a/68491679/128761

Usage:

<?php
$info
= webpinfo('your-image.webp');
if (isset(
$info['Animation']) && $info['Animation'] === true) {
    echo
'It is animated webp.';
}
if (isset(
$info['Alpha']) && $info['Alpha'] === true) {
    echo
'It is transparent webp.';
}
?>
To Top