Skip to content

Commit

Permalink
Merge pull request #1 from chaos-bodensee/use_guetzli
Browse files Browse the repository at this point in the history
make guetzli optional
  • Loading branch information
DO1JLR authored Aug 23, 2021
2 parents 866c4c7 + 9165e29 commit f1ac3a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The difference between this plugin and the lektor [thumbnail](https://www.getlek
TL;DR: What does this plugin do?
---------------------------------
+ It will generate ``JPEG`` images in the sizes you configured of all images in your Lektor content.
+ 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)*
+ 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)*

Usage
-------
Expand All @@ -29,7 +29,7 @@ To install the plugin, just add ``lektor-image-resize`` to your plugins from the
lektor plugins add lektor-image-resize
```

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

[woowee]
max_width = 2000
use_guetzli = True
```

Will take a file called `waffle.jpg` and create the files `waffle-small.jpg`,
`waffle-medium.jpg` and `waffle-woowee.jpg`. All the files will be created, regardless
of whether the original file is smaller, so you can link without worrying
whether a file will exist or not. If the original file is smaller than the width
you have specified, the file will only be copied, and will not be resized.

If you want to run guetzli at the generated output, set ``use_guetzli`` to ``True``.

The `max_width`/`max_height` parameters work like for the [Lektor
thumbnail](https://www.getlektor.com/docs/api/db/record/thumbnail/) command.
22 changes: 14 additions & 8 deletions lektor_image_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def process_image(
height=None,
mode=None,
quality=None,
use_guetzli=False,
extra_params=None,
):
"""Build image from source image, optionally compressing and resizing.
"source_image" is the absolute path of the source in the content directory,
"dst_filename" is the absolute path of the target in the output directory.
"""
reporter.report_debug_info("processing image:", dst_filename)
if width is None and height is None:
raise ValueError("Must specify at least one of width or height.")

Expand Down Expand Up @@ -56,8 +58,9 @@ def process_image(

reporter.report_debug_info("imagemagick cmd line", cmdline)
portable_popen(cmdline).wait()
reporter.report_debug_info("guetzli cmd line", cmd2line)
portable_popen(cmd2line).wait()
if use_guetzli:
reporter.report_debug_info("guetzli cmd line", cmd2line)
portable_popen(cmd2line).wait()

@buildprogram(Image)
class ResizedImageBuildProgram(AttachmentBuildProgram):
Expand Down Expand Up @@ -85,14 +88,16 @@ def build_artifact(self, artifact):
if not height:
_, height = compute_dimensions(width, None, w, h)

use_guetzli = bool(conf.get("use_guetzli", "0"))

if not use_guetzli:
use_guetzli = False

df = artifact.source_obj.url_path
ext_pos = df.rfind(".")
if df[ext_pos + 1 :] == 'svg':
dst_filename = "%s-%s.png" % (df[:ext_pos], item)
else:
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])
dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1 :])

def closure(dst_filename, source_img, width, height, resize_image=True):
def closure(dst_filename, source_img, width, height, resize_image=True, ):
# We need this closure, otherwise variables get updated and this
# doesn't work at all.
@ctx.sub_artifact(artifact_name=dst_filename, sources=[source_img])
Expand All @@ -107,7 +112,8 @@ def build_thumbnail_artifact(artifact):
artifact.dst_filename,
width,
height,
quality=85,
quality=89,
use_guetzli=use_guetzli,
extra_params=[
"-strip",
"-interlace",
Expand Down

0 comments on commit f1ac3a1

Please sign in to comment.