普通网友 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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧