hugo/content/en/content-management/image-processing/index.md
Bjørn Erik Pedersen 766085c2dc Squashed 'docs/' changes from a4fa0d1d6..81847981f
81847981f Add notes for installing "extended" Sass/SCSS version
02da3bb58 Include usage of disqus internal template
9b53380c4 Update sitemap-template.md
f3417dd52 Update sitemap-template.md
aae33d9a8 Update sitemap-template.md
834edc205 Release 0.45.1
fb93ab905 Merge branch 'temp451'
79f350906 releaser: Prepare repository for 0.46-DEV
25cd2352c releaser: Add release notes to /docs for release of 0.45.1
e60377c71 releaser: Bump versions for release of 0.45.1
873f6dce2 Update features.md
ecc116642 Update link for Azure
cb88c3185 Added .Site.Home to Site Variiables (#541)
975a88791 Document includePaths
56c4e332f Release Hugo 0.45
c22b25210 Merge branch 'temp45'
0ffad3ada releaser: Prepare repository for 0.46-DEV
08d494cc3 releaser: Add release notes to /docs for release of 0.45
ad546ba45 releaser: Bump versions for release of 0.45
971c46bb2 Fixed "Sratch" to become "Scratch" on line 44
a28255bf3 Clarify the meaning of .Pages vs .Site.Pages
127aeee09 docs: Update ref, relref, GetPage docs
350d674ec resources.Concat doesn't have an alias
1fd1219b0 docs: Document refLinksErrorLevel and refLinksNotFoundURL
af2252ff6 Merge commit 'a3535c8486b2ce762b1a8a9c30b03985c3e02cee'
a11486805 Merge commit 'b6b37a1f00f808f3c0d2715f65ca2d3091f36495'
1c8896cb1 Fix addkit link to account for i18n
468aef3fc releaser: Prepare repository for 0.45-DEV
c6f4b97a2 releaser: Add release notes to /docs for release of 0.44
d3985afb7 releaser: Bump versions for release of 0.44
2c59a330c Adjust release notes
83966769a releaser: Prepare repository for 0.44-DEV
e539613f7 releaser: Add release notes to /docs for release of 0.43
2bf648944 releaser: Bump versions for release of 0.43
d9d6e4bf7 Fix typos
d6798afda Merge commit '98293eaa1570b5aff4452021c8b6d6c8560b3f06'
37cc52261 Add a newScratch template func
1f7f09613 Merge branch 'release-0.42.2'
857b0b26a releaser: Prepare repository for 0.43-DEV

git-subtree-dir: docs
git-subtree-split: 81847981f1f2cb1ebc83d42d275a2afb2bb22df1
2018-08-01 10:01:05 +02:00

195 lines
5.8 KiB
Markdown

---
title: "Image Processing"
description: "Image Page resources can be resized and cropped."
date: 2018-01-24T13:10:00-05:00
lastmod: 2018-01-26T15:59:07-05:00
linktitle: "Image Processing"
categories: ["content management"]
keywords: [bundle,content,resources,images]
weight: 4004
draft: false
toc: true
menu:
docs:
parent: "content-management"
weight: 32
---
## The Image Page Resource
The `image` is a [Page Resource]({{< relref "/content-management/page-resources" >}}), and the processing methods listed below does not work on images inside your `/static` folder.
To get all images in a [Page Bundle]({{< relref "/content-management/organization#page-bundles" >}}):
```go-html-template
{{ with .Resources.ByType "image" }}
{{ end }}
```
## Image Processing Methods
The `image` resource implements the methods `Resize`, `Fit` and `Fill`, each returning the transformed image using the specified dimensions and processing options.
Resize
: Resizes the image to the specified width and height.
```go
// Resize to a width of 600px and preserve ratio
{{ $image := $resource.Resize "600x" }}
// Resize to a height of 400px and preserve ratio
{{ $image := $resource.Resize "x400" }}
// Resize to a width 600px and a height of 400px
{{ $image := $resource.Resize "600x400" }}
```
Fit
: Scale down the image to fit the given dimensions while maintaining aspect ratio. Both height and width are required.
```go
{{ $image := $resource.Fit "600x400" }}
```
Fill
: Resize and crop the image to match the given dimensions. Both height and width are required.
```go
{{ $image := $resource.Fill "600x400" }}
```
{{% note %}}
Image operations in Hugo currently **do not preserve EXIF data** as this is not supported by Go's [image package](https://github.com/golang/go/search?q=exif&type=Issues&utf8=%E2%9C%93). This will be improved on in the future.
{{% /note %}}
## Image Processing Options
In addition to the dimensions (e.g. `600x400`), Hugo supports a set of additional image options.
JPEG Quality
: Only relevant for JPEG images, values 1 to 100 inclusive, higher is better. Default is 75.
```go
{{ $image.Resize "600x q50" }}
```
Rotate
: Rotates an image by the given angle counter-clockwise. The rotation will be performed first to get the dimensions correct. The main use of this is to be able to manually correct for [EXIF orientation](https://github.com/golang/go/issues/4341) of JPEG images.
```go
{{ $image.Resize "600x r90" }}
```
Anchor
: Only relevant for the `Fill` method. This is useful for thumbnail generation where the main motive is located in, say, the left corner.
Valid are `Center`, `TopLeft`, `Top`, `TopRight`, `Left`, `Right`, `BottomLeft`, `Bottom`, `BottomRight`.
```go
{{ $image.Fill "300x200 BottomLeft" }}
```
Resample Filter
: Filter used in resizing. Default is `Box`, a simple and fast resampling filter appropriate for downscaling.
Examples are: `Box`, `NearestNeighbor`, `Linear`, `Gaussian`.
See https://github.com/disintegration/imaging for more. If you want to trade quality for faster processing, this may be a option to test.
```go
{{ $image.Resize "600x400 Gaussian" }}
```
## Image Processing Examples
_The photo of the sunset used in the examples below is Copyright [Bjørn Erik Pedersen](https://commons.wikimedia.org/wiki/User:Bep) (Creative Commons Attribution-Share Alike 4.0 International license)_
{{< imgproc sunset Resize "300x" />}}
{{< imgproc sunset Fill "90x120 left" />}}
{{< imgproc sunset Fill "90x120 right" />}}
{{< imgproc sunset Fit "90x90" />}}
{{< imgproc sunset Resize "300x q10" />}}
This is the shortcode used in the examples above:
{{< code file="layouts/shortcodes/imgproc.html" >}}
{{< readfile file="layouts/shortcodes/imgproc.html" >}}
{{< /code >}}
And it is used like this:
```go-html-template
{{</* imgproc sunset Resize "300x" /*/>}}
```
{{% note %}}
**Tip:** Note the self-closing shortcode syntax above. The `imgproc` shortcode can be called both with and without **inner content**.
{{% /note %}}
## Image Processing Config
You can configure an `imaging` section in `config.toml` with default image processing options:
```toml
[imaging]
# Default resample filter used for resizing. Default is Box,
# a simple and fast averaging filter appropriate for downscaling.
# See https://github.com/disintegration/imaging
resampleFilter = "box"
# Defatult JPEG quality setting. Default is 75.
quality = 75
# Anchor used when cropping pictures.
# Default is "smart" which does Smart Cropping, using https://github.com/muesli/smartcrop
# Smart Cropping is content aware and tries to find the best crop for each image.
# Valid values are Smart, Center, TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom, BottomRight
anchor = "smart"
```
All of the above settings can also be set per image procecssing.
## Smart Cropping of Images
By default, Hugo will use the [Smartcrop](https://github.com/muesli/smartcrop), a library created by [muesli](https://github.com/muesli), when cropping images with `.Fill`. You can set the anchor point manually, but in most cases the smart option will make a good choice. And we will work with the library author to improve this in the future.
An example using the sunset image from above:
{{< imgproc sunset Fill "200x200 smart" />}}
## Image Processing Performance Consideration
Processed images are stored below `<project-dir>/resources` (can be set with `resourceDir` config setting). This folder is deliberately placed in the project, as it is recommended to check these into source control as part of the project. These images are not "Hugo fast" to generate, but once generated they can be reused.
If you change your image settings (e.g. size), remove or rename images etc., you will end up with unused images taking up space and cluttering your project.
To clean up, run:
```bash
hugo --gc
```
{{% note %}}
**GC** is short for **Garbage Collection**.
{{% /note %}}