243 lines
6.1 KiB
Go
243 lines
6.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/gif"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/disintegration/imaging"
|
|
"golang.org/x/image/bmp"
|
|
"golang.org/x/image/tiff"
|
|
)
|
|
|
|
// ProcessImage applies the specified operation.
|
|
func resizeImage(img image.Image, width, height int, maintainAspect bool) image.Image {
|
|
if maintainAspect {
|
|
if width > 0 && height > 0 {
|
|
return imaging.Thumbnail(img, width, height, imaging.Lanczos)
|
|
}
|
|
if width > 0 && height == 0 {
|
|
return imaging.Resize(img, width, 0, imaging.Lanczos)
|
|
}
|
|
if height > 0 && width == 0 {
|
|
return imaging.Resize(img, 0, height, imaging.Lanczos)
|
|
}
|
|
return img
|
|
}
|
|
targetW := width
|
|
if targetW == 0 {
|
|
targetW = img.Bounds().Dx()
|
|
}
|
|
targetH := height
|
|
if targetH == 0 {
|
|
targetH = img.Bounds().Dy()
|
|
}
|
|
return imaging.Resize(img, targetW, targetH, imaging.Lanczos)
|
|
}
|
|
|
|
func filterImage(img image.Image, filterType string) image.Image {
|
|
switch filterType {
|
|
case "BLUR":
|
|
return imaging.Blur(img, 2.0)
|
|
case "SHARPEN":
|
|
return imaging.Sharpen(img, 2.0)
|
|
case "GRAYSCALE":
|
|
return imaging.Grayscale(img)
|
|
case "INVERT":
|
|
return imaging.Invert(img)
|
|
case "SEPIA":
|
|
gray := imaging.Grayscale(img)
|
|
return imaging.AdjustSaturation(imaging.AdjustSigmoid(gray, 0.5, 3.0), 50)
|
|
default:
|
|
return img
|
|
}
|
|
}
|
|
|
|
func rotateImage(img image.Image, op string) image.Image {
|
|
switch op {
|
|
case "90":
|
|
return imaging.Rotate270(img) // Python's -90 is Rotate270
|
|
case "180":
|
|
return imaging.Rotate180(img)
|
|
case "270":
|
|
return imaging.Rotate90(img) // Python's 90 is Rotate90
|
|
case "FLIP_H":
|
|
return imaging.FlipH(img)
|
|
case "FLIP_V":
|
|
return imaging.FlipV(img)
|
|
default:
|
|
return img
|
|
}
|
|
}
|
|
|
|
func cropImage(img image.Image, left, top, right, bottom int) image.Image {
|
|
bounds := img.Bounds()
|
|
if left < 0 {
|
|
left = 0
|
|
}
|
|
if top < 0 {
|
|
top = 0
|
|
}
|
|
if right > bounds.Dx() {
|
|
right = bounds.Dx()
|
|
}
|
|
if bottom > bounds.Dy() {
|
|
bottom = bounds.Dy()
|
|
}
|
|
rect := image.Rect(left, top, right, bottom)
|
|
return imaging.Crop(img, rect)
|
|
}
|
|
|
|
func watermarkImage(baseImg, watermarkImg image.Image, position string, opacity int) image.Image {
|
|
baseBounds := baseImg.Bounds()
|
|
wmWidth := baseBounds.Dx() / 4
|
|
if wmWidth == 0 {
|
|
wmWidth = 1
|
|
}
|
|
wmBounds := watermarkImg.Bounds()
|
|
wmHeight := int(float64(wmBounds.Dy()) * (float64(wmWidth) / float64(wmBounds.Dx())))
|
|
if wmHeight == 0 {
|
|
wmHeight = 1
|
|
}
|
|
|
|
wmResized := imaging.Resize(watermarkImg, wmWidth, wmHeight, imaging.Lanczos)
|
|
|
|
// Apply opacity
|
|
mask := image.NewUniform(color.Alpha{uint8(255 * opacity / 100)})
|
|
wmWithOpacity := image.NewRGBA(wmResized.Bounds())
|
|
draw.DrawMask(wmWithOpacity, wmWithOpacity.Bounds(), wmResized, image.Point{}, mask, image.Point{}, draw.Over)
|
|
|
|
var pt image.Point
|
|
switch position {
|
|
case "top_left":
|
|
pt = image.Pt(10, 10)
|
|
case "top_right":
|
|
pt = image.Pt(baseBounds.Dx()-wmWidth-10, 10)
|
|
case "bottom_left":
|
|
pt = image.Pt(10, baseBounds.Dy()-wmHeight-10)
|
|
case "bottom_right":
|
|
pt = image.Pt(baseBounds.Dx()-wmWidth-10, baseBounds.Dy()-wmHeight-10)
|
|
default: // center
|
|
pt = image.Pt((baseBounds.Dx()-wmWidth)/2, (baseBounds.Dy()-wmHeight)/2)
|
|
}
|
|
|
|
result := image.NewRGBA(baseBounds)
|
|
draw.Draw(result, baseBounds, baseImg, image.Point{}, draw.Src)
|
|
draw.Draw(result, wmWithOpacity.Bounds().Add(pt), wmWithOpacity, image.Point{}, draw.Over)
|
|
|
|
return result
|
|
}
|
|
|
|
func encodeExternal(img image.Image, targetFormat string, quality int) ([]byte, error) {
|
|
var pngBuf bytes.Buffer
|
|
if err := png.Encode(&pngBuf, img); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmtLower := strings.ToLower(targetFormat)
|
|
if fmtLower == "jpeg" {
|
|
fmtLower = "jpg"
|
|
}
|
|
|
|
// 1. Try ImageMagick convert: convert - -quality <quality> <fmt>:-
|
|
cmd := exec.Command("convert", "-", "-quality", strconv.Itoa(quality), fmtLower+":-")
|
|
cmd.Stdin = bytes.NewReader(pngBuf.Bytes())
|
|
var outBuf bytes.Buffer
|
|
cmd.Stdout = &outBuf
|
|
if err := cmd.Run(); err == nil && outBuf.Len() > 0 {
|
|
return outBuf.Bytes(), nil
|
|
}
|
|
|
|
// 2. Fallback to Python PIL in user's venv
|
|
pythonBin := "/home/creserver/extras/local_venv/bin/python3"
|
|
if _, err := os.Stat(pythonBin); os.IsNotExist(err) {
|
|
pythonBin = "python3"
|
|
}
|
|
|
|
pilFormat := strings.ToUpper(targetFormat)
|
|
if pilFormat == "JPG" {
|
|
pilFormat = "JPEG"
|
|
}
|
|
|
|
pyScript := fmt.Sprintf(`import sys, io
|
|
from PIL import Image
|
|
data = sys.stdin.buffer.read()
|
|
img = Image.open(io.BytesIO(data))
|
|
out = io.BytesIO()
|
|
img.save(out, format='%s', quality=%d)
|
|
sys.stdout.buffer.write(out.getvalue())
|
|
`, pilFormat, quality)
|
|
|
|
pyCmd := exec.Command(pythonBin, "-c", pyScript)
|
|
pyCmd.Stdin = bytes.NewReader(pngBuf.Bytes())
|
|
var pyOutBuf bytes.Buffer
|
|
pyCmd.Stdout = &pyOutBuf
|
|
if err := pyCmd.Run(); err == nil && pyOutBuf.Len() > 0 {
|
|
return pyOutBuf.Bytes(), nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("external encoder failed for %s", targetFormat)
|
|
}
|
|
|
|
func compressLogic(img image.Image, targetFormat string, quality int) ([]byte, string) {
|
|
targetFmt := strings.ToUpper(strings.TrimSpace(targetFormat))
|
|
if targetFmt == "JPG" {
|
|
targetFmt = "JPEG"
|
|
}
|
|
if targetFmt == "" {
|
|
targetFmt = "PNG"
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
switch targetFmt {
|
|
case "JPEG":
|
|
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality}); err == nil {
|
|
return buf.Bytes(), "JPEG"
|
|
}
|
|
case "PNG":
|
|
if err := png.Encode(&buf, img); err == nil {
|
|
return buf.Bytes(), "PNG"
|
|
}
|
|
case "BMP":
|
|
if err := bmp.Encode(&buf, img); err == nil {
|
|
return buf.Bytes(), "BMP"
|
|
}
|
|
case "TIFF":
|
|
if err := tiff.Encode(&buf, img, nil); err == nil {
|
|
return buf.Bytes(), "TIFF"
|
|
}
|
|
case "GIF":
|
|
if err := gif.Encode(&buf, img, nil); err == nil {
|
|
return buf.Bytes(), "GIF"
|
|
}
|
|
default:
|
|
// WEBP, ICO, AVIF, HEIC, etc.
|
|
if data, err := encodeExternal(img, targetFmt, quality); err == nil {
|
|
return data, targetFmt
|
|
}
|
|
}
|
|
|
|
// If switch didn't return (e.g. BMP/TIFF native error, or unknown), try external
|
|
if data, err := encodeExternal(img, targetFmt, quality); err == nil {
|
|
return data, targetFmt
|
|
}
|
|
|
|
// Ultimate fallback to PNG or JPEG
|
|
buf.Reset()
|
|
if err := png.Encode(&buf, img); err == nil {
|
|
return buf.Bytes(), "PNG"
|
|
}
|
|
buf.Reset()
|
|
jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
|
|
return buf.Bytes(), "JPEG"
|
|
}
|