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>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!