Debian Codegraph @

docs/content/en/functions/images/Padding.md

raw
1 ---
2 title: images.Padding
3 description: Returns an image filter that resizes the image canvas without resizing the image.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: images.filter
10 signatures: ['images.Padding V1 [V2] [V3] [V4] [COLOR]']
11 ---
12
13 The last argument is the canvas color, expressed as an RGB or RGBA [hexadecimal color][]. The default value is `ffffffff` (opaque white). The preceding arguments are the padding values, in pixels, using the CSS [shorthand property][] syntax. Negative padding values will crop the image.
14
15 ## Usage
16
17 Create the filter:
18
19 ```go-html-template
20 {{ $filter := images.Padding 20 40 "#976941" }}
21 ```
22
23 {{% include "/_common/functions/images/apply-image-filter.md" %}}
24
25 Combine with the [`Colors`][] method to create a border with one of the image's most dominant colors:
26
27 ```go-html-template
28 {{ with resources.Get "images/original.jpg" }}
29 {{ $filter := images.Padding 20 40 (index .Colors 2) }}
30 {{ with . | images.Filter $filter }}
31 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
32 {{ end }}
33 {{ end }}
34 ```
35
36 ## Example
37
38 {{< img
39 src="images/examples/zion-national-park.jpg"
40 alt="Zion National Park"
41 filter="Padding"
42 filterArgs="20,40,20,40,#976941"
43 example=true
44 >}}
45
46 ## Other recipes
47
48 This example resizes an image to 300px wide, converts it to the WebP format, adds 20px vertical padding and 50px horizontal padding, then sets the canvas color to dark green with 33% opacity.
49
50 In the example below, conversion to WebP is required to support transparency. AVIF, PNG, and WebP images have an alpha channel; JPEG and GIF do not.
51
52 ```go-html-template
53 {{ $img := resources.Get "images/a.jpg" }}
54 {{ $filters := slice
55 (images.Process "resize 300x webp")
56 (images.Padding 20 50 "#0705")
57 }}
58 {{ $img = $img.Filter $filters }}
59 ```
60
61 To add a 2px gray border to an image:
62
63 ```go-html-template
64 {{ $img = $img.Filter (images.Padding 2 "#777") }}
65 ```
66
67 [`Colors`]: /methods/resource/colors/
68 [hexadecimal color]: https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
69 [shorthand property]: https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties#edges_of_a_box