Skip to content

Commit 99d177c

Browse files
committed
Improve image handling docs
1 parent c76c786 commit 99d177c

File tree

1 file changed

+115
-24
lines changed

1 file changed

+115
-24
lines changed

docs/classic-ui/images.md

Lines changed: 115 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,58 @@ html_meta:
55
"property=og:title": "Image resolving and scaling"
66
"keywords": "Plone, Classic UI, classic-ui, image, resize, scale"
77
---
8-
8+
****
99
(classic-ui-images-label)=
1010

11-
# Image resolving and scaling
11+
# Image handling
12+
13+
The default content types and behaviors are using a `plone.namedfile.NamedBlobImage` field for all images.
14+
15+
For this fields, image scaling and HTML tag creation is provided by the `plone.namedfile.scaling` module, specifically in the [`ImageScaling`](https://github.com/plone/plone.namedfile/blob/ecf33a2bc7a8c61888909bc383b3e08d80888e43/plone/namedfile/scaling.py#L350) view.
16+
17+
Given a Dexterity content type named `news-item1` with a `plone.namedfile.NamedBlobImage` field named `image`, we can use the following API's to access the image scales and manage scales.
18+
```{note}
19+
We will use the image object as context in the following examples.
20+
```
21+
22+
## Default scales
23+
24+
Plone allows you in the `/@@imaging-controlpanel` to configure which scales are available and what dimensions they should have. By default we have the following scales configured:
25+
26+
* large 768:768
27+
* preview 400:400
28+
* mini 200:200
29+
* thumb 128:128
30+
* tile 64:64
31+
* icon 32:32
32+
* listing 16:16
33+
34+
You can add or change scale as you like.
35+
36+
37+
## Image resolving by URI
38+
39+
Plone can resolve an image scale via URI in different ways.
40+
41+
### By field and scale name
42+
43+
Given our `news-item1` with the field `image`, we can get the name scale `thumb` as follows:
44+
45+
http://localhost:8080/Plone/news-item1/@@images/image/thumb
46+
47+
To get the original image, you can leave out the scale:
48+
49+
http://localhost:8080/Plone/news-item1/@@images/image
50+
51+
52+
53+
### By cacheable scale UID name
54+
55+
When an image scale was created, it will be cached under the name `UID.jpeg` in the object annotations and can be resolved by it as follows:
1256

13-
Image scaling is done in `plone.namedfile.scaling`, specifically in the `ImageScaling` view.
57+
http://localhost:8080/Plone/news-item1/@@images/3d182f34-8773-4f20-a79d-8774c3151b7e.jpeg
58+
59+
This very useful for caching URL's in Varnish or the Browser. In case the scale definitions have changed, they will be saved again under a different UID, so that the URL will change and enforce the Browser or a cache proxy like Varnish to fetch it again.
1460

1561

1662
(classic-ui-images-image-tag-label)=
@@ -23,7 +69,7 @@ To get an HTML tag for a scaled image, you can use the `ImageScaling` view as fo
2369
from plone import api
2470

2571
scale_util = api.content.get_view("images", context, request)
26-
tag = scale_util.tag("leadimage", scale="mini")
72+
tag = scale_util.tag("image", scale="mini")
2773
```
2874

2975
To get a specific image size:
@@ -32,7 +78,7 @@ To get a specific image size:
3278
from plone import api
3379

3480
scale_util = api.content.get_view("images", context, request)
35-
tag = scale_util.tag("leadimage", width="600", height="200")
81+
tag = scale_util.tag("image", width="600", height="200")
3682
```
3783

3884
The complete list of arguments with their default values is shown in the following example.
@@ -61,8 +107,9 @@ To get the scaling information only without creating an HTML tag, you can use th
61107
from plone import api
62108

63109
scale_util = api.content.get_view("images", context, request)
64-
# on the following line "leadimage" is the field name. The default Image content types field name is "image".
65-
image_scale = scale_util.scale("leadimage", scale="mini")
110+
# on the following line "image" is the field name.
111+
# The default Image content types field name is "image".
112+
image_scale = scale_util.scale("image", scale="mini")
66113
print(image_scale.url)
67114
print(image_scale.width)
68115
print(image_scale.height)
@@ -71,7 +118,7 @@ print(image_scale.height)
71118
This will produce the following output:
72119

73120
```console
74-
http://localhost:8080/Plone/enl/@@images/3d182f34-8773-4f20-a79d-8774c3151b7e.jpeg
121+
http://localhost:8080/Plone/news-item1/@@images/3d182f34-8773-4f20-a79d-8774c3151b7e.jpeg
75122
200
76123
110
77124
```
@@ -103,34 +150,78 @@ Instead of using the configured named scales you can also get an HTML tag with a
103150
from plone import api
104151

105152
scale_util = api.content.get_view("images", context, request)
106-
tag = scale_util.scale("leadimage", width="600", height="200")
153+
tag = scale_util.scale("image", width="600", height="200")
154+
```
155+
156+
### Using image_scale in templates
157+
158+
You could use the URL-variant from above, but that would be an an uncached version. To create a cached scale in a page template you can do the following:
159+
160+
```xml
161+
<div tal:define="scale_view context/@@images;
162+
image_scale python: scale_view.scale('image', 'mini')">
163+
<img
164+
src="${python: image_scale.url}"
165+
width="${python: image_scale.width"
166+
height="${python: image_scale.height}"
167+
>
168+
</div>
169+
```
170+
171+
or you can just get the HTML tag back and replace the current tag with it:
172+
173+
```xml
174+
<div tal:define="scale_view context/@@images">
175+
<img tal:replace="structured python: scale_view.tag('image', 'mini')">
176+
</div>
177+
```
178+
179+
You can also provide the following keyword arguments to set title, alt, css_class for the generated tag:
180+
181+
```xml
182+
<div tal:define="scale_view context/@@images">
183+
<img tal:replace="structured python: scale_view.tag('banner', 'mini', title='The Banner', alt='Alternative text', css_class='banner')">
184+
</div>
185+
```
186+
187+
### Get image_scale by cached UID name
188+
189+
If you only have the cached image name from an URL and need to get the image scale, unfortunately you can't use restrictedTraverse(), as this will not be able to resolve the scale. But you can use this workaround, by calling the `publishTraverse` method in `ImageScaling` directly:
190+
191+
```python
192+
import re
193+
from plone import api
194+
195+
uri = "http://localhost:8080/Plone/news-item1/@@images/3d182f34-8773-4f20-a79d-8774c3151b7e.jpeg"
196+
image_url = re.compile(r"(.*@@images)\/([a-zA-Z0-9.-]*)\/?([a-zA-Z]*)")
197+
198+
url_match = image_url.match(uri)
199+
groups = url_match.groups()
200+
# ("http://localhost:8080/Plone/news-item1", "3d182f34-8773-4f20-a79d-8774c3151b7e.jpeg")
201+
scale_util = api.content.get_view("images", context, request)
202+
image_scale = scaling_util.publishTraverse(context.REQUEST, groups[1])
107203
```
108204

109205

110206
(classic-ui-images-scaling-direction-deprecated-in-favor-of-label)=
111207

112-
## Scaling `direction` deprecated in favor of `mode`
208+
## Scaling `direction`
113209

114-
The actual scaling is done in `plone.scale`.
115-
In Plone 6 in `plone.scale`, the `direction` argument is deprecated in favor of `mode`.
116-
The values should be converted as follows:
210+
The default direction is `thumbnail`.
117211

118-
direction values | mode values
119-
-----------------|------------
120-
scale-crop-to-fit | contain
121-
down | contain
122-
scale-crop-to-fill | cover
123-
up | cover
124-
keep | scale
125-
thumbnail | scale
212+
Other options are:
126213

127-
For now `plone.namedfile` still expects the `direction` argument with the old values.
214+
* scale-crop-to-fit
215+
* down
216+
* scale-crop-to-fill
217+
* up
218+
* keep
219+
* thumbnail
128220

129221

130222
(classic-ui-images-permissions-label)=
131223

132224
## Permissions
133225

134226
The `ImageScaling` view explicitly checks the permissions of the current user.
135-
To access image scales which are normally not accessible to the current user override the `validate_access` method in `plone.namedfile.scaling.ImageScale`.
136-
In `Products.EasyNewsletter` you can find an example of that.
227+
To access image scales which are normally not accessible to the current user, override the `validate_access` method in `plone.namedfile.scaling.ImageScale`.

0 commit comments

Comments
 (0)