4
4
5
5
Responsive cross-browser image library using modern codes like AVIF & WebP.
6
6
7
- * responsive web images using the [ picture] ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture ) tag
8
- * native grid system support
9
- * serve files with or without a CDN
10
- * placeholders for local development
11
- * migration support
12
- * async image processing for [ Celery] , [ Dramatiq] or [ Django RQ] [ django-rq ]
13
- * [ DRF] support
7
+ - responsive web images using the [ picture] ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture ) tag
8
+ - native grid system support
9
+ - serve files with or without a CDN
10
+ - placeholders for local development
11
+ - migration support
12
+ - async image processing for [ Celery] , [ Dramatiq] or [ Django RQ] [ django-rq ]
13
+ - [ DRF] support
14
14
15
15
[ ![ PyPi Version] ( https://img.shields.io/pypi/v/django-pictures.svg )] ( https://pypi.python.org/pypi/django-pictures/ )
16
16
[ ![ Test Coverage] ( https://codecov.io/gh/codingjoe/django-pictures/branch/main/graph/badge.svg )] ( https://codecov.io/gh/codingjoe/django-pictures )
@@ -21,8 +21,9 @@ Responsive cross-browser image library using modern codes like AVIF & WebP.
21
21
Before you start, it can be a good idea to understand the fundamentals of
22
22
[ responsive images] ( https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images ) .
23
23
24
- Once you get a feeling how complicated things can get with all device types, you'll probably find
25
- a new appreciation for this package and are ready to adopt in you project :)
24
+ Once you get a feeling how complicated things can get with all device types,
25
+ you'll probably find a new appreciation for this package,
26
+ and are ready to adopt in your project :)
26
27
27
28
``` python
28
29
# models.py
@@ -41,12 +42,13 @@ class Profile(models.Model):
41
42
```
42
43
43
44
The keyword arguments ` m=6 l=4 ` define the columns the image should take up in
44
- a grid at a given breakpoint. So in this example, the image will take up
45
- 6 columns on medium screens and 4 columns on large screens. You can define your
46
- grid and breakpoints as you want, refer to the [ grid columns] ( #grid-columns ) and
45
+ a grid at a given breakpoint. So in this example, the image will take up
46
+ six columns on medium screens and four columns on large screens. You can define
47
+ your grid and breakpoints as you want, refer to the [ grid columns] ( #grid-columns ) and
47
48
[ breakpoints] ( #breakpoints ) sections.
48
49
49
50
The template above will render into:
51
+
50
52
``` html
51
53
<picture class =" my-picture" >
52
54
<source type =" image/webp"
@@ -128,19 +130,14 @@ Although the `picture`-tag is [adequate for most use-cases][caniuse-picture],
128
130
some remain, where a single ` img ` tag is necessary. Notably in email, where
129
131
[ most clients do support WebP] [ caniemail-webp ] but not [ srcset] [ caniemail-srcset ] .
130
132
The template tag ` img_url ` returns a single size image URL.
131
- In addition to the ratio you will need to define the ` file_type `
133
+ In addition to the ratio, you will need to define the ` file_type `
132
134
as well as the ` width ` (absolute width in pixels).
133
135
134
-
135
136
``` html
136
137
{% load pictures %}
137
- <img src =" {% img_url profile.picture ratio=" 3 /2 " file_type=" webp " width=800 %}" alt =" profile picture" >
138
+ <img src =" {% img_url profile.picture ratio=' 3/2' file_type=' webp' width=800 %}" alt =" profile picture" >
138
139
```
139
140
140
- [ caniuse-picture ] : https://caniuse.com/picture
141
- [ caniemail-webp ] : https://www.caniemail.com/features/image-webp/
142
- [ caniemail-srcset ] : https://www.caniemail.com/features/html-srcset/
143
-
144
141
## Config
145
142
146
143
### Aspect ratios
@@ -172,17 +169,17 @@ class Profile(models.Model):
172
169
If you don't specify an aspect ratio or None in your template, the image will be
173
170
served with the original aspect ratio of the file.
174
171
175
- You may only use aspect ratios in templates, that have been defined on the model.
172
+ You may only use aspect ratios in templates that have been defined on the model.
176
173
The model ` aspect_ratios ` will default to ` [None] ` , if other value is provided.
177
174
178
175
### Breakpoints
179
176
180
- You may define your own breakpoints, they should be identical to the ones used
181
- in your css library. Simply override the ` PICTURES["BREAKPOINTS"] ` setting.
177
+ You may define your own breakpoints they should be identical to the ones used
178
+ in your CSS library. This can be achieved by overriding the ` PICTURES["BREAKPOINTS"] ` setting.
182
179
183
180
### Grid columns
184
181
185
- Grids are so common in web design, that they even made it into CSS.
182
+ Grids are so common in web design that they even made it into CSS.
186
183
We default to 12 columns, but you can override this setting, via the
187
184
` PICTURES["GRID_COLUMNS"] ` setting.
188
185
@@ -196,13 +193,20 @@ You may also set it to `None`, should you not use a container.
196
193
197
194
### File types
198
195
199
- Unless you still services IE11 clients, you should be fine serving just
200
- [ WebP] ( https://caniuse.com/webp ) . Sadly, [ AVIF] ( https://caniuse.com/avif )
201
- (WebP's successor) is
202
- [ not yet supported by Pillow] ( https://github.com/python-pillow/Pillow/pull/5201 ) .
196
+ [ AVIF] ( https://caniuse.com/avif ) ([ WebP] ( https://caniuse.com/webp ) 's successor)
197
+ is the best and most efficient image format available today. It is part of
198
+ Baseline 2024 and is supported by all major browsers. Additionally, most modern
199
+ devices will have hardware acceleration for AVIF decoding. This will not only
200
+ reduce network IO but speed up page rendering.
201
+
202
+ > [ !NOTE]
203
+ > Pillow 11.2.1 shipped without AVIF binaries.
204
+ > You will need to [ install Pillow from source] [ libavif-install ] for AVIF support.
205
+ > This should be resolved in upcoming releases, once
206
+ > [ #8858 ] ( https://github.com/python-pillow/Pillow/pull/8858 ) has been merged.
203
207
204
- If you are serving IE11 use add ` JPEG ` to the list. Beware though, that this may
205
- drastically increase you storage needs.
208
+ Should you still serve IE11, use add ` JPEG ` to the list. But, beware, this may
209
+ drastically increase your storage needs.
206
210
207
211
### Pixel densities
208
212
@@ -224,13 +228,11 @@ processor, should you need to do some custom processing.
224
228
## Migrations
225
229
226
230
Django doesn't support file field migrations, but we do.
227
- You can simply auto create the migration and replace Django's
231
+ You can auto create the migration and replace Django's
228
232
` AlterField ` operation with ` AlterPictureField ` . That's it.
229
233
230
234
You can follow [ the example] [ migration ] in our test app, to see how it works.
231
235
232
- [ migration ] : tests/testapp/migrations/0002_alter_profile_picture.py
233
-
234
236
## Contrib
235
237
236
238
### Django Rest Framework ([ DRF] )
@@ -257,7 +259,7 @@ class PictureSerializer(serializers.Serializer):
257
259
picture = PictureField(aspect_ratios = [" 16/9" ], file_types = [" WEBP" ])
258
260
```
259
261
260
- You also may provide optional GET parameters to the serializer,
262
+ You also may provide optional GET parameters to the serializer
261
263
to specify the aspect ratio and breakpoints you want to include in the response.
262
264
The parameters are prefixed with the ` fieldname_ `
263
265
to avoid conflicts with other fields.
@@ -303,7 +305,7 @@ Should you use a CDN, or some other external image processing service, you can
303
305
set this up in two simple steps:
304
306
305
307
1 . Override ` PICTURES["PROCESSOR"] ` to disable the default processing.
306
- 2 . Override ` PICTURES["PICTURE_CLASS"] ` implement any custom behavior.
308
+ 1 . Override ` PICTURES["PICTURE_CLASS"] ` implement any custom behavior.
307
309
308
310
``` python
309
311
# settings.py
@@ -317,10 +319,11 @@ The `MyPicture`class should implement the url property, which returns the URL
317
319
of the image. You may use the ` Picture ` class as a base class.
318
320
319
321
Available attributes are:
320
- * ` parent_name ` - name of the source file uploaded to the ` PictureField `
321
- * ` aspect_ratio ` - aspect ratio of the output image
322
- * ` width ` - width of the output image
323
- * ` file_type ` - file type of the output image
322
+
323
+ - ` parent_name ` - name of the source file uploaded to the ` PictureField `
324
+ - ` aspect_ratio ` - aspect ratio of the output image
325
+ - ` width ` - width of the output image
326
+ - ` file_type ` - format of the output image
324
327
325
328
``` python
326
329
# path/to.py
@@ -337,7 +340,12 @@ class MyPicture(Picture):
337
340
)
338
341
```
339
342
340
- [ drf ] : https://www.django-rest-framework.org/
343
+ [ caniemail-srcset ] : https://www.caniemail.com/features/html-srcset/
344
+ [ caniemail-webp ] : https://www.caniemail.com/features/image-webp/
345
+ [ caniuse-picture ] : https://caniuse.com/picture
341
346
[ celery ] : https://docs.celeryproject.org/en/stable/
342
- [ dramatiq ] : https://dramatiq.io/
343
347
[ django-rq ] : https://github.com/rq/django-rq
348
+ [ dramatiq ] : https://dramatiq.io/
349
+ [ drf ] : https://www.django-rest-framework.org/
350
+ [ libavif-install ] : https://pillow.readthedocs.io/en/latest/installation/building-from-source.html#external-libraries
351
+ [ migration ] : tests/testapp/migrations/0002_alter_profile_picture.py
0 commit comments