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 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)