ottobonn / hexo-image-sizes

Generate multiple images sizes (thumbnail, body, etc.) for each source image in Hexo
MIT License
15 stars 9 forks source link
hexo hexo-plugin image-processing

hexo-image-sizes

NPM

Generate multiple image resolutions for each source image in Hexo. Uses the awesome sharp image library under the hood.

What is it?

Let's say you have a static blog in Hexo, and you keep your awesome full-size photos in your _source directory with your Markdown files. You want to keep your original photos, but they're way too big to serve to your users.

You could manually scale each image, perhaps to the content width of your site. But what if you change your site theme? You need to resize all those images again.

Taking inspiration from dynamic CMSs like Wordpress, this Hexo plugin allows you to specify a "profile" for each type of image you want on your site, and it will take care of generating the resized image files and linking to them. An image profile is a semantic idea; each profile should correspond to a use-case for an image.

For example, if you have a gallery on the front page, you could create an image profile called front_gallery. Configure the sizes once and forget about it. If your theme changes, adjust the sizes and regenerate. Easy!

Installation

In your Hexo site's root directory, run

npm install hexo-image-sizes

Usage

Using hexo-image-sizes requires two steps: first, you need to set up your desired image profiles. Then, you need to embed your images in your posts.

Configure image profiles

First, you need to set up image profiles in your sitewide _config.yml. Add an image_sizes section to your config file, like this:

# Configuration for hexo-image-sizes
image_sizes:
  pattern: !!js/regexp /\.(gif|jpg|jpeg|png)$/i
  profiles:
    body:
      width: 700 # height will adjust to preserve aspect ratio
    thumbnail:
      width: 100 # Image will be cropped to a square
      height: 100
    huge:
      height: 1000
      allowEnlargement: true
  defaultProfile: body
  link: true
  linkProfile: huge
  useAltForTitle: true

The image_sizes config object supports the following fields:

Embed images

To use hexo-image-sizes, you need to alter the way you embed images in Markdown. This package provides support for the imsize tag, which you place in your posts' Markdown like this:

{% imsize %}
src: /uploads/2017/01/05/5510-repair.jpg
alt: Dell Precision 5510 repair
title: Cool beans!
profile: thumbnail
link: true
linkProfile: huge
{% endimsize %}

The body of the imsize tag is a YAML document. It supports these keys (others are ignored and may be used in the future):

Regenerate your site

After configuring things the way you want, run hexo clean and hexo generate to generate your site.