golang图片裁剪和缩略图生成

cqvoip · · 11416 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

直接贴代码了

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
}

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

本文来自:博客园

感谢作者:cqvoip

查看原文:golang图片裁剪和缩略图生成

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

11416 次点击  
加入收藏 微博
1 回复  |  直到 2019-02-19 16:52:57
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传