定制 shdev/spress-plugin-assets 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shdev/spress-plugin-assets

Composer 安装命令:

composer require shdev/spress-plugin-assets

包简介

It allows to asset boost your Spress site

关键字:

README 文档

README

Spress 2 ready

This plugin has the aim to bring the most features of jekyll-assets to SPRESS.

**This plugin requires Spress >= 2.2.

How to install?

Go to your site folder and input the following command:

$ spress add:plugin shdev/spress-plugin-assets

As as soft dependency you need ImageMagick

Under macOS I used Homebrew to install it with

$ brew install imagemagick

How to use?

Store your assets in src/assets with your folder structure

For example this structure

./src/assets/
|- img
   |- header.jpg
   |- feature-image.jpg
|- css
   |- style.css
|- js
   |- app.js

In config.yml you can set the following options. Here the default values are displayed.

asset_output_path: 'build/assets'
asset_output_web_prefix: '/assets'
asset_defaults_gravity: 'Center'
asset_defaults_quality: ~

asset_output_path the path where the file is stored during building.

asset_output_web_prefix is the path prefix for the link.

asset_defaults_gravity is the default gravity for cropping a image if nothing is given in asset-filter options

asset_defaults_quality is the default quality for images. I would recommend 90. If this is null imagemagick is not necessary as dependency.

For example if you use a CDN you want the assets in an own build-folder and the URL prefix contains information about your CDN.

In your template you use the path relative to src/assets for your items with the asset filters.

Filter asset_path

This filter gives the path of

{{ 'img/header.jpg' | asset_path }}

Renders the URL

<asset_output_web_prefix>/img/header_813dec0e6ec1420d101c3f07bfc0a135.jpg

and writes the file

<asset_output_path>/img/header_813dec0e6ec1420d101c3f07bfc0a135.jpg

You see the output file is enriched with a hash value. It's a md5 sum which depends on the content of the file and some other information.

Filter img

{{ 'img/header.jpg' | img }}

Renders the HTML

<img src="<asset_output_web_prefix>/img/header_813dec0e6ec1420d101c3f07bfc0a135.jpg">

and writes the file

<asset_output_path>/img/header_813dec0e6ec1420d101c3f07bfc0a135.jpg

Filter css

{{ 'css/style.css' | css }}

Renders the HTML

<link rel="stylesheet" href="<asset_output_web_prefix>/css/style_813dec0e6ec1420d101c3f07bfc0a135.css"/>

and writes the file

<asset_output_path>/css/style_813dec0e6ec1420d101c3f07bfc0a135.css

Filter js

{{ 'js/app.js' | js }}

Renders the HTML

<script src="<asset_output_web_prefix>/js/app_813dec0e6ec1420d101c3f07bfc0a135.js"></script>

and writes the file

<asset_output_path>/js/app_813dec0e6ec1420d101c3f07bfc0a135.js

Filter options for img|css|js

If you want to set some attributes to the tags use the attr options. This options only effects the render output.

Example with img

{{ 'img/header.jpg' | img({attr:{class:'img-responsive', id:"header-image", alt:"Some importend text about the image"}}) }}

Renders the HTML

<img class="img-responsive" id="header-image" alt="Some importend text about the image" src="<asset_output_web_prefix>/img/header_813dec0e6ec1420d101c3f07bfc0a135.jpg">

Filter options for all tags if the file extension is jpg|jpeg|png|gif

Like the shining example jekyll-assets it uses ImageMagick for image manipulation.

You can use it with every filter but it should only usefull with img and asset_path

This options of the convert-command are used:

Also when crop is used a +repage will appended, look here repage for more information

Here an example with all options at once

{{ 'img/feature-image.jpg' | img({resize: '300x300^', crop: '300x300+0+0', gravity: 'SouthEast', quality:60 }) }}

This creates a new images which is quadratic.

  • resize: '300x300^' means resize the image that it cover a 300px times 300px area
  • crop: '300x300+0+0' means crop to 300px times 300px but
  • gravity: 'SouthEast' use the bottom-right corner
  • quality:60 reduce the quality to 60%.

Renders the HTML

<img src="<asset_output_web_prefix>/img/feature-image_6fa6fa3af470d85af98ac7f7b8d5c933.jpg">

and writes the file

<asset_output_path>/img/feature-image_6fa6fa3af470d85af98ac7f7b8d5c933.jpg

Be informed different image transformation options lead to other hash values used for the file.

So

{{ 'img/feature-image.jpg' | img }}

will not create the file

<asset_output_path>/img/feature-image_6fa6fa3af470d85af98ac7f7b8d5c933.jpg

Cache folder

For a better performance the plugin cached all files in the folder .cache/assets. This saved time for the image manipulation. It will not clear old unused file. So please clean it up from time to time.

asset boost on apache webserver

As an extra you can asset boost your site if it runs with an apache webserver with the following .htaccess options.

RewriteEngine On

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+\.(xml|html|css|js|eot|eot|woff2|woff|ttf|svg))$ /$1.gz [L]

<FilesMatch "\.(xml|xml.gz|html|htm|html.gz|htm.gz)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
</FilesMatch>

<FilesMatch "\.(gz)$">
Header set Vary "Accept-Encoding" 
Header set Content-Encoding "gzip"
</FilesMatch>

<FilesMatch "\.js.gz$">
Header set Content-Type "application/javascript"
</FilesMatch>

<FilesMatch "\.css.gz$">
Header set Content-Type "text/css; charset=UTF-8"
</FilesMatch>

<FilesMatch "_[a-f0-9]{32}\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|ico.gz|pdf.gz|flv.gz|jpg.gz|jpeg.gz|png.gz|gif.gz|swf.gz|js|css|css.gz|js.gz)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header set Cache-Control "max-age=8916000, public"
</FilesMatch>

I generate the gzip files as an after build step.

Wished features for the next releases

  • a posteriori run of jpegoptim und optipng
  • filter for outputing raw asset to twig. E.g. inline js or css
  • filter for outputing transformed image to twig. E.g. inline base64 images
  • asset boost configs for other web servers, I hope for pull requests
  • render css|js or other files with twig before coping, this allows to inject e.g. colors from config.yml to the style files.
  • scss processing
  • babel processing

shdev/spress-plugin-assets 适用场景与选型建议

shdev/spress-plugin-assets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「plugin」 「spress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shdev/spress-plugin-assets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shdev/spress-plugin-assets 我们能提供哪些服务?
定制开发 / 二次开发

基于 shdev/spress-plugin-assets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-16