fix(image-manipulator): prevent excessive WebP compression and preserve crisp resolution with cwebp Sharp-YUV and quality controls
This commit is contained in:
@@ -341,13 +341,108 @@ func hasTransparency(img image.Image) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// encodeWebP encodes an image to high-fidelity WebP without aggressive compression or resolution loss.
|
||||
func encodeWebP(img image.Image, quality int) ([]byte, error) {
|
||||
if quality <= 0 {
|
||||
quality = 95
|
||||
}
|
||||
if quality > 100 {
|
||||
quality = 100
|
||||
}
|
||||
|
||||
var pngBuf bytes.Buffer
|
||||
if err := png.Encode(&pngBuf, img); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawPng := pngBuf.Bytes()
|
||||
|
||||
// 1. First-class: Google cwebp binary with Sharp-YUV and full alpha quality
|
||||
if cwebpPath, err := exec.LookPath("cwebp"); err == nil && cwebpPath != "" {
|
||||
var args []string
|
||||
if quality >= 100 {
|
||||
// True lossless WebP compression
|
||||
args = []string{"-lossless", "-m", "6", "-exact", "-o", "-", "--", "-"}
|
||||
} else {
|
||||
// High quality lossy: minimal quantization, Sharp-YUV prevents color edge blur, exact alpha
|
||||
args = []string{
|
||||
"-q", strconv.Itoa(quality),
|
||||
"-m", "6",
|
||||
"-sharp_yuv",
|
||||
"-alpha_q", "100",
|
||||
"-exact",
|
||||
"-o", "-",
|
||||
"--", "-",
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(cwebpPath, args...)
|
||||
cmd.Stdin = bytes.NewReader(rawPng)
|
||||
var outBuf bytes.Buffer
|
||||
cmd.Stdout = &outBuf
|
||||
if err := cmd.Run(); err == nil && outBuf.Len() > 0 {
|
||||
return outBuf.Bytes(), nil
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to Python Pillow in user venv (properly supports libwebp quality, method 6, alpha)
|
||||
pythonBin := "/home/creserver/extras/local_venv/bin/python3"
|
||||
if _, err := os.Stat(pythonBin); os.IsNotExist(err) {
|
||||
pythonBin = "python3"
|
||||
}
|
||||
|
||||
pyScript := fmt.Sprintf(`import sys, io
|
||||
from PIL import Image
|
||||
data = sys.stdin.buffer.read()
|
||||
img = Image.open(io.BytesIO(data))
|
||||
if img.mode == 'CMYK':
|
||||
img = img.convert('RGB')
|
||||
elif img.mode in ('P', 'LA'):
|
||||
img = img.convert('RGBA')
|
||||
|
||||
out = io.BytesIO()
|
||||
q = %d
|
||||
if q >= 100:
|
||||
img.save(out, format='WEBP', lossless=True, method=6)
|
||||
else:
|
||||
img.save(out, format='WEBP', quality=q, method=6, alpha_quality=100)
|
||||
sys.stdout.buffer.write(out.getvalue())
|
||||
`, quality)
|
||||
|
||||
pyCmd := exec.Command(pythonBin, "-c", pyScript)
|
||||
pyCmd.Stdin = bytes.NewReader(rawPng)
|
||||
var pyOutBuf bytes.Buffer
|
||||
pyCmd.Stdout = &pyOutBuf
|
||||
if err := pyCmd.Run(); err == nil && pyOutBuf.Len() > 0 {
|
||||
return pyOutBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
// 3. Fallback to ImageMagick if neither cwebp nor Pillow are available
|
||||
imQuality := quality
|
||||
if imQuality <= 0 {
|
||||
imQuality = 95
|
||||
}
|
||||
imCmd := exec.Command("convert", "-", "-quality", strconv.Itoa(imQuality), "webp:-")
|
||||
imCmd.Stdin = bytes.NewReader(rawPng)
|
||||
var imOutBuf bytes.Buffer
|
||||
imCmd.Stdout = &imOutBuf
|
||||
if err := imCmd.Run(); err == nil && imOutBuf.Len() > 0 {
|
||||
return imOutBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to encode webp")
|
||||
}
|
||||
|
||||
func encodeExternal(img image.Image, targetFormat string, quality int) ([]byte, error) {
|
||||
fmtLower := strings.ToLower(targetFormat)
|
||||
if fmtLower == "webp" {
|
||||
return encodeWebP(img, quality)
|
||||
}
|
||||
|
||||
var pngBuf bytes.Buffer
|
||||
if err := png.Encode(&pngBuf, img); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmtLower := strings.ToLower(targetFormat)
|
||||
if fmtLower == "jpeg" {
|
||||
fmtLower = "jpg"
|
||||
}
|
||||
@@ -427,6 +522,10 @@ func compressLogic(img image.Image, targetFormat string, quality int) ([]byte, s
|
||||
if err := png.Encode(&buf, img); err == nil {
|
||||
return buf.Bytes(), "PNG"
|
||||
}
|
||||
case "WEBP":
|
||||
if data, err := encodeWebP(img, quality); err == nil {
|
||||
return data, "WEBP"
|
||||
}
|
||||
case "BMP":
|
||||
if err := bmp.Encode(&buf, img); err == nil {
|
||||
return buf.Bytes(), "BMP"
|
||||
@@ -440,7 +539,7 @@ func compressLogic(img image.Image, targetFormat string, quality int) ([]byte, s
|
||||
return buf.Bytes(), "GIF"
|
||||
}
|
||||
default:
|
||||
// WEBP, ICO, AVIF, HEIC, etc.
|
||||
// ICO, AVIF, HEIC, etc.
|
||||
if data, err := encodeExternal(img, targetFmt, quality); err == nil {
|
||||
return data, targetFmt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user