You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 6.0KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. +++
  2. title = "Image processing"
  3. weight = 120
  4. +++
  5. Zola provides support for automatic image resizing through the built-in function `resize_image`,
  6. which is available in template code as well as in shortcodes.
  7. The function usage is as follows:
  8. ```jinja2
  9. resize_image(path, width, height, op, quality)
  10. ```
  11. ### Arguments
  12. - `path`: The path to the source image relative to the `content` directory in the [directory structure](@/documentation/getting-started/directory-structure.md).
  13. - `width` and `height`: The dimensions in pixels of the resized image. Usage depends on the `op` argument.
  14. - `op` (_optional_): Resize operation. This can be one of:
  15. - `"scale"`
  16. - `"fit_width"`
  17. - `"fit_height"`
  18. - `"fit"`
  19. - `"fill"`
  20. What each of these does is explained below. The default is `"fill"`.
  21. - `format` (_optional_): Encoding format of the resized image. May be one of:
  22. - `"auto"`
  23. - `"jpg"`
  24. - `"png"`
  25. The default is `"auto"`, this means the format is chosen based on input image format.
  26. JPEG is chosen for JPEGs and other lossy formats, while PNG is chosen for PNGs and other lossless formats.
  27. - `quality` (_optional_): JPEG quality of the resized image, in percents. Only used when encoding JPEGs, default value is `75`.
  28. ### Image processing and return value
  29. Zola performs image processing during the build process and places the resized images in a subdirectory in the static files directory:
  30. ```
  31. static/processed_images/
  32. ```
  33. Filename of each resized image is a hash of the function arguments,
  34. which means that once an image is resized in a certain way, it will be stored in the above directory and will not
  35. need to be resized again during subsequent builds (unless the image itself, the dimensions, or other arguments are changed).
  36. Therefore, if you have a large number of images, they will only need to be resized once.
  37. The function returns a full URL to the resized image.
  38. ## Resize operations
  39. The source for all examples is this 300 × 380 pixels image:
  40. ![zola](01-zola.png)
  41. ### **`"scale"`**
  42. Simply scales the image to the specified dimensions (`width` & `height`) irrespective of the aspect ratio.
  43. `resize_image(..., width=150, height=150, op="scale")`
  44. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="scale") }}
  45. ### **`"fit_width"`**
  46. Resizes the image such that the resulting width is `width` and height is whatever will preserve the aspect ratio.
  47. The `height` argument is not needed.
  48. `resize_image(..., width=100, op="fit_width")`
  49. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=100, height=0, op="fit_width") }}
  50. ### **`"fit_height"`**
  51. Resizes the image such that the resulting height is `height` and width is whatever will preserve the aspect ratio.
  52. The `width` argument is not needed.
  53. `resize_image(..., height=150, op="fit_height")`
  54. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=0, height=150, op="fit_height") }}
  55. ### **`"fit"`**
  56. Like `"fit_width"` and `"fit_height"` combined.
  57. Resizes the image such that the result fits within `width` and `height` preserving aspect ratio. This means that both width or height
  58. will be at max `width` and `height`, respectively, but possibly one of them smaller so as to preserve the aspect ratio.
  59. `resize_image(..., width=150, height=150, op="fit")`
  60. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fit") }}
  61. ### **`"fill"`**
  62. This is the default operation. It takes the image's center part with the same aspect ratio as the `width` & `height` given and resizes that
  63. to `width` & `height`. This means that parts of the image that are outsize of the resized aspect ratio are cropped away.
  64. `resize_image(..., width=150, height=150, op="fill")`
  65. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fill") }}
  66. ## Using `resize_image` in markdown via shortcodes
  67. `resize_image` is a built-in Tera global function (see the [Templates](@/documentation/templates/_index.md) chapter),
  68. but it can be used in markdown, too, using [Shortcodes](@/documentation/content/shortcodes.md).
  69. The examples above were generated using a shortcode file named `resize_image.html` with this content:
  70. ```jinja2
  71. <img src="{{ resize_image(path=path, width=width, height=height, op=op) }}" />
  72. ```
  73. ## Creating picture galleries
  74. The `resize_image()` can be used multiple times and/or in loops. It is designed to handle this efficiently.
  75. This can be used along with `assets` [page metadata](@/documentation/templates/pages-sections.md) to create picture galleries.
  76. The `assets` variable holds paths to all assets in the directory of a page with resources
  77. (see [assets colocation](@/documentation/content/overview.md#assets-colocation)): if you have files other than images you
  78. will need to filter them out in the loop first like in the example below.
  79. This can be used in shortcodes. For example, we can create a very simple html-only clickable
  80. picture gallery with the following shortcode named `gallery.html`:
  81. ```jinja2
  82. {% for asset in page.assets %}
  83. {% if asset is matching("[.](jpg|png)$") %}
  84. <a href="{{ get_url(path=asset) }}">
  85. <img src="{{ resize_image(path=asset, width=240, height=180, op="fill") }}" />
  86. </a>
  87. &ensp;
  88. {% endif %}
  89. {% endfor %}
  90. ```
  91. As you can notice, we didn't specify an `op` argument, which means it'll default to `"fill"`. Similarly, the format will default to
  92. `"auto"` (choosing PNG or JPEG as appropriate) and the JPEG quality will default to `75`.
  93. To call it from a markdown file, simply do:
  94. ```jinja2
  95. {{/* gallery() */}}
  96. ```
  97. Here is the result:
  98. {{ gallery() }}
  99. <small>
  100. Image attribution: Public domain, except: _06-example.jpg_: Willi Heidelbach, _07-example.jpg_: Daniel Ullrich.
  101. </small>
  102. ## Get image size
  103. Sometimes when building a gallery it is useful to know the dimensions of each asset. You can get this information with
  104. [get_image_metadata](./documentation/templates/overview.md#get-image-metadata)