golang圖片裁剪和縮圖產生

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

直接貼代碼了

package mainimport (    "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}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.