golang图片裁剪和缩略图生成

时间:2024-12-25 21:06:56

直接贴代码了

package main

import (
"errors"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"strings" "golang.org/x/image/bmp" "github.com/nfnt/resize"
) func main() {
src := "data/1.gif"
dst := strings.Replace(src, ".", "_small.", 1)
fmt.Println("src=", src, " dst=", dst)
fIn, _ := os.Open(src)
defer fIn.Close() fOut, _ := os.Create(dst)
defer fOut.Close() // err := clip(fIn, fOut, 0, 0, 150, 150, 100)
// if err != nil {
// panic(err)
// }
err := scale(fIn, fOut, 150, 150, 100)
if err != nil {
panic(err)
}
} /*
* 图片裁剪
* 入参:
* 规则:如果精度为0则精度保持不变
*
* 返回:error
*/
func clip(in io.Reader, out io.Writer, x0, y0, x1, y1, quality int) error {
origin, fm, err := image.Decode(in)
if err != nil {
return err
} switch fm {
case "jpeg":
img := origin.(*image.YCbCr)
subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.YCbCr)
return jpeg.Encode(out, subImg, &jpeg.Options{quality})
case "png":
switch canvas.(type) {
case *image.NRGBA:
img := canvas.(*image.NRGBA)
subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.NRGBA)
return png.Encode(out, subImg)
case *image.RGBA:
img := canvas.(*image.RGBA)
subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
return png.Encode(out, subImg)
}
case "gif":
img := origin.(*image.Paletted)
subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.Paletted)
return gif.Encode(out, subImg, &gif.Options{})
case "bmp":
img := origin.(*image.RGBA)
subImg := img.SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
return bmp.Encode(out, subImg)
default:
return errors.New("ERROR FORMAT")
}
return nil
} /*
* 缩略图生成
* 入参:
* 规则: 如果width 或 hight其中有一个为0,则大小不变 如果精度为0则精度保持不变
* 矩形坐标系起点是左上
* 返回:error
*/
func scale(in io.Reader, out io.Writer, width, height, quality int) error {
origin, fm, err := image.Decode(in)
if err != nil {
return err
}
if width == 0 || height == 0 {
width = origin.Bounds().Max.X
height = origin.Bounds().Max.Y
}
if quality == 0 {
quality = 100
}
canvas := resize.Thumbnail(uint(width), uint(height), origin, resize.Lanczos3) //return jpeg.Encode(out, canvas, &jpeg.Options{quality}) switch fm {
case "jpeg":
return jpeg.Encode(out, canvas, &jpeg.Options{quality})
case "png":
return png.Encode(out, canvas)
case "gif":
return gif.Encode(out, canvas, &gif.Options{})
case "bmp":
return bmp.Encode(out, canvas)
default:
return errors.New("ERROR FORMAT")
}
return nil
}