62 Star 374 Fork 120

admpub / nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
thumb_crop.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2020-04-18 14:38 . update
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package file
import (
"bytes"
"io/ioutil"
"path"
"strings"
"github.com/webx-top/client/upload/watermark"
"github.com/webx-top/echo/param"
"github.com/admpub/checksum"
"github.com/admpub/errors"
imageproxy "github.com/admpub/imageproxy"
)
// Crop 裁剪图片
func (t *Thumb) Crop(opt *CropOptions) error {
b, err := ioutil.ReadAll(opt.SrcReader)
if err != nil {
return errors.WithMessage(err, `Thumb.Crop.ReadAll`)
}
thumb, err := imageproxy.Transform(b, *opt.Options)
if err != nil {
return errors.WithMessage(err, `Thumb.Crop.Transform`)
}
if opt.WatermarkOptions != nil && opt.WatermarkOptions.IsEnabled() {
var extension string
if pos := strings.LastIndex(opt.DestFile, `.`); pos > -1 {
extension = opt.DestFile[pos:]
}
b, err = watermark.Bytes(thumb, extension, opt.WatermarkOptions)
if err != nil {
return errors.WithMessage(err, `Thumb.Crop.Bytes`)
}
} else {
b = thumb
}
byteReader := bytes.NewReader(b)
t.SavePath, t.ViewUrl, err = opt.Storer.Put(opt.DestFile, byteReader, byteReader.Size()) //r-4;w-2;x-1
if err != nil {
return errors.WithMessage(err, `Thumb.Crop.Put`)
}
opt.SetThumbData(byteReader)
t.Size = uint64(len(b))
t.Width = param.AsUint(opt.Options.Width)
t.Height = param.AsUint(opt.Options.Height)
t.SaveName = path.Base(t.SavePath)
t.UsedTimes = 0
if len(opt.FileMD5) == 0 {
opt.FileMD5, err = checksum.MD5sumReader(opt.SrcReader)
if err != nil {
return errors.WithMessage(err, `Thumb.Crop.MD5`)
}
}
t.Md5 = opt.FileMD5 //原图Md5
return t.SetByFile(opt.File).Save()
}
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.3

搜索帮助

53164aa7 5694891 3bd8fe86 5694891