Go图片压缩工具
在网络应用开发中,常通过对图片进行压缩来加快网页加载的速度。Go实现图片压缩十分简单,以下代码实现传入图片路径完成压缩工作的功能,支持jpg和png格式的图片文件。
// import "github.com/nfnt/resize"
//compress a jpg or png format image, the new images will be named autoly
func CompressImg(source string, hight uint) error {
var err error
var file *os.File
reg, _ := regexp.Compile(`^.*\.((png)|(jpg))$`)
if !reg.MatchString(source) {
err = errors.New("%s is not a .png or .jpg file")
logs.Error(err)
return err
}
if file, err = os.Open(source); err