普通网友 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

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

报告相同问题?

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序