修复:
保存图片的时候出现 颜色无法转换的问题
// CropImage by the specified box, quality is only for jpeg bin.
func CropImage(bin []byte, quality, x, y, width, height int) ([]byte, error) {
img, typ, err := image.Decode(bytes.NewBuffer(bin))
if err != nil {
return nil, err
}
cropped := bytes.NewBuffer(nil)
switch typ {
case "png":
switch img := img.(type) {
case *image.NRGBA:
img = img.SubImage(image.Rect(x, y, x+width, y+height)).(*image.NRGBA)
err = png.Encode(cropped, img)
case *image.RGBA:
img = img.SubImage(image.Rect(x, y, x+width, y+height)).(*image.RGBA)
err = png.Encode(cropped, img)
default:
return nil, fmt.Errorf("unsupported image type: %T", img)
}
case "jpeg":
switch img := img.(type) {
case *image.YCbCr:
img = img.SubImage(image.Rect(x, y, x+width, y+height)).(*image.YCbCr)
if quality == 0 {
quality = 80
}
err = jpeg.Encode(cropped, img, &jpeg.Options{Quality: quality})
default:
return nil, fmt.Errorf("unsupported image type for jpeg: %T", img)
}
}
return cropped.Bytes(), err
}改进部分:增加色域匹配机制