fix(image-manipulator): robust parameter parsing, transparent JPEG compositing, and full single/batch support for all 12 tools
This commit is contained in:
+560
-341
File diff suppressed because it is too large
Load Diff
@@ -123,12 +123,17 @@ func handleProcessing(w http.ResponseWriter, r *http.Request, processFunc func(i
|
||||
}
|
||||
|
||||
func resizeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
width := parseFormInt(r, "width", 800)
|
||||
height := parseFormInt(r, "height", 600)
|
||||
width := parseFormInt(r, "width", 0)
|
||||
height := parseFormInt(r, "height", 0)
|
||||
maintainAspect := r.FormValue("maintain_aspect") == "true"
|
||||
|
||||
handleProcessing(w, r, func(img image.Image, format string, idx int, filename string) (image.Image, string, float64) {
|
||||
return resizeImage(img, width, height, maintainAspect), format, 0
|
||||
wVal, hVal := width, height
|
||||
if wVal <= 0 && hVal <= 0 {
|
||||
wVal = img.Bounds().Dx()
|
||||
hVal = img.Bounds().Dy()
|
||||
}
|
||||
return resizeImage(img, wVal, hVal, maintainAspect), format, 0
|
||||
})
|
||||
}
|
||||
|
||||
@@ -137,6 +142,7 @@ func convertHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if targetFormat == "" {
|
||||
targetFormat = "png"
|
||||
}
|
||||
quality := parseFormInt(r, "quality", 95)
|
||||
|
||||
if err := r.ParseMultipartForm(1024 * 1024 * 1024); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -155,7 +161,8 @@ func convertHandler(w http.ResponseWriter, r *http.Request) {
|
||||
format = "png"
|
||||
}
|
||||
|
||||
finalFmt, b64 := imageToBase64(img, targetFormat)
|
||||
buf, finalFmt := compressLogic(img, targetFormat, quality)
|
||||
b64 := base64.StdEncoding.EncodeToString(buf)
|
||||
ext := strings.ToLower(finalFmt)
|
||||
if ext == "jpeg" {
|
||||
ext = "jpg"
|
||||
@@ -181,6 +188,7 @@ func convertHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func compressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
quality := parseFormInt(r, "quality", 80)
|
||||
targetFormat := strings.ToLower(r.FormValue("format"))
|
||||
targetSizeMB := parseFormFloat(r, "target_size_mb", 0)
|
||||
|
||||
if err := r.ParseMultipartForm(1024 * 1024 * 1024); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -200,8 +208,43 @@ func compressHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if fmtTarget == "original" || fmtTarget == "" {
|
||||
fmtTarget = format
|
||||
}
|
||||
|
||||
buf, finalFmt := compressLogic(img, fmtTarget, quality)
|
||||
|
||||
var buf []byte
|
||||
var finalFmt string
|
||||
origSize := fileHeader.Size
|
||||
|
||||
if targetSizeMB > 0 && origSize > 0 {
|
||||
targetBytes := int64(targetSizeMB * 1024 * 1024)
|
||||
ratio := float64(targetBytes) / float64(origSize)
|
||||
if ratio >= 1.0 {
|
||||
buf, finalFmt = compressLogic(img, fmtTarget, 92)
|
||||
} else {
|
||||
initQ := int(ratio * 90)
|
||||
if initQ < 10 { initQ = 10 }
|
||||
if initQ > 90 { initQ = 90 }
|
||||
trialBuf, trialFmt := compressLogic(img, fmtTarget, initQ)
|
||||
if int64(len(trialBuf)) <= targetBytes {
|
||||
buf, finalFmt = trialBuf, trialFmt
|
||||
} else {
|
||||
lo, hi := 5, initQ
|
||||
bestBuf, bestFmt := trialBuf, trialFmt
|
||||
for iter := 0; iter < 4; iter++ {
|
||||
mid := (lo + hi) / 2
|
||||
tBuf, tFmt := compressLogic(img, fmtTarget, mid)
|
||||
if int64(len(tBuf)) <= targetBytes {
|
||||
bestBuf, bestFmt = tBuf, tFmt
|
||||
lo = mid + 1
|
||||
} else {
|
||||
hi = mid - 1
|
||||
}
|
||||
}
|
||||
buf, finalFmt = bestBuf, bestFmt
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buf, finalFmt = compressLogic(img, fmtTarget, quality)
|
||||
}
|
||||
|
||||
b64 := base64.StdEncoding.EncodeToString(buf)
|
||||
sizeKB := float64(len(buf)) / 1024.0
|
||||
|
||||
@@ -330,15 +373,21 @@ func watermarkHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
wmFile, _, err := r.FormFile("watermark")
|
||||
if err != nil {
|
||||
http.Error(w, "No watermark image provided", http.StatusBadRequest)
|
||||
return
|
||||
var wmImg image.Image
|
||||
if wmFile, _, err := r.FormFile("watermark"); err == nil {
|
||||
wmImg, _, _ = image.Decode(wmFile)
|
||||
wmFile.Close()
|
||||
} else if wmBase64 := r.FormValue("watermark_base64"); wmBase64 != "" {
|
||||
if commaIdx := strings.Index(wmBase64, ","); commaIdx != -1 {
|
||||
wmBase64 = wmBase64[commaIdx+1:]
|
||||
}
|
||||
if decoded, err := base64.StdEncoding.DecodeString(wmBase64); err == nil {
|
||||
wmImg, _, _ = image.Decode(bytes.NewReader(decoded))
|
||||
}
|
||||
}
|
||||
defer wmFile.Close()
|
||||
wmImg, _, err := image.Decode(wmFile)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
||||
if wmImg == nil {
|
||||
http.Error(w, "No watermark image provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -346,7 +395,7 @@ func watermarkHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if position == "" {
|
||||
position = "bottom_right"
|
||||
}
|
||||
opacity := parseFormInt(r, "opacity", 50)
|
||||
opacity := parseFormInt(r, "opacity", 60)
|
||||
|
||||
files := r.MultipartForm.File["images"]
|
||||
var results []SingleResult
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/tiff"
|
||||
_ "golang.org/x/image/webp"
|
||||
@@ -211,6 +213,75 @@ func previewBatchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func getParamFloat(p map[string]interface{}, key string, def float64) float64 {
|
||||
if v, ok := p[key]; ok {
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
return val
|
||||
case float32:
|
||||
return float64(val)
|
||||
case int:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case string:
|
||||
if f, err := strconv.ParseFloat(val, 64); err == nil {
|
||||
return f
|
||||
}
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getParamInt(p map[string]interface{}, key string, def int) int {
|
||||
if v, ok := p[key]; ok {
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
return int(val)
|
||||
case float32:
|
||||
return int(val)
|
||||
case int:
|
||||
return val
|
||||
case int64:
|
||||
return int(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return i
|
||||
}
|
||||
if f, err := strconv.ParseFloat(val, 64); err == nil {
|
||||
return int(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getParamBool(p map[string]interface{}, key string, def bool) bool {
|
||||
if v, ok := p[key]; ok {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
return val
|
||||
case string:
|
||||
valLower := strings.ToLower(val)
|
||||
return valLower == "true" || valLower == "1" || valLower == "yes"
|
||||
case int:
|
||||
return val != 0
|
||||
case float64:
|
||||
return val != 0
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getParamString(p map[string]interface{}, key string, def string) string {
|
||||
if v, ok := p[key]; ok {
|
||||
if s, ok := v.(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func batchProcessSessionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
sessionData := getSessionData(r)
|
||||
|
||||
@@ -389,35 +460,28 @@ func batchProcessSessionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
switch data.Operation {
|
||||
case "resize":
|
||||
width := 0
|
||||
if w, ok := data.Params["width"].(float64); ok { width = int(w) }
|
||||
height := 0
|
||||
if h, ok := data.Params["height"].(float64); ok { height = int(h) }
|
||||
maintain := true
|
||||
if m, ok := data.Params["maintain_aspect"].(bool); ok { maintain = m }
|
||||
if m, ok := data.Params["maintain"].(bool); ok { maintain = m }
|
||||
width := getParamInt(data.Params, "width", 0)
|
||||
height := getParamInt(data.Params, "height", 0)
|
||||
maintain := getParamBool(data.Params, "maintain_aspect", true)
|
||||
if !maintain {
|
||||
maintain = getParamBool(data.Params, "maintain", false)
|
||||
}
|
||||
processed := resizeImage(img, width, height, maintain)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "convert":
|
||||
targetFmt := "png"
|
||||
if f, ok := data.Params["format"].(string); ok && f != "" {
|
||||
targetFmt = f
|
||||
}
|
||||
buf, finalFmt = compressLogic(img, targetFmt, 95)
|
||||
targetFmt := getParamString(data.Params, "format", "png")
|
||||
quality := getParamInt(data.Params, "quality", 95)
|
||||
buf, finalFmt = compressLogic(img, targetFmt, quality)
|
||||
case "compress":
|
||||
targetFmt := format
|
||||
if f, ok := data.Params["format"].(string); ok && f != "" && f != "original" {
|
||||
targetFmt = f
|
||||
targetFmt := getParamString(data.Params, "format", "original")
|
||||
if targetFmt == "original" {
|
||||
targetFmt = format
|
||||
}
|
||||
q := 80
|
||||
if qval, ok := data.Params["quality"].(float64); ok { q = int(qval) }
|
||||
q := getParamInt(data.Params, "quality", 80)
|
||||
|
||||
// Smart Target Size Compression
|
||||
var targetSizeMB, currentSizeMB float64
|
||||
if t, ok := data.Params["target_size_mb"].(float64); ok { targetSizeMB = t }
|
||||
if tstr, ok := data.Params["target_size_mb"].(string); ok { targetSizeMB, _ = strconv.ParseFloat(tstr, 64) }
|
||||
if c, ok := data.Params["current_size_mb"].(float64); ok { currentSizeMB = c }
|
||||
if cstr, ok := data.Params["current_size_mb"].(string); ok { currentSizeMB, _ = strconv.ParseFloat(cstr, 64) }
|
||||
targetSizeMB := getParamFloat(data.Params, "target_size_mb", 0)
|
||||
currentSizeMB := getParamFloat(data.Params, "current_size_mb", 0)
|
||||
|
||||
if targetSizeMB > 0 && currentSizeMB > 0 {
|
||||
ratio := targetSizeMB / currentSizeMB
|
||||
@@ -464,42 +528,40 @@ func batchProcessSessionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
bounds := img.Bounds()
|
||||
wDx, hDy := float64(bounds.Dx()), float64(bounds.Dy())
|
||||
l, t, r, b := 0, 0, bounds.Dx(), bounds.Dy()
|
||||
if lp, ok := data.Params["left_pct"].(float64); ok {
|
||||
rp, _ := data.Params["right_pct"].(float64)
|
||||
tp, _ := data.Params["top_pct"].(float64)
|
||||
bp, _ := data.Params["bottom_pct"].(float64)
|
||||
if rp > lp && bp > tp {
|
||||
l = int(wDx * lp / 100.0)
|
||||
r = int(wDx * rp / 100.0)
|
||||
t = int(hDy * tp / 100.0)
|
||||
b = int(hDy * bp / 100.0)
|
||||
}
|
||||
lp := getParamFloat(data.Params, "left_pct", -1)
|
||||
rp := getParamFloat(data.Params, "right_pct", -1)
|
||||
tp := getParamFloat(data.Params, "top_pct", -1)
|
||||
bp := getParamFloat(data.Params, "bottom_pct", -1)
|
||||
|
||||
if lp >= 0 && rp > lp && tp >= 0 && bp > tp {
|
||||
l = int(wDx * lp / 100.0)
|
||||
r = int(wDx * rp / 100.0)
|
||||
t = int(hDy * tp / 100.0)
|
||||
b = int(hDy * bp / 100.0)
|
||||
} else {
|
||||
if val, ok := data.Params["left"].(float64); ok { l = int(val) }
|
||||
if val, ok := data.Params["top"].(float64); ok { t = int(val) }
|
||||
if val, ok := data.Params["right"].(float64); ok { r = int(val) }
|
||||
if val, ok := data.Params["bottom"].(float64); ok { b = int(val) }
|
||||
l = getParamInt(data.Params, "left", 0)
|
||||
t = getParamInt(data.Params, "top", 0)
|
||||
r = getParamInt(data.Params, "right", bounds.Dx())
|
||||
b = getParamInt(data.Params, "bottom", bounds.Dy())
|
||||
}
|
||||
processed := cropImage(img, l, t, r, b)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "filter":
|
||||
f := "BLUR"
|
||||
if val, ok := data.Params["filter"].(string); ok && val != "" { f = val }
|
||||
if val, ok := data.Params["filter_type"].(string); ok && val != "" { f = val }
|
||||
f := getParamString(data.Params, "filter", "")
|
||||
if f == "" {
|
||||
f = getParamString(data.Params, "filter_type", "BLUR")
|
||||
}
|
||||
processed := filterImage(img, f)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "rotate":
|
||||
op := "90"
|
||||
if val, ok := data.Params["operation"].(string); ok { op = val }
|
||||
op := getParamString(data.Params, "operation", "90")
|
||||
processed := rotateImage(img, op)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "watermark":
|
||||
wmPath, _ := data.Params["watermark_path"].(string)
|
||||
wmBase64, _ := data.Params["watermark_base64"].(string)
|
||||
pos := "bottom_right"
|
||||
if p, ok := data.Params["position"].(string); ok && p != "" { pos = p }
|
||||
op := 50
|
||||
if o, ok := data.Params["opacity"].(float64); ok { op = int(o) }
|
||||
wmPath := getParamString(data.Params, "watermark_path", "")
|
||||
wmBase64 := getParamString(data.Params, "watermark_base64", "")
|
||||
pos := getParamString(data.Params, "position", "bottom_right")
|
||||
op := getParamInt(data.Params, "opacity", 60)
|
||||
|
||||
var wmImg image.Image
|
||||
if wmBase64 != "" {
|
||||
@@ -520,38 +582,48 @@ func batchProcessSessionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
buf, finalFmt = compressLogic(img, format, 95)
|
||||
case "adjust":
|
||||
brightness, contrast, saturation := 0.0, 0.0, 0.0
|
||||
gamma := 1.0
|
||||
if b, ok := data.Params["brightness"].(float64); ok { brightness = b }
|
||||
if c, ok := data.Params["contrast"].(float64); ok { contrast = c }
|
||||
if s, ok := data.Params["saturation"].(float64); ok { saturation = s }
|
||||
if g, ok := data.Params["gamma"].(float64); ok && g > 0 { gamma = g }
|
||||
brightness := getParamFloat(data.Params, "brightness", 0.0)
|
||||
contrast := getParamFloat(data.Params, "contrast", 0.0)
|
||||
saturation := getParamFloat(data.Params, "saturation", 0.0)
|
||||
gamma := getParamFloat(data.Params, "gamma", 1.0)
|
||||
processed := adjustColor(img, brightness, contrast, saturation, gamma)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "border":
|
||||
borderWidth := 20
|
||||
if bw, ok := data.Params["border_width"].(float64); ok { borderWidth = int(bw) }
|
||||
borderColor := "#ffffff"
|
||||
if bc, ok := data.Params["border_color"].(string); ok && bc != "" { borderColor = bc }
|
||||
padAspect := ""
|
||||
if pa, ok := data.Params["pad_aspect"].(string); ok { padAspect = pa }
|
||||
borderWidth := getParamInt(data.Params, "border_width", 20)
|
||||
borderColor := getParamString(data.Params, "border_color", "#ffffff")
|
||||
padAspect := getParamString(data.Params, "pad_aspect", "none")
|
||||
processed := addBorderCanvas(img, borderWidth, borderColor, padAspect)
|
||||
buf, finalFmt = compressLogic(processed, format, 95)
|
||||
case "round_corners":
|
||||
radius := 30
|
||||
if r, ok := data.Params["radius"].(float64); ok { radius = int(r) }
|
||||
isCircle := false
|
||||
if c, ok := data.Params["is_circle"].(bool); ok { isCircle = c }
|
||||
radius := getParamInt(data.Params, "radius", 35)
|
||||
isCircle := getParamBool(data.Params, "is_circle", false)
|
||||
processed := roundCorners(img, radius, isCircle)
|
||||
buf, finalFmt = compressLogic(processed, "PNG", 95)
|
||||
case "strip_metadata":
|
||||
targetFmt := format
|
||||
if f, ok := data.Params["format"].(string); ok && f != "" && f != "original" {
|
||||
targetFmt = f
|
||||
targetFmt := getParamString(data.Params, "format", "original")
|
||||
if targetFmt == "original" {
|
||||
targetFmt = format
|
||||
}
|
||||
q := 95
|
||||
if qval, ok := data.Params["quality"].(float64); ok { q = int(qval) }
|
||||
q := getParamInt(data.Params, "quality", 95)
|
||||
buf, finalFmt = compressLogic(img, targetFmt, q)
|
||||
case "masker", "mask":
|
||||
var maskImg image.Image
|
||||
maskBase64 := getParamString(data.Params, "mask_base64", "")
|
||||
if maskBase64 != "" {
|
||||
if commaIdx := strings.Index(maskBase64, ","); commaIdx != -1 {
|
||||
maskBase64 = maskBase64[commaIdx+1:]
|
||||
}
|
||||
if decoded, err := base64.StdEncoding.DecodeString(maskBase64); err == nil {
|
||||
maskImg, _, _ = image.Decode(bytes.NewReader(decoded))
|
||||
}
|
||||
}
|
||||
if maskImg != nil {
|
||||
resizedMask := imaging.Resize(maskImg, img.Bounds().Dx(), img.Bounds().Dy(), imaging.Lanczos)
|
||||
buf, finalFmt = compressLogic(resizedMask, "PNG", 95)
|
||||
} else {
|
||||
blackCanvas := imaging.New(img.Bounds().Dx(), img.Bounds().Dy(), color.Black)
|
||||
buf, finalFmt = compressLogic(blackCanvas, "PNG", 95)
|
||||
}
|
||||
default:
|
||||
buf, finalFmt = compressLogic(img, format, 95)
|
||||
}
|
||||
|
||||
@@ -22,24 +22,27 @@ import (
|
||||
|
||||
// ProcessImage applies the specified operation.
|
||||
func resizeImage(img image.Image, width, height int, maintainAspect bool) image.Image {
|
||||
if width <= 0 && height <= 0 {
|
||||
return img
|
||||
}
|
||||
if maintainAspect {
|
||||
if width > 0 && height > 0 {
|
||||
return imaging.Thumbnail(img, width, height, imaging.Lanczos)
|
||||
return imaging.Fit(img, width, height, imaging.Lanczos)
|
||||
}
|
||||
if width > 0 && height == 0 {
|
||||
if width > 0 && height <= 0 {
|
||||
return imaging.Resize(img, width, 0, imaging.Lanczos)
|
||||
}
|
||||
if height > 0 && width == 0 {
|
||||
if height > 0 && width <= 0 {
|
||||
return imaging.Resize(img, 0, height, imaging.Lanczos)
|
||||
}
|
||||
return img
|
||||
}
|
||||
targetW := width
|
||||
if targetW == 0 {
|
||||
if targetW <= 0 {
|
||||
targetW = img.Bounds().Dx()
|
||||
}
|
||||
targetH := height
|
||||
if targetH == 0 {
|
||||
if targetH <= 0 {
|
||||
targetH = img.Bounds().Dy()
|
||||
}
|
||||
return imaging.Resize(img, targetW, targetH, imaging.Lanczos)
|
||||
@@ -146,10 +149,14 @@ func roundCorners(img image.Image, radius int, isCircle bool) image.Image {
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
if isCircle {
|
||||
radius = w / 2
|
||||
if h/2 < radius {
|
||||
radius = h / 2
|
||||
side := w
|
||||
if h < side {
|
||||
side = h
|
||||
}
|
||||
img = imaging.CropCenter(img, side, side)
|
||||
bounds = img.Bounds()
|
||||
w, h = bounds.Dx(), bounds.Dy()
|
||||
radius = side / 2
|
||||
}
|
||||
if radius <= 0 {
|
||||
return img
|
||||
@@ -261,19 +268,23 @@ func rotateImage(img image.Image, op string) image.Image {
|
||||
|
||||
func cropImage(img image.Image, left, top, right, bottom int) image.Image {
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
if left < 0 {
|
||||
left = 0
|
||||
}
|
||||
if top < 0 {
|
||||
top = 0
|
||||
}
|
||||
if right > bounds.Dx() {
|
||||
right = bounds.Dx()
|
||||
if right > w {
|
||||
right = w
|
||||
}
|
||||
if bottom > bounds.Dy() {
|
||||
bottom = bounds.Dy()
|
||||
if bottom > h {
|
||||
bottom = h
|
||||
}
|
||||
rect := image.Rect(left, top, right, bottom)
|
||||
if right <= left || bottom <= top {
|
||||
return img
|
||||
}
|
||||
rect := image.Rect(bounds.Min.X+left, bounds.Min.Y+top, bounds.Min.X+right, bounds.Min.Y+bottom)
|
||||
return imaging.Crop(img, rect)
|
||||
}
|
||||
|
||||
@@ -317,6 +328,19 @@ func watermarkImage(baseImg, watermarkImg image.Image, position string, opacity
|
||||
return result
|
||||
}
|
||||
|
||||
func hasTransparency(img image.Image) bool {
|
||||
bounds := img.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += 4 {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += 4 {
|
||||
_, _, _, a := img.At(x, y).RGBA()
|
||||
if a < 0xFFFF {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func encodeExternal(img image.Image, targetFormat string, quality int) ([]byte, error) {
|
||||
var pngBuf bytes.Buffer
|
||||
if err := png.Encode(&pngBuf, img); err != nil {
|
||||
@@ -352,8 +376,19 @@ func encodeExternal(img image.Image, targetFormat string, quality int) ([]byte,
|
||||
from PIL import Image
|
||||
data = sys.stdin.buffer.read()
|
||||
img = Image.open(io.BytesIO(data))
|
||||
fmt = '%s'
|
||||
if fmt in ('JPEG', 'JPG', 'BMP'):
|
||||
if img.mode in ('RGBA', 'LA', 'P'):
|
||||
bg = Image.new('RGB', img.size, (255, 255, 255))
|
||||
if 'A' in img.mode:
|
||||
bg.paste(img, mask=img.split()[-1])
|
||||
else:
|
||||
bg.paste(img.convert('RGB'))
|
||||
img = bg
|
||||
elif img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
out = io.BytesIO()
|
||||
img.save(out, format='%s', quality=%d)
|
||||
img.save(out, format=fmt, quality=%d)
|
||||
sys.stdout.buffer.write(out.getvalue())
|
||||
`, pilFormat, quality)
|
||||
|
||||
@@ -380,7 +415,12 @@ func compressLogic(img image.Image, targetFormat string, quality int) ([]byte, s
|
||||
var buf bytes.Buffer
|
||||
switch targetFmt {
|
||||
case "JPEG":
|
||||
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality}); err == nil {
|
||||
var rgbImg image.Image = img
|
||||
if hasTransparency(img) {
|
||||
whiteBg := imaging.New(img.Bounds().Dx(), img.Bounds().Dy(), color.White)
|
||||
rgbImg = imaging.Paste(whiteBg, img, image.Pt(0, 0))
|
||||
}
|
||||
if err := jpeg.Encode(&buf, rgbImg, &jpeg.Options{Quality: quality}); err == nil {
|
||||
return buf.Bytes(), "JPEG"
|
||||
}
|
||||
case "PNG":
|
||||
|
||||
+560
-341
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user