压缩和调整Web图片的大小

时间:2019-01-09 21:18:23

标签: javascript php html css webp

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

4 个答案:

答案 0 :(得分:0)

调整大小必须在服务器端完成。您可以做的是使用image标签的srcsetsizes属性来优化要使用的版本:

<img srcset="elva-fairy-320w.jpg 320w,
             elva-fairy-480w.jpg 480w,
             elva-fairy-800w.jpg 800w"
     sizes="(max-width: 320px) 280px,
            (max-width: 480px) 440px,
            800px"
     src="elva-fairy-800w.jpg" alt="Elva dressed as a fairy" />

(直接来自Mozilla文档)

答案 1 :(得分:0)

我强烈建议使用Adobe Photoshop。借助此功能,您可以手动压缩/调整图像大小或批量提交图像。

答案 2 :(得分:0)

I don't know if you have access to the server, but one way could be to call ImageMagick from PHP. It would require for PHP to interact with the server, which can be dangerous, so please keep that in mind.

ImageMagick although don't support webm, to my knowledge, but Im sure you get the idea behind the though.

If you don't want PHP to interact with the server itself, you could also scan for non-converted / Resized images, and then convert them. On linux it could be: find ./ -name "*.jpg" -exec CONVERT_FUNCTION{} \;

答案 3 :(得分:0)

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
?>