This article offers some use cases for showing how to process images via URL query string and fragment, such as resize, crop, fit, fill and align images.

Module PathVersion
github.com/razonyang/hugo-mod-imagesVersion

URL Concepts

The URL syntax as follows.

1URL = scheme ":" ["//" authority] path ["?" query] ["#" fragment]

The query string preceded by a question mark (?), and the fragment preceded by a hash (#).

This module uses the query string to process images, and use fragment to align images.

Example For example, ![Example](featured.jpeg?crop=300x120&brightness=-10#float-end), which crop the image in size 300x120, change the brightness as -10 and floating the image to the end (right).

Image Types

TypeDescriptionExample
Site Image ResourceThe images located in the assets directory./images/foo.png
Page Image ResourceThe images located in the page’s directory.bar.png
Static ImageThe images located in the static directory.images/fizz.png
External ImageThe external image.https://example.com/images/buzz.png

The image resources begin with a leading slash / will be treated as a site image resource.

Most of the processing methods work only on image resources, except for alignment and resizing.

Aligning Images

We can easily align images by adding URL fragments. Such as #center, #float-start and #float-end represents align center, float start and float end respectively.

Aligning Images to Center

Adding the #center fragment for aligning images to the center.

For example: ![Center](featured.jpeg#center).

Center

Floating Images to Start (Left)

Float Start Adding the #float-start fragment for floating images to the start, we have to wrap it inside bootstrap/clearfix shortcode to avoid breaking the layout. Please note that use %% instead of <> to render content as Markdown fully.

For example.

1{{% bootstrap/clearfix %}}
2![Float Start](featured.jpeg#float-start) TEXTS AROUNDS THE IMAGE.
3{{% /bootstrap/clearfix %}}

Floating Images to End (Right)

Float End Similarly, we can also float images to the end by adding the #float-end fragment.

For example.

1{{% bootstrap/clearfix %}}
2![Float End](featured.jpeg#float-end) TEXTS AROUNDS THE IMAGE.
3{{% /bootstrap/clearfix %}}

Resizing Images

We use the URL query of image for resizing images. For example:

Specify the Width and Preserve Ratio

1![Resize](featured.jpeg?width=300px)

Resize

Specify the Height and Preserve Ratio

1![Resize](featured.jpeg?height=180px)

Resize

Specify the Width and Height

1![Resize](featured.jpeg?width=300px&height=180px)

Resize

Cropping Images

Crop an image to match the given dimensions without resizing. You must provide both width and height. Use the anchor1 option to change the crop box anchor point.

1![Crop Image](featured.jpeg?crop=[width]x[height],[anchor])
  • The size [width]x[height] is required.
  • [anchor] is optional.
Examples
Crop ImageCrop ImageCrop Image
Crop ImageCrop ImageCrop Image
Crop ImageCrop ImageCrop Image

Fill Images

Crop and resize an image to match the given dimensions. You must provide both width and height. Use the anchor1 option to change the crop box anchor point.

1![Fill Image](featured.jpeg?fill=[width]x[height],[anchor])
  • The size [width]x[height] is required.
  • [anchor] is optional.
Examples
Fill ImageFill ImageFill Image
Fill ImageFill ImageFill Image
Fill ImageFill ImageFill Image

Fitting Images

Downscale an image to fit the given dimensions while maintaining aspect ratio. You must provide both width and height.

1![Fit Image](featured.jpeg?fit=[width]x[height])

Fit Image

Image Filters

Brightness

The brightness must be in range (-100, 100).

1![Image Brightness](featured.jpeg?brightness=-30)

Image Brightness

ColorBalance

ColorBalance creates a filter that changes the color balance of an image. The percentage parameters for each color channel (red, green, blue) must be in range (-100, 500).

1![Image ColorBalance](featured.jpeg?colorBalance=-50,50,150)

Image ColorBalance

Colorize

Colorize creates a filter that produces a colorized version of an image. The hue parameter is the angle on the color wheel, typically in range (0, 360). The saturation parameter must be in range (0, 100). The percentage parameter specifies the strength of the effect, it must be in range (0, 100).

1![Image Colorize](featured.jpeg?colorize=-100,50,150)

Image Colorize

Contrast

The contrast must be in range (-100, 100).

1![Image Contrast](featured.jpeg?contrast=50)

Image Contrast

Gamma

Gamma creates a filter that performs a gamma correction on an image. The gamma parameter must be positive. Gamma = 1 gives the original image. Gamma less than 1 darkens the image and gamma greater than 1 lightens it.

1![Image Gamma](featured.jpeg?gamma=2)

Image Gamma

GaussianBlur

Applies a gaussian blur to an image.

1![Image GaussianBlur](featured.jpeg?gaussianBlur=2)

Image GaussianBlur

Grayscale

Grayscale creates a filter that produces a grayscale version of an image.

1![Image Grayscale](featured.jpeg?grayscale)

Image Grayscale

Hue

Hue creates a filter that rotates the hue of an image. The hue angle shift is typically in range -180 to 180.

1![Image Hue](featured.jpeg?hue=90)

Image Hue

Invert

Invert creates a filter that negates the colors of an image.

1![Image Invert](featured.jpeg?invert)

Image Invert

Pixelate

Pixelate creates a filter that applies a pixelation effect to an image.

1![Image Pixelate](featured.jpeg?pixelate=8)

Image Pixelate

Saturation

Saturation creates a filter that changes the saturation of an image.

1![Image Saturation](featured.jpeg?saturation=100)

Image Saturation

Sepia

Sepia creates a filter that produces a sepia-toned version of an image.

1![Image Sepia](featured.jpeg?sepia=200)

Image Sepia

Sigmoid

Sigmoid creates a filter that changes the contrast of an image using a sigmoidal function and returns the adjusted image. It’s a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.

1![Image Sigmoid](featured.jpeg?sigmoid=2,5)

Image Sigmoid

UnsharpMask

UnsharpMask creates a filter that sharpens an image. The sigma parameter is used in a gaussian function and affects the radius of effect. Sigma must be positive. Sharpen radius roughly equals 3 * sigma. The amount parameter controls how much darker and how much lighter the edge borders become. Typically between 0.5 and 1.5. The threshold parameter controls the minimum brightness change that will be sharpened. Typically between 0 and 0.05.

1![Image UnsharpMask](featured.jpeg?unsharpMask=10,1,0.05)

Image UnsharpMask

Examples

Static Example

External Example

SVG Example


  1. When using the Crop or Fill method, the anchor determines the placement of the crop box. You may specify TopLeft, Top, TopRight, Left, Center, Right, BottomLeft, Bottom, BottomRight, or Smart. The default value is Smart↩︎ ↩︎