Skip to content

JPEG encoding quality is not configurable #34

@fkxr-dev

Description

@fkxr-dev

Hi,

There's no way to configure the quality level that's used for the JPEG output of the resized images foto generates. In my case I noticed some undesirable compression artifacts in some of my photos- so I wanted to bump the conversion quality up a little bit.

Go's JPEG package defaults to quality level 75/100; I've found for my images 85 is more appropriate. It's quick enough to patch in image.go so that's what I've done for now, but it might be nice to have an option for this in foto.toml, or-- if nothing else-- bump the default up a little bit as 75 can get a big grungy.

Patch attached in case anyone else wants to modify this locally:

From 1f4ee740ce46af1019b17d9ce400de691d0db1c1 Mon Sep 17 00:00:00 2001
From: fkxr <f@fkxr.io>
Date: Sat, 27 Jul 2024 20:52:37 +0200
Subject: [PATCH] force jpeg quality to 85

---
 internal/images/image.go | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/internal/images/image.go b/internal/images/image.go
index afc126d..be9f4ce 100644
--- a/internal/images/image.go
+++ b/internal/images/image.go
@@ -61,10 +61,12 @@ func ResizeData(path string, width int) (*bytes.Buffer, error) {
 	}
 
 	resized := imaging.Resize(src, width, 0, imaging.Lanczos)
-	buf := new(bytes.Buffer)
-	if err := jpeg.Encode(buf, resized, nil); err != nil {
-		return nil, err
-	}
+  buf := new(bytes.Buffer)
+  opt := new (jpeg.Options)
+  opt.Quality = 85
+  if err := jpeg.Encode(buf, resized, opt); err != nil {
+    return nil, err
+  }
 
 	return buf, nil
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions