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.4KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, format, 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 that the format is chosen based on input image format.
  26. JPEG is chosen for JPEGs and other lossy formats, and PNG is chosen for PNGs and other lossless formats.
  27. - `quality` (_optional_): JPEG quality of the resized image, in percent. 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. The 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 pixel × 380 pixel 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, but only resize if the image is bigger than any of the specified dimensions.
  57. This mode is handy, if for example images are automatically shrunk to certain sizes in a shortcode for
  58. mobile optimization.
  59. Resizes the image such that the result fits within `width` and `height` while preserving the aspect ratio. This
  60. means that both width or height will be at max `width` and `height`, respectively, but possibly one of them
  61. smaller so as to preserve the aspect ratio.
  62. `resize_image(..., width=5000, height=5000, op="fit")`
  63. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=5000, height=5000, op="fit") }}
  64. `resize_image(..., width=150, height=150, op="fit")`
  65. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fit") }}
  66. ### **`"fill"`**
  67. This is the default operation. It takes the image's center part with the same aspect ratio as the `width` and
  68. `height` given and resizes that to `width` and `height`. This means that parts of the image that are outside
  69. of the resized aspect ratio are cropped away.
  70. `resize_image(..., width=150, height=150, op="fill")`
  71. {{ resize_image(path="documentation/content/image-processing/01-zola.png", width=150, height=150, op="fill") }}
  72. ## Using `resize_image` in markdown via shortcodes
  73. `resize_image` is a built-in Tera global function (see the [templates](@/documentation/templates/_index.md) chapter),
  74. but it can be used in Markdown using [shortcodes](@/documentation/content/shortcodes.md).
  75. The examples above were generated using a shortcode file named `resize_image.html` with this content:
  76. ```jinja2
  77. <img src="{{ resize_image(path=path, width=width, height=height, op=op) }}" />
  78. ```
  79. ## Creating picture galleries
  80. The `resize_image()` can be used multiple times and/or in loops. It is designed to handle this efficiently.
  81. This can be used along with `assets` [page metadata](@/documentation/templates/pages-sections.md) to create picture galleries.
  82. The `assets` variable holds paths to all assets in the directory of a page with resources
  83. (see [asset colocation](@/documentation/content/overview.md#asset-colocation)); if you have files other than images you
  84. will need to filter them out in the loop first like in the example below.
  85. This can be used in shortcodes. For example, we can create a very simple html-only clickable
  86. picture gallery with the following shortcode named `gallery.html`:
  87. ```jinja2
  88. {% for asset in page.assets %}
  89. {% if asset is matching("[.](jpg|png)$") %}
  90. <a href="{{ get_url(path=asset) }}">
  91. <img src="{{ resize_image(path=asset, width=240, height=180, op="fill") }}" />
  92. </a>
  93. &ensp;
  94. {% endif %}
  95. {% endfor %}
  96. ```
  97. As you can notice, we didn't specify an `op` argument, which means that it'll default to `"fill"`. Similarly,
  98. the format will default to `"auto"` (choosing PNG or JPEG as appropriate) and the JPEG quality will default to `75`.
  99. To call it from a Markdown file, simply do:
  100. ```jinja2
  101. {{/* gallery() */}}
  102. ```
  103. Here is the result:
  104. {{ gallery() }}
  105. <small>
  106. Image attribution: Public domain, except: _06-example.jpg_: Willi Heidelbach, _07-example.jpg_: Daniel Ullrich.
  107. </small>
  108. ## Get image size
  109. Sometimes when building a gallery it is useful to know the dimensions of each asset. You can get this information with
  110. [get_image_metadata](@/documentation/templates/overview.md#get-image-metadata).