首页 > 解决方案 > 为 Web 压缩和调整图像大小

问题描述

所以我目前正在使用 php 后端和聚合物前端构建一个网站。客户希望能够为自己的事件提供新闻功能。为此,我想将所有图像转换为 webp 并创建一些不同的尺寸,以便我可以将它们快速提供给不同的浏览器(手机、平板电脑、桌面等)。但是,我还没有找到在 PHP 或 JS 中执行此操作的好方法。Squoosh 非常适合静态资产,但不适用于用户生成的内容。任何帮助表示感谢!

标签: javascriptphphtmlcsswebp

解决方案


PHP 具有处理 webp 图像的功能。尝试这个。

<?php
$im = imagecreatefromstring(file_get_contents('path/to/image.jpg')); // Create image identifier
imagewebp($im, 'path/to/image.webp'); // Generate webp image and save to location
imagedestroy($im); // Free up image identifier
?>

推荐阅读