diff --git a/README.md b/README.md index 75f6f80..f489f44 100644 --- a/README.md +++ b/README.md @@ -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 ------- @@ -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 @@ -54,6 +54,7 @@ max_height = 800 [woowee] max_width = 2000 +use_guetzli = True ``` Will take a file called `waffle.jpg` and create the files `waffle-small.jpg`, @@ -61,5 +62,8 @@ Will take a file called `waffle.jpg` and create the files `waffle-small.jpg`, 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. diff --git a/lektor_image_resize.py b/lektor_image_resize.py index 928fd4d..5e87cbe 100644 --- a/lektor_image_resize.py +++ b/lektor_image_resize.py @@ -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.") @@ -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): @@ -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]) @@ -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",