dongshou6041 2019-05-19 21:39
浏览 153

PHP7 - 优化图像(压缩而不会丢失质量)

I have a problem with my project. I want to optimize images on it. I tried to convert images to .webp, but I found that it not supported by browsers.

When I test my website on PageSpeedInsight of Google, it's given me warning about images and I need to compress them. and at the same time, he gives me the new compress picture, perfectly and with small size.

But I try to do that with PHP, but I can't get the same result of google page Insight.

Message from pageSpeedInsight:

Image formats like JPEG 2000, JPEG XR, and WebP often provide better compression than PNG or JPEG, which means faster downloads and less data consumption

Is there any best solution to optimize images? I use a native PHP framework (MVC).

  • 写回答

1条回答 默认 最新

  • duanlei2458 2019-05-19 21:48
    关注

    You can install Intervention Image library, to convert your images to WEBP format.

    And use .htaccess to show the webp images when server supports, cuz not all browsers support WEBP images. In this case, you must have a copy of the image in other common extension (jpg, png) and you can compress with tinypng API.

    <IfModule mod_setenvif.c>
        # Vary: Accept for all the requests to jpeg and png
        SetEnvIf Request_URI "\.(jpe?g|png)$" REQUEST_image
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Check if browser supports WebP images
        RewriteCond %{HTTP_ACCEPT} image/webp
    
        # Check if WebP replacement image exists
        RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
    
        # Serve WebP image instead
        RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp]
    </IfModule>
    
    <IfModule mod_headers.c>
        Header append Vary Accept env=REQUEST_image
    </IfModule>
    
    <IfModule mod_mime.c>
        AddType image/webp .webp
    </IfModule>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料