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"
@@ -89,7 +91,7 @@ PICTURES = {
89
91
},
90
92
" GRID_COLUMNS" : 12 ,
91
93
" CONTAINER_WIDTH" : 1200 ,
92
- " FILE_TYPES" : [" WEBP " ],
94
+ " FILE_TYPES" : [" AVIF " ],
93
95
" PIXEL_DENSITIES" : [1 , 2 ],
94
96
" USE_PLACEHOLDERS" : True ,
95
97
" QUEUE_NAME" : " pictures" ,
@@ -128,19 +130,16 @@ 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
136
+ Sadly, AVIF support [ requires a little more setup] [ libavif-install ] .
134
137
135
138
``` html
136
139
{% load pictures %}
137
- <img src =" {% img_url profile.picture ratio=" 3 /2 " file_type=" webp " width=800 %}" alt =" profile picture" >
140
+ <img src =" {% img_url profile.picture ratio=' 3/2' file_type=' webp' width=800 %}" alt =" profile picture" >
138
141
```
139
142
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
143
## Config
145
144
146
145
### Aspect ratios
@@ -172,17 +171,17 @@ class Profile(models.Model):
172
171
If you don't specify an aspect ratio or None in your template, the image will be
173
172
served with the original aspect ratio of the file.
174
173
175
- You may only use aspect ratios in templates, that have been defined on the model.
174
+ You may only use aspect ratios in templates that have been defined on the model.
176
175
The model ` aspect_ratios ` will default to ` [None] ` , if other value is provided.
177
176
178
177
### Breakpoints
179
178
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.
179
+ You may define your own breakpoints they should be identical to the ones used
180
+ in your CSS library. This can be achieved by overriding the ` PICTURES["BREAKPOINTS"] ` setting.
182
181
183
182
### Grid columns
184
183
185
- Grids are so common in web design, that they even made it into CSS.
184
+ Grids are so common in web design that they even made it into CSS.
186
185
We default to 12 columns, but you can override this setting, via the
187
186
` PICTURES["GRID_COLUMNS"] ` setting.
188
187
@@ -196,13 +195,14 @@ You may also set it to `None`, should you not use a container.
196
195
197
196
### File types
198
197
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 ) .
198
+ [ AVIF] ( https://caniuse.com/avif ) ([ WebP] ( https://caniuse.com/webp ) 's successor)
199
+ is the best and most efficient image format available today. It is part of
200
+ Baseline 2024 and is supported by all major browsers. Additionally, most modern
201
+ devices will have hardware acceleration for AVIF decoding. This will not only
202
+ reduce network IO but speed up page rendering.
203
203
204
- If you are serving IE11 use add ` JPEG ` to the list. Beware though, that this may
205
- drastically increase you storage needs.
204
+ Should you still serve IE11, use add ` JPEG ` to the list. But, beware, this may
205
+ drastically increase your storage needs.
206
206
207
207
### Pixel densities
208
208
@@ -224,13 +224,11 @@ processor, should you need to do some custom processing.
224
224
## Migrations
225
225
226
226
Django doesn't support file field migrations, but we do.
227
- You can simply auto create the migration and replace Django's
227
+ You can auto create the migration and replace Django's
228
228
` AlterField ` operation with ` AlterPictureField ` . That's it.
229
229
230
230
You can follow [ the example] [ migration ] in our test app, to see how it works.
231
231
232
- [ migration ] : tests/testapp/migrations/0002_alter_profile_picture.py
233
-
234
232
## Contrib
235
233
236
234
### Django Rest Framework ([ DRF] )
@@ -254,10 +252,10 @@ from rest_framework import serializers
254
252
from pictures.contrib.rest_framework import PictureField
255
253
256
254
class PictureSerializer (serializers .Serializer ):
257
- picture = PictureField(aspect_ratios = [" 16/9" ], file_types = [" WEBP " ])
255
+ picture = PictureField(aspect_ratios = [" 16/9" ], file_types = [" AVIF " ])
258
256
```
259
257
260
- You also may provide optional GET parameters to the serializer,
258
+ You also may provide optional GET parameters to the serializer
261
259
to specify the aspect ratio and breakpoints you want to include in the response.
262
260
The parameters are prefixed with the ` fieldname_ `
263
261
to avoid conflicts with other fields.
@@ -303,7 +301,7 @@ Should you use a CDN, or some other external image processing service, you can
303
301
set this up in two simple steps:
304
302
305
303
1 . Override ` PICTURES["PROCESSOR"] ` to disable the default processing.
306
- 2 . Override ` PICTURES["PICTURE_CLASS"] ` implement any custom behavior.
304
+ 1 . Override ` PICTURES["PICTURE_CLASS"] ` implement any custom behavior.
307
305
308
306
``` python
309
307
# settings.py
@@ -317,10 +315,11 @@ The `MyPicture`class should implement the url property, which returns the URL
317
315
of the image. You may use the ` Picture ` class as a base class.
318
316
319
317
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
318
+
319
+ - ` parent_name ` - name of the source file uploaded to the ` PictureField `
320
+ - ` aspect_ratio ` - aspect ratio of the output image
321
+ - ` width ` - width of the output image
322
+ - ` file_type ` - format of the output image
324
323
325
324
``` python
326
325
# path/to.py
@@ -337,7 +336,12 @@ class MyPicture(Picture):
337
336
)
338
337
```
339
338
340
- [ drf ] : https://www.django-rest-framework.org/
339
+ [ caniemail-srcset ] : https://www.caniemail.com/features/html-srcset/
340
+ [ caniemail-webp ] : https://www.caniemail.com/features/image-webp/
341
+ [ caniuse-picture ] : https://caniuse.com/picture
341
342
[ celery ] : https://docs.celeryproject.org/en/stable/
342
- [ dramatiq ] : https://dramatiq.io/
343
343
[ django-rq ] : https://github.com/rq/django-rq
344
+ [ dramatiq ] : https://dramatiq.io/
345
+ [ drf ] : https://www.django-rest-framework.org/
346
+ [ libavif-install ] : https://pillow.readthedocs.io/en/latest/installation/building-from-source.html#external-libraries
347
+ [ migration ] : tests/testapp/migrations/0002_alter_profile_picture.py
0 commit comments