裁剪 mysql 编译,golang图片裁剪和缩略图生成

直接贴代码了

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

}

有疑问加站长微信联系(非本文作者)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值