GithubHelp home page GithubHelp logo

ottobonn / hexo-image-sizes Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 9.0 45 KB

Generate multiple images sizes (thumbnail, body, etc.) for each source image in Hexo

License: MIT License

JavaScript 100.00%
hexo hexo-plugin image-processing

hexo-image-sizes's Introduction

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:

  • pattern: The regular expression describing which images you would like to process. If you don't specify a pattern, this will default to files with extension jpg, jpeg, and png. Note that the pattern needs to be a YAML js/regexp object, as shown in the example above. Just prefix your JavaScript regex with !!js/regexp to prevent YAML from trying to parse it.

  • profiles: This object describes the image sizes you would like to produce. Each key is the name of an image profile, which can contain the following properties:

    • width: The maximum width of images with this profile, in pixels.
    • height: The maximum height of images with this profile, in pixels
    • allowEnlargement: A boolean, true if images smaller than the profile size should be enlarged to the maximum dimensions. By default, this is false. Enlargement can cause quality degradation, so use accordingly.
    • autoRotate: Rotate images based on their EXIF data. True by default.

    If you want to preserve the aspect ratio of your images, just specify one of width and height, and the other will adjust automatically. Images are resized using bicubic interpolation.

  • defaultProfile: The name of a profile specified in profiles that should be the default when an embedded image tag doesn't specify a profile (see below).

  • link: True if the image should be wrapped in a link to its source file. This property can also be specified in the embed tag, in which case the setting in the embed tag will take precedence.

  • linkProfile: The profile of the image to which to link. If linkProfile is omitted, the link will go to the original image. This property can also be specified in the embed tag, in which case the setting in the embed tag will take precedence.

  • useAltForTitle: Set to true to use image alt attributes as their title as well.

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):

  • src: The source path of the image you want to include. This is the same path you would use with regular Markdown images in Hexo, and it depends on how you've configured Hexo to treat paths for your site.
  • alt: The alt-text for the image. If you leave this key out, then no "alt" property will be added to the image tag.
  • profile: The name of the image profile you'd like to use. This name should match one you've configured in your sitewide _config.yml as described above. If you leave this key out, or the name is invalid, the image will use the default profile specified in _config.yml. If you don't have a default profile, the image will be the unaltered original size.
  • link: True if the image should be wrapped in a link to its source file. If specified here, overrides the setting in _config.yml.
  • linkProfile: The profile of the image to which to link. If linkProfile is omitted, the link will go to the original image. If specified here, overrides the setting in _config.yml.

Regenerate your site

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

hexo-image-sizes's People

Contributors

dantler avatar foolip avatar ottobonn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hexo-image-sizes's Issues

Support exclusion of source image asset

Feature request

It would be very useful if there were an option to exclude the image asset file referenced inside the {% imsize %} tag from being copied into the public/ directory. The benefit is disk space savings.

Syntactically I could imagine this being added as an option inside the {% imsize %} tag.

Background info

As mentioned in my previous PR, I'm using this plugin to generate photo albums on my personal webpage. My source images are full res (~10MB each), but when I share them on my webpage they are downsampled by this plugin (~2MB each). For most of my images, I do not wish to serve the original source asset. Currently my /public directory is 1GB when it should be < 200MB. This makes syncing cumbersome and needlessly wears out my SSD.

the path of src have problem

I am newer in hexo and web code, I just install your package and write MarkDown like this

{% imsize %}
src: testpicture1.jpg
alt: Dell Precision 5510 repair
title: Cool beans!
profile: thumbnail
link: true
linkProfile: huge
{% endimsize %}

and here is my _config.yml

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

but I can't get my image and the request url is error like this

Request URL: http://localhost:4000/_posts/testpicture/thumbnail-testpicture1.jpg
Request Method: GET
Status Code: 404 Not Found

Here is the url should be

Request URL: http://localhost:4000/2018/12/20/testpicture/testpicture2.jpg
Request Method: GET
Status Code: 200 OK

If you have any idea about this, please tell me:)

Resized images get deleted

Quick Summary

When i run hexo clean;hexo generate everything works. Subsequent runs of hexo generate delete the generated image files.

Details

I've configured the plugin as follows:

# 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
    sm-body:
      width: 400
    sq-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

Within my post I have the following code:

{% imsize %}
src: /images/20190119/laptop-specs.png
alt: Apple Macbook Pro specs
title: Apple Macbook Pro specs
profile: sm-body
link: false
{% endimsize %}

When I run hexo clean;hexo generate everything works and I get the following output:

INFO  Deleted database.
INFO  Deleted public folder.
INFO  Start processing
INFO  Files loaded in 309 ms
INFO  Generated: sitemap.xml
INFO  Generated: atom.xml
INFO  Generated: index.html
INFO  Generated: tags/index.html
INFO  Generated: categories/index.html
INFO  Generated: archives/index.html
INFO  Generated: img/bg-pattern.png
INFO  Generated: archives/2019/01/index.html
INFO  Generated: categories/Development/macOS/index.html
INFO  Generated: archives/2019/index.html
INFO  Generated: categories/Development/index.html
INFO  Generated: images/20190119/sm-body-laptop-specs.png
INFO  Generated: css/style.css
INFO  Generated: js/main.js
INFO  Generated: images/20190119/sm-body-desktop-specs.png
INFO  Generated: categories/Development/macOS/Software/Hardware/index.html
INFO  Generated: categories/Development/macOS/Software/index.html
INFO  Generated: images/20190119/laptop-specs.png
INFO  Generated: images/20190119/desktop-specs.png
INFO  Generated: 2019/01/20/hexo-serverless/index.html
INFO  Generated: 2019/01/19/development-environment/index.html
INFO  21 files generated in 387 ms

When I then run hexo generate the resized images are deleted and my post no longer contains the images. Here's the output:

INFO  Start processing
INFO  Files loaded in 167 ms
INFO  Deleted: images/20190119/sm-body-desktop-specs.png
INFO  Deleted: images/20190119/sm-body-laptop-specs.png
INFO  0 files generated in 218 ms

Can't get any images to generate

Hi, I don't seem to be getting any images to generate. I'm using post_asset_folder: true with the following config (copy pasted from the README except with linkProfile elided:

# 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

When I clean and generate I get (some unrelated lines removed):

giacomo@hamilton:~/share/Sites/internal_blog$ hexo clean && hexo generate
INFO  Deleted database.
INFO  Deleted public folder.
INFO  Start processing
INFO  Files loaded in 137 ms
<snipped out some lines>
INFO  Generated: 2020/04/18/First-Steps/index.html
INFO  Generated: 2020/04/18/First-Steps/driveway.jpg

I don't think this plugin is generating any images at all... I'm including the image like this:

{% imsize %}
src: driveway.jpg
alt: Large driveway
{% endimsize %}

As a sanity check, this works fine:

{% asset_img driveway.jpg 'Large driveway' %}

hexo: 4.2.0
hexo-cli: 3.1.0

Thanks for your work!

The output path is incorrect 404 (Not Found)

Maybe this has to do with me using Next https://theme-next.org/next-7-4-2-released/
The output folder structure is now like:

├── 2019
│   └── 10
│       └── 20
│           └── post
│               ├── blog.png
│               ├── huge-blog.png
│               ├── index.html
│               └── thumbnail-blog.png

but hexo-image-sizes's output path is _post/post/blog.png, which in result to a sad 404
I put some hack fix in by adding a date attribute + updated my local hexo-image-sizes in node_module to solve this issue. Not sure if I should make a pr for that since this is pretty hacky. Just bring it up so if anyone use next + hexo-image-sizes met the same problem can solve it this way. : ]

{% imsize %}
src: blog.png
alt: sample image
title: sample image
profile: thumbnail
link: true
linkProfile: huge
blogDate: 2019/10/26 .  <--- 
{% endimsize %}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.