javascript - 为 Web 压缩和调整图像大小
问题描述
所以我目前正在使用 php 后端和聚合物前端构建一个网站。客户希望能够为自己的事件提供新闻功能。为此,我想将所有图像转换为 webp 并创建一些不同的尺寸,以便我可以将它们快速提供给不同的浏览器(手机、平板电脑、桌面等)。但是,我还没有找到在 PHP 或 JS 中执行此操作的好方法。Squoosh 非常适合静态资产,但不适用于用户生成的内容。任何帮助表示感谢!
解决方案
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
?>
推荐阅读
- java - 无法在 netbean 8.2 和 jdk1.8.0_231 中运行程序“cmd”java maven 项目
- django - Django - 'bool' 对象不可调用
- xcode - 无法加载“网络链接调节器”(Catalina)
- elasticsearch - Elasticsearch:通过嵌套对象查询查找内部命中
- android - 添加 Places API 依赖项时出现主题错误
- c# - MessageBox 不抑制键盘 Enter 键
- python - JAX:jit 函数的时间随着函数访问的内存而超线性增长
- jsp - 如何在 Struts 标签的属性中使用 Scriptlet 变量?
- django - 如何将基于类的视图数据传递给 django formset
- python - 如何在python中访问不同类中任何类的变量和函数