go随聊-golang实现生成缩略图

来源(CSDN博客)

From: https://blog.csdn.net/yimin_tank/article/details/83659485

go随聊-golang实现生成缩略图

九品神元师 2018-11-02 18:34:38 1359 收藏 1

分类专栏: go随聊 go入了门就不想放弃

版权

项目中难免会遇到图片查询的情况,有时候原始图片很大,考虑到带宽,可以只传缩略图给前端。golang有很多生成缩略图的方法,本文介绍一个不错的图片处理包。

disintegration/imaging

https://github.com/disintegration/imaging

Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.).

All the image processing functions provided by the package accept any image type that implements image.Image interface as an input, and return a new image of *image.NRGBA type (32bit RGBA colors, not premultiplied by alpha).

Installation

go get -u github.com/disintegration/imaging

例子一:

func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA
import (
	"fmt"
	"bytes"
	"testing"
	"io/ioutil"

	"github.com/disintegration/imaging"
)

func TestImageResize(t *testing.T) {
	//读取本地文件,本地文件尺寸300*400
	imgData,_:=ioutil.ReadFile("d:/1.jpg")
	buf:=bytes.NewBuffer(imgData)
	image,err:=imaging.Decode(buf)
	if err!=nil{
		fmt.Println(err)
		return
	}
	//生成缩略图,尺寸150*200,并保持到为文件2.jpg
	image=imaging.Resize(image, 150, 200, imaging.Lanczos)
	err=imaging.Save(image,"d:/2.jpg")
	if err!=nil{
		fmt.Println(err)
	}
}




width和height都传了值,就按传值的比例放缩

例子二:

import (
	"fmt"
	"bytes"
	"testing"
	"io/ioutil"

	"github.com/disintegration/imaging"
)

func TestImageResize(t *testing.T) {
	//读取本地文件,本地文件尺寸300*400
	imgData,_:=ioutil.ReadFile("d:/1.jpg")
	buf:=bytes.NewBuffer(imgData)
	image,err:=imaging.Decode(buf)
	if err!=nil{
		fmt.Println(err)
		return
	}
	//生成缩略图,尺寸宽100,高传0表示等比例放缩
	//最后缩略图尺寸为100*133
	image=imaging.Resize(image, 100, 0, imaging.Lanczos)
	err=imaging.Save(image,"d:/2.jpg")
	if err!=nil{
		fmt.Println(err)
	}
}




width传了值,height传0则表示宽按传值宽度拉伸,高则等比例拉伸

例子三:

func Fill(img image.Image, width, height int, anchor Anchor, filter ResampleFilter) *image.NRGBA
import (
	"fmt"
	"bytes"
	"testing"
	"io/ioutil"

	"github.com/disintegration/imaging"
)

func TestImageResize(t *testing.T) {
	//读取本地文件,本地文件尺寸300*400
	imgData,_:=ioutil.ReadFile("d:/1.jpg")
	buf:=bytes.NewBuffer(imgData)
	image,err:=imaging.Decode(buf)
	if err!=nil{
		fmt.Println(err)
		return
	}
	image=imaging.Fill(image, 64, 64, imaging.Center,imaging.Lanczos)
	err=imaging.Save(image,"d:/2.jpg")
	if err!=nil{
		fmt.Println(err)
	}
}




参数说明:

NearestNeighbor - Fastest resampling filter, no antialiasing.

Box - Simple and fast averaging filter appropriate for downscaling. When upscaling it's similar to NearestNeighbor.

Linear - Bilinear filter, smooth and reasonably fast.

MitchellNetravali - А smooth bicubic filter.

CatmullRom - A sharp bicubic filter.

Gaussian - Blurring filter that uses gaussian function, useful for noise removal.

Lanczos - High-quality resampling filter for photographic images yielding sharp results, slower than cubic filters.



————————————————

版权声明:本文为CSDN博主「九品神元师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/yimin_tank/java/article/details/83659485

Link: http://www.asm32.net/article_details.aspx?id=7365


浏览次数 0 发布时间 2020-07-22 00:04:06 从属分类 Go语言 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1