Skip to content

Commit f1ac3a1

Browse files
authored
Merge pull request #1 from chaos-bodensee/use_guetzli
make guetzli optional
2 parents 866c4c7 + 9165e29 commit f1ac3a1

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The difference between this plugin and the lektor [thumbnail](https://www.getlek
1111
TL;DR: What does this plugin do?
1212
---------------------------------
1313
+ It will generate ``JPEG`` images in the sizes you configured of all images in your Lektor content.
14-
+ It try to optimize the images with the [guetzli](https://github.com/google/guetzli) JPEG encoder. *(You have to install the guetzli binary by yourself)*
14+
+ It can optionally optimize the images with the [guetzli](https://github.com/google/guetzli) JPEG encoder. *(You have to install the guetzli binary by yourself)*
1515

1616
Usage
1717
-------
@@ -29,7 +29,7 @@ To install the plugin, just add ``lektor-image-resize`` to your plugins from the
2929
lektor plugins add lektor-image-resize
3030
```
3131

32-
You **have to install** the [guetzli](https://github.com/google/guetzli) JPEG encoder.
32+
If you want to use the [guetzli](https://github.com/google/guetzli) JPEG encoder for image post-processing, you have to install it manually.
3333
```bash
3434
# example
3535
apt install guetzli
@@ -54,12 +54,16 @@ max_height = 800
5454

5555
[woowee]
5656
max_width = 2000
57+
use_guetzli = True
5758
```
5859

5960
Will take a file called `waffle.jpg` and create the files `waffle-small.jpg`,
6061
`waffle-medium.jpg` and `waffle-woowee.jpg`. All the files will be created, regardless
6162
of whether the original file is smaller, so you can link without worrying
6263
whether a file will exist or not. If the original file is smaller than the width
6364
you have specified, the file will only be copied, and will not be resized.
65+
66+
If you want to run guetzli at the generated output, set ``use_guetzli`` to ``True``.
67+
6468
The `max_width`/`max_height` parameters work like for the [Lektor
6569
thumbnail](https://www.getlektor.com/docs/api/db/record/thumbnail/) command.

lektor_image_resize.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ def process_image(
2222
height=None,
2323
mode=None,
2424
quality=None,
25+
use_guetzli=False,
2526
extra_params=None,
2627
):
2728
"""Build image from source image, optionally compressing and resizing.
2829
"source_image" is the absolute path of the source in the content directory,
2930
"dst_filename" is the absolute path of the target in the output directory.
3031
"""
32+
reporter.report_debug_info("processing image:", dst_filename)
3133
if width is None and height is None:
3234
raise ValueError("Must specify at least one of width or height.")
3335

@@ -56,8 +58,9 @@ def process_image(
5658

5759
reporter.report_debug_info("imagemagick cmd line", cmdline)
5860
portable_popen(cmdline).wait()
59-
reporter.report_debug_info("guetzli cmd line", cmd2line)
60-
portable_popen(cmd2line).wait()
61+
if use_guetzli:
62+
reporter.report_debug_info("guetzli cmd line", cmd2line)
63+
portable_popen(cmd2line).wait()
6164

6265
@buildprogram(Image)
6366
class ResizedImageBuildProgram(AttachmentBuildProgram):
@@ -85,14 +88,16 @@ def build_artifact(self, artifact):
8588
if not height:
8689
_, height = compute_dimensions(width, None, w, h)
8790

91+
use_guetzli = bool(conf.get("use_guetzli", "0"))
92+
93+
if not use_guetzli:
94+
use_guetzli = False
95+
8896
df = artifact.source_obj.url_path
8997
ext_pos = df.rfind(".")
90-
if df[ext_pos + 1 :] == 'svg':
91-
dst_filename = "%s-%s.png" % (df[:ext_pos], item)
92-
else:
93-
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])
98+
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])
9499

95-
def closure(dst_filename, source_img, width, height, resize_image=True):
100+
def closure(dst_filename, source_img, width, height, resize_image=True, ):
96101
# We need this closure, otherwise variables get updated and this
97102
# doesn't work at all.
98103
@ctx.sub_artifact(artifact_name=dst_filename, sources=[source_img])
@@ -107,7 +112,8 @@ def build_thumbnail_artifact(artifact):
107112
artifact.dst_filename,
108113
width,
109114
height,
110-
quality=85,
115+
quality=89,
116+
use_guetzli=use_guetzli,
111117
extra_params=[
112118
"-strip",
113119
"-interlace",

0 commit comments

Comments
 (0)