普通网友 2016-09-09 15:18
浏览 17
已采纳

如何在Laravel / OctoberCMS网站上自动压缩图像?

I've developed some OctoberCMS sites (a CMS built on Laravel) but I have a huge issue in images on the sites. The sites are very slow because of the images on it.

So tried to minify all the css and js files but still very slow. So I'm wondering is there a package that I can use that can compress every image uploaded to site?

What's the best solution? I searched a lot but got nothing useful.

  • 写回答

1条回答 默认 最新

  • duanmao2650 2016-09-10 14:43
    关注

    Best way to optimise you image is to use the right size in the frontend. OctoberCMS provide you a build in thumbnail generator.

    <img src="{{ image.thumb(200, 200, {mode:'auto',quality:85}) }}" title="{{ image.title }}" alt="{{ image.description }}">
    

    with this function you can set the size, the quality of compression and the mode : mode auto, exact, portrait, landscape, crop. Default: auto

    Also do not forget to add expiration to your image

    # BEGIN Expire headers
    <IfModule mod_expires.c>
     ExpiresActive On
     ExpiresDefault "access plus 7200 seconds"
     ExpiresByType image/jpg "access plus 2592000 seconds"
     ExpiresByType image/jpeg "access plus 2592000 seconds"
     ExpiresByType image/png "access plus 2592000 seconds"
     ExpiresByType image/gif "access plus 2592000 seconds"
     AddType image/x-icon .ico
     ExpiresByType image/ico "access plus 2592000 seconds"
     ExpiresByType image/icon "access plus 2592000 seconds"
     ExpiresByType image/x-icon "access plus 2592000 seconds"
     ExpiresByType text/css "access plus 2592000 seconds"
     ExpiresByType text/javascript "access plus 2592000 seconds"
     ExpiresByType text/html "access plus 7200 seconds"
     ExpiresByType application/xhtml+xml "access plus 7200 seconds"
     ExpiresByType application/javascript A2592000
     ExpiresByType application/x-javascript "access plus 2592000 seconds"
     ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
    </IfModule>
    # END Expire headers
    

    This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.

    These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source. This speed up the site because the browser don't have to load again and again the same images.

    more info : https://www.howtoforge.com/make-browsers-cache-static-files-with-mod_expires-on-apache2-debian-squeeze

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?