imagewebp

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

imagewebp将 WebP 格式的图像输出到浏览器或文件

说明

imagewebp ( resource $image , mixed $to = null , int $quality = 80 ) : bool

image 参数指定的图像以 WebP 格式输出到浏览器或者保存到文件。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的图象资源。

to

文件保存的路径,如果未设置或为 null,将会直接输出原始图象流。

quality

quality 范围从0(最低质量,最小文件体积)到100 (最好质量, 最大文件体积)。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
5.4.0 支持把 resource 流作为 to 参数传入。

范例

Example #1 保存为 WebP 图像文件

<?php
// 创建一个空图像并在其上加入一些文字
$im imagecreatetruecolor(12020);
$text_color imagecolorallocate($im2331491);

imagestring($im155,  'WebP with PHP'$text_color);

// 保存图像
imagewebp($im'php.webp');

// 释放内存
imagedestroy($im);
?>

User Contributed Notes

Sam 24-Jun-2021 08:47
Safari on mac now has limited support (limited to Safari 14+ on Big Sur or later)

Safari on iOS 14.4 and higher has full support
vinicius dot sizilio at gmail dot com 18-Apr-2020 01:07
To convert a PNG image to Webp, we can do this:

<?php

// Image
$dir = 'img/countries/';
$name = 'brazil.png';
$newName = 'brazil.webp';

// Create and save
$img = imagecreatefrompng($dir . $name);
imagepalettetotruecolor($img);
imagealphablending($img, true);
imagesavealpha($img, true);
imagewebp($img, $dir . $newName, 100);
imagedestroy($img);

?>
day4gerard 14-Nov-2019 07:14
WebP is not yet supported by Safari, although they are experimenting with it.

Check out https://caniuse.com/#search=webp for the latest support information.
Khalyomede 01-Feb-2019 07:24
As of today (end of january 2019), WebP is now supported across all the major browsers (Edge, Chrome, Firefox, Opera).
mauro dot ludovico dot colella at gmail dot com 12-Feb-2015 06:41
It is also possible to defer the creation of png replacements for WebP images to the client, with notably the library libwebpjs enabling transparent conversion.
orangefish at sogetthis dot com 25-Jun-2013 11:32
WebP is a great file format, but it's basically supported only by Chrome. For WebP files with transparency it's necessary to have PNG fallback for other browsers (otherwise it won't work in iOS, Firefox, IE, etc.).

Regular truecolor PNG with alpha gives pretty large files, but there's a special smaller PNG file variant that can be created by pngquant - a command line utility.

If you have pngquant 1.8 on your server (just get package from official pngquant website), then you can create small fallback images (with quality better than from PHP's libgd):

<?php

/**
 * Optimizes PNG file with pngquant 1.8 or later (reduces file size of 24-bit/32-bit PNG images).
 *
 * You need to install pngquant 1.8.x on the server (ancient version 1.0 won't work).
 * There's package for Debian/Ubuntu and RPM for other distributions on http://pngquant.org
 *
 * @param $path_to_png_file string - path to any PNG file, e.g. $_FILE['file']['tmp_name']
 * @param $max_quality int - conversion quality, useful values from 60 to 100 (smaller number = smaller file)
 * @return string - content of PNG file after conversion
 */
function compress_png($path_to_png_file, $max_quality = 90)
{
    if (!
file_exists($path_to_png_file)) {
        throw new
Exception("File does not exist: $path_to_png_file");
    }

   
// guarantee that quality won't be worse than that.
   
$min_quality = 60;

   
// '-' makes it use stdout, required to save to $compressed_png_content variable
    // '<' makes it read from the given file path
    // escapeshellarg() makes this safe to use with any path
   
$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg(    $path_to_png_file));

    if (!
$compressed_png_content) {
        throw new
Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
    }

    return
$compressed_png_content;
}
?>

So for example when user is uploading a PNG file:

<?php

$read_from_path
= $_FILE['file']['tmp_name'];
$save_to_path = "uploads/compressed_file.png";

$compressed_png_content = compress_png($read_from_path);
file_put_contents($save_to_path, $compressed_png_content);

// you don't need move_uploaded_file().

// and for webp:
imagewebp(imagecreatefrompng($read_from_path), $save_to_path + ".webp");
?>

And then you can use URL with .webp version in Chrome and browsers that send Accept: image/webp, and .png for the rest (and all will get small file!)