Hexo Advanced Image Optimizer Plugin

The Hexo Advanced Image Optimizer Plugin enhances your Hexo site by leveraging the power of wsrv.nl, a high-performance image cache and resize service. Powered by Cloudflare’s CDN, it delivers images from over 300 global datacenters, ensuring lightning-fast load times and optimal performance. With support for advanced image processing using technologies like nginx and libvips, this plugin transforms and optimizes images on-the-fly. It’s open-source, free to use, and seamlessly integrates with Hexo to process markdown images, cover images, CSS background images, and <object> tags.

Installation

Run the (any) following command in your Hexo project directory to install the plugin:

bash
npm install hexo-adv-img-optimizer --save
bash
bun add hexo-adv-img-optimizer --save
bash
pnpm install hexo-adv-img-optimizer --save

Usage

After installing the plugin, all image urls will be converted to CDN urls automatically. For example, the origin markdown file is

md
![Image Caption](//wsrv.nl/?url=https%3A%2F%2Fwsrv.nl%2Flichtenstein.jpg&default=https%3A%2F%2Fdynamic-og-image-generator.vercel.app%2Fapi%2Fgenerate%3Ftitle%3DImage%2Bis%2Benjoying%2Bvacation...%26author%3DHarman%2BSingh%2BHira%26websiteUrl%3D%252F%252Fme.hsinghhira.me%26avatar%3Dhttps%253A%252F%252Favatars.githubusercontent.com%252Fu%252F11346694%26theme%3Dgithub&filename=hexo-advanced-image-optimizer-plugin)

Then it will be converted to

md
![Image Caption](//wsrv.nl/?url=https%3A%2F%2Fimages.weserv.nl%2F%3Furl%3Dhttps%3A%2F%2Fwsrv.nl%2Flichtenstein.jpg&default=https%3A%2F%2Fdynamic-og-image-generator.vercel.app%2Fapi%2Fgenerate%3Ftitle%3DImage%2Bis%2Benjoying%2Bvacation...%26author%3DHarman%2BSingh%2BHira%26websiteUrl%3D%252F%252Fme.hsinghhira.me%26avatar%3Dhttps%253A%252F%252Favatars.githubusercontent.com%252Fu%252F11346694%26theme%3Dgithub&filename=hexo-advanced-image-optimizer-plugin)

The local image urls will be also converted. For example, the original file is

md
![Image Caption](//wsrv.nl/?url=http%3A%2F%2Fme.hsinghhira.me%2Fimg%2Fanti996.png&default=https%3A%2F%2Fdynamic-og-image-generator.vercel.app%2Fapi%2Fgenerate%3Ftitle%3DImage%2Bis%2Benjoying%2Bvacation...%26author%3DHarman%2BSingh%2BHira%26websiteUrl%3D%252F%252Fme.hsinghhira.me%26avatar%3Dhttps%253A%252F%252Favatars.githubusercontent.com%252Fu%252F11346694%26theme%3Dgithub&filename=hexo-advanced-image-optimizer-plugin)

It will be rendered as

md
![Image Caption](//wsrv.nl/?url=https%3A%2F%2Fimages.weserv.nl%2F%3Furl%3Dhttps%3A%2F%2Fexample.com%2Fimg%2Fanti996.png&default=https%3A%2F%2Fdynamic-og-image-generator.vercel.app%2Fapi%2Fgenerate%3Ftitle%3DImage%2Bis%2Benjoying%2Bvacation...%26author%3DHarman%2BSingh%2BHira%26websiteUrl%3D%252F%252Fme.hsinghhira.me%26avatar%3Dhttps%253A%252F%252Favatars.githubusercontent.com%252Fu%252F11346694%26theme%3Dgithub&filename=hexo-advanced-image-optimizer-plugin)

where https://example.com is the url of your blog that you set in _config.yml.

Configuration

The plugin can be configured globally in Hexo’s _config.yml file under the cdn section or per post using front-matter in Markdown files. Front-matter settings override global settings for specific posts.

  1. Open your Hexo project’s _config.yml file.
  2. Add a cdn section to configure global settings (see Configuration below).
    Example:
yaml
cdn:
  cdn_server: //wsrv.nl
  use_webp: true
  max_width: [1920, 1200, 800, 400]

Global Configuration (_config.yml)

Add a cdn section to _config.yml to set default options for all images. All options are optional, and defaults are applied when not specified.

yaml
cdn:
  cdn_server: //wsrv.nl
  native: false
  use_webp: true
  max_width: 1200
  filename: default-image
  brightness: 0
  quality: 75
  fit: cover
  .
  .
  .

Per-Post Configuration (Front-Matter)

Add a cdn section to a post’s front-matter to override global settings for that post’s images (e.g., cover image, markdown images, background images). For example:

yaml
---
title: My Post
cdn:
  brightness: 20
  filename: post-specific-image
  quality: 80
  fit: contain
  .
  .
  .
---

Available Configuration Options

Below is a list of all available configuration options, which can be set in _config.yml or post front-matter. Most options support both full names (e.g., brightness) and aliases (e.g., bri) for compatibility with the CDN’s query parameters.

OptionAliasTypeDescriptionExample
cdn_serverserverStringCDN server URL (default: https://images.weserv.nl).https://images.weserv.nl
nativeBooleanUse Cloudflare native resize instead of images.weserv.nl (default: false).true
use_webpcdn_use_webpBooleanEnable WebP format in ``elements (default:false).true
max_widthNumber or ArrayMaximum width(s) for responsive images (e.g., [1200, 800, 400]).1200 or [1200, 800]
exclude_domainsArrayDomains to exclude from CDN processing.["example.com", "cdn.com"]
heighthNumberImage height in pixels.600
device_pixel_ratiodprNumberDevice pixel ratio for high-DPI displays.2
fitStringImage fit mode (inside, outside, cover, fill, contain).cover
alignmentaStringImage alignment (center, top, right, bottom, left, focal).center
focal_point_xfpxNumber (0.0-1.0)X-coordinate for focal point (used with alignment: focal).0.5
focal_point_yfpyNumber (0.0-1.0)Y-coordinate for focal point (used with alignment: focal).0.5
cropcString or ArrayCrop coordinates [x,y,w,h] or string.[10,20,100,100] or 10,20,100,100
precropString or ArrayPre-resize crop coordinates [x,y,w,h] or string.[10,20,100,100]
trimBoolean or NumberTrim boring pixels (true for default 10, or 1-254).true or 20
maskStringMask shape (circle, ellipse, triangle, pentagon, hexagon, square, star, heart).circle
mask_trimmtrimBooleanRemove whitespace from mask.true
mask_backgroundmbgStringMask background color (hex without #).ffffff
qualityqNumber (0-100)Image quality.80
compressionlNumber (0-9)Compression level.6
losslessllBooleanUse lossless WebP compression (requires WebP output).true
backgroundbgStringBackground color (hex without #).000000
rotation_backgroundrbgStringBackground color for rotation (hex without #).ffffff
blurNumber (0.3-1000)Apply blur effect.5
sharpensharpNumber (0-10)Sharpen effect.3
sharpen_flatsharpfNumber (0-1000000)Sharpen flat areas.100
sharpen_jaggedsharpjNumber (0-1000000)Sharpen jagged areas.100
contrastconNumber (-100 to 100)Adjust contrast.10
brightnessbriNumber (-100 to 100)Adjust brightness.20
gammagamNumber (1.0-3.0)Adjust gamma.1.5
saturationsatNumberSaturation multiplier.1.2
hueNumberHue rotation in degrees.90
modulatemodString or ArrayBrightness, saturation, hue adjustments [b,s,h] or string.[1.1,1.2,90] or 1.1,1.2,90
tintStringTint color (hex without #).ff0000
filterfiltStringApply filter (greyscale, sepia, duotone, negate).greyscale
filter_startstartStringDuotone start color (hex without #).000000
filter_stopstopStringDuotone stop color (hex without #).ffffff
rotationroString or NumberRotation angle (0-360 or auto).90
flipBooleanMirror vertically (up-down).true
flopBooleanMirror horizontally (left-right).true
output_formatoutputStringOutput format (jpg, png, gif, webp).webp
encodingStringEncoding type (e.g., base64 for data URL).base64
without_enlargementweBooleanPrevent image enlargement.true
adaptive_filterafBooleanUse adaptive filter.true
interlaceilBooleanEnable progressive/interlaced rendering.true
max_agemaxageStringCache control duration (e.g., 1d, 1y).1d
pageNumberPage number for multi-page images (e.g., PDFs).1
nNumberNumber of pages to process (-1 for all).-1
filenameStringCustom filename for downloads (default: page title, e.g., hello-example).custom-image
default_imagedefaultStringFallback image URL if the original fails.https://example.com/fallback.jpg

Notes

  • Priority: Front-matter options (cdn: ...) in a post override global settings in _config.yml. If neither is set, defaults like the page-title-based filename (hello-example for “Hello Example”) or null are used.
  • Aliases: Use either full names (e.g., brightness) or aliases (e.g., bri) in _config.yml or front-matter.
  • Validation: Ensure values match the expected format (e.g., crop as [10,20,100,100], brightness as a number between -100 and 100) to avoid errors.
  • SVG Exclusion: SVG images are excluded from CDN processing to preserve their vector format.
  • CDN Support: Verify that your CDN (e.g., images.weserv.nl) supports the specified options. Invalid values may be ignored.
  • Testing: After configuration, run hexo generate and inspect the generated HTML to confirm CDN URLs include the desired parameters (e.g., https://images.weserv.nl/?url=...&bri=20&filename=post-specific-image).

Troubleshooting

  • Images Not Processed: Ensure the plugin is installed correctly. Check for exclude_domains that might skip certain images.
  • Incorrect Filenames: Verify the filename setting in _config.yml or front-matter. If using the page-title default, ensure posts have a title in their front-matter.
  • CDN Errors: Confirm that the cdn_server supports the configured options (e.g., images.weserv.nl documentation: https://images.weserv.nl/).
  • WebP Not Working: Set use_webp: true and ensure the browser supports WebP. Check if output_format: webp is set correctly for specific images.

For additional help or feature requests, please open an issue on the plugin’s repository or contact the maintainer.

Special Thanks


Made with ❤️ in Aotearoa.