All Projects → hexojs → hexo-filter-responsive-images

hexojs / hexo-filter-responsive-images

Licence: MIT license
Generate mutliple version of images for responsive Hexo blogs

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to hexo-filter-responsive-images

blog
🔥公众号:后端搬运工
Stars: ✭ 37 (+117.65%)
Mutual labels:  hexo
hexo-theme-ayer
一个干净优雅的hexo主题 A clean and elegant theme for Hexo. 🐋
Stars: ✭ 1,396 (+8111.76%)
Mutual labels:  hexo
oi.men.ci
Menci 的 OI 博客(源代码与网站文件)
Stars: ✭ 27 (+58.82%)
Mutual labels:  hexo
Seje
寫嘢 - A beatutiful Hexo Theme
Stars: ✭ 53 (+211.76%)
Mutual labels:  hexo
hexo-theme-chord
a simple hexo theme.
Stars: ✭ 15 (-11.76%)
Mutual labels:  hexo
hexo-renderer-less
Less renderer for Hexo.
Stars: ✭ 14 (-17.65%)
Mutual labels:  hexo
community
Volantis Community 社区主页 https://vlts.cc https://volantis.js.org
Stars: ✭ 85 (+400%)
Mutual labels:  hexo
hexo-jade-starter
Starter theme for Hexo implemented with Jade and Less.
Stars: ✭ 49 (+188.24%)
Mutual labels:  hexo
hexo-netlify-cms
☕ A cup of coffee time to enable Netlify CMS
Stars: ✭ 29 (+70.59%)
Mutual labels:  hexo
github.io
个人博客乁(๑˙ϖ˙๑乁) 国内用户 ->http://sunhang.top
Stars: ✭ 16 (-5.88%)
Mutual labels:  hexo
mulukhiya-toot-proxy
各種ActivityPub対応インスタンスへの投稿に対して、内容の更新等を行うプロキシ。通称「モロヘイヤ」。
Stars: ✭ 24 (+41.18%)
Mutual labels:  hexo
hexo-hide-posts
A plugin to hide specific posts from your Hexo blog and make them only accessible by links. (隐藏 Hexo 文章)
Stars: ✭ 149 (+776.47%)
Mutual labels:  hexo
hexo-theme-zurb-foundation
Simple plain Zurb foundation theme for the static site generator hexo.js
Stars: ✭ 22 (+29.41%)
Mutual labels:  hexo
hexo-theme-simple99
A minimalist theme developed based on hexo, welcome to use!基于hexo开发的双栏式简约风主题,欢迎使用!已适配 twikoo 静态评论系统。
Stars: ✭ 16 (-5.88%)
Mutual labels:  hexo
hexo-theme-materialized
A cool materialized theme for Hexo
Stars: ✭ 39 (+129.41%)
Mutual labels:  hexo
hexo-theme-wind
A Simple Hexo Theme.
Stars: ✭ 20 (+17.65%)
Mutual labels:  hexo
hexo-theme-next5-leaf
一款基于 Hexo v3.9 & Next v5.1.4 魔改的主题 🌞
Stars: ✭ 82 (+382.35%)
Mutual labels:  hexo
zhisheng17.github.io
my blog website —— www.54tianzhisheng.cn
Stars: ✭ 92 (+441.18%)
Mutual labels:  hexo
hexo-filter-mathjax
💯 Server side MathJax renderer plugin for Hexo.
Stars: ✭ 76 (+347.06%)
Mutual labels:  hexo
Hibop.github.io
Hibop 个人博客
Stars: ✭ 20 (+17.65%)
Mutual labels:  hexo

hexo-filter-responsive-images

build status npm version

Generate mutliple versions of images for responsive blogs using Hexo >= 3.x. It uses sharp library to transform images.

Comparison to similar plugins:

  • hexo-img-optimization adds a separate command and doesn't work with hexo server. Requires ImageMagick to be installed. This plugin is integrated with hexo generate and works with hexo server.
  • hexo-image-sizes is very similar, but this one is optimized for performance for local testing by being lazy - nothing is transformed unless requested.

Installation

npm i hexo-filter-responsive-images --save

Configuration

Put all your configuration under responsive_images key.

Pattern

pattern: String

A micromatch pattern. All matching assets will use the assigned size rules.

Sizes

sizes:
  [name]:
    width: Number
    height: Number
    ...

Put a size name as a key. It will be used as a prefix for the generated file names. Use width and height keys to configure dimensions. Skip one for auto-scale.

Everything else will be passed to the resize method of sharp. See the most important options listed below.

Resizing options

fit: String

After sharp documentation:

  • cover: Default value. Crops to cover both provided dimensions.
  • contain: Embeds within both provided dimensions.
  • fill: Ignores the aspect ratio of the input and stretch to both provided dimensions.
  • inside: Resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. Preserves aspect ratio.
  • outside: Resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified. Preserves aspect ratio.
position: String

Works for cover or contain fit setting. Should be value from either sharp.position or sharp.gravity list, for example 'right' or 'northeast'

withoutEnlargement: Boolean

When true, do not enlarge if the width or height are already less than the specified dimensions

quality: Number

It's a number from 0 to 100, which controls quality of the output file. Works with jpg, webp and tiff format.

Full information about sharp API specific options can be found in the sharp documentation

Priority

Optionally, you can put pattern and sizes configuration under rules key and use priority option to set the priority of after_generate filter. Can be handy, if you want to control the order of filters executed on your files.

priority: 9
rules: ...

You can find more information about priority in Filter documentation.

Examples

Single pattern:

responsive_images:
  pattern: '**/*.+(png|jpg|jpeg)'
  sizes:
    small:
      width: 800
    medium:
      width: 1200
    large:
      width: 2000

For your photo.jpg it will generate the following files:

small_photo.jpg
medium_photo.jpg
large_photo.jpg

You can also use multiple patterns:

responsive_images:
  - pattern: squares/*.jpg
    sizes:
      square:
        width: 200
        height: 200
        
  - pattern: '**/*.+(png|jpg|jpeg)'
    sizes:
      thumb:
        width: 900

And the example with priority:

responsive_images:
  priority: 9
  rules:
    - pattern: squares/*.jpg
      sizes:
        square:
          width: 200
          height: 200

    - pattern: '**/*.+(png|jpg|jpeg)'
      sizes:
        thumb:
          width: 900

The following example uses sharp API specific options for advanced control:

responsive_images:
  pattern: '**/*.+(png|jpg|jpeg)'
  sizes:
    small:
      width: 800
      height: 800
      fit: inside
    large:
      width: 2000
      withoutEnlargement: true

Usage

To get the responsive image URL you can just refer to it's prefixed version. For a programmatic usage in theme templates, you can use a view helper:

image_version(image_path, {prefix: ''})

For example:

image_version('photo.jpg', {prefix: 'small')

It returns 'small_photo.jpg'

For usage in generated content you can hook into the after_post_render filter.

const image_version = hexo.extend.helper.get('image_version');
hexo.extend.filter.register('after_post_render', data => {
  data.content = data.content.replace(/<img([^>]+)?>/igm, (_, attr) => {
    attr = attr.replace(/src="([^"]+)?"/, (_, src) => {
      return `src="${image_version(src, { prefix: 'thumb' })}"`;
    })
    return `<img${attr}>`;
  });
});

Development

To run specs use:

npm test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].