123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package image
- import (
- "fmt"
- "github.com/gographics/gmagick"
- "icloudapp.cn/tools/util"
- fileObj "icloudapp.cn/tools/util/file"
- )
- type Image struct {
- Cache
- Path string
- }
- func NewImage(path string) *Image {
- return &Image{
- Path: path,
- }
- }
- func (i *Image) Create(file, background string, size Size) (*gmagick.MagickWand, error) {
- if file != "" {
- file = fmt.Sprintf("%s/%s", i.Path, file)
- if !fileObj.IsFile(file) {
- return nil, util.NewError(fmt.Sprintf("文件 %s 不存在", file))
- }
- }
- imageMW := gmagick.NewMagickWand()
- if file != "" {
- if err := imageMW.ReadImage(file); err != nil {
- return nil, util.NewError(fmt.Sprintf("imageMW.ReadImageBlob:%s", err.Error()))
- }
- } else {
- if err := imageMW.ReadImageBlob(util.TransparentPNG()); err != nil {
- return nil, util.NewError(fmt.Sprintf("imageMW.ReadImageBlob:%s", err.Error()))
- }
- }
- fmt.Println("size", size)
- if err := imageMW.SetImageFormat("PNG"); err != nil {
- return nil, util.NewError("imageMW.SetImageFormat", err.Error())
- }
- if err := imageMW.ResizeImage(size.Width, size.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
- return nil, util.NewError("imageMW.SetSize", err.Error())
- }
- if background != "" {
- if err := imageMW.SetImageBackgroundColor(Pixel(background)); err != nil {
- return nil, util.NewError(fmt.Sprintf("imageMW.SetImageBackgroundColor:%s", err.Error()))
- }
- }
- return imageMW, nil
- }
- func (i *Image) Thumb(file string, size *Size) (string, error) {
- if !fileObj.IsFile(file) {
- return "", util.NewError(fmt.Sprintf("文件 %s 不存在", file))
- }
- mw := gmagick.NewMagickWand()
- defer mw.Destroy()
- var err error
- if err = mw.ReadImage(file); err != nil {
- return "", err
- }
- width := mw.GetImageWidth()
- height := mw.GetImageHeight()
- imageScale := NewScale(width, height, 1.0)
- scale := imageScale.MinRatio(size.Width, size.Height)
- if err = mw.ResizeImage(scale.Width, scale.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
- return "", err
- }
- //fmt.Println("DisplayImage:", os.Getenv("DISPLAY"))
- //err = mw.DisplayImage(os.Getenv("DISPLAY"))
- destFile := i.CacheFile(file, mw.GetImageFormat())
- if err = mw.WriteImage(destFile); err != nil {
- return "", err
- }
- return destFile, nil
- }
- func (i *Image) Cut(file string, size *Size, position string) (string, error) {
- if position == "" {
- position = "left"
- }
- mw := gmagick.NewMagickWand()
- defer mw.Destroy()
- var err error
- err = mw.ReadImage(file)
- if err != nil {
- return "", err
- }
- sourceWidth := mw.GetImageWidth()
- sourceHeight := mw.GetImageHeight()
- imageScale := NewScale(sourceWidth, sourceHeight, 1.0)
- scale := imageScale.MaxRatio(size.Width, size.Height)
- if err = mw.ResizeImage(scale.Width, scale.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
- return "", err
- }
- x, y, width, height := i.Position(position, scale.Width, scale.Height, size)
- if err = mw.CropImage(width, height, x, y); err != nil {
- return "", err
- }
- destFile := i.CacheFile(file, mw.GetImageFormat())
- if err = mw.WriteImage(destFile); err != nil {
- return "", err
- }
- return destFile, nil
- }
- func (i *Image) Resize(image string, width, height uint, angle int) (*gmagick.MagickWand, error) {
- imageMV := gmagick.NewMagickWand()
- if err := imageMV.ReadImage(image); err != nil {
- return nil, util.NewError("Resize", "ReadImage", err.Error())
- }
- if err := imageMV.ResizeImage(width, height, gmagick.FILTER_LANCZOS, 1); err != nil {
- return nil, util.NewError("Resize", "ResizeImage", err.Error())
- }
- if err := imageMV.SetSize(width, height); err != nil {
- return nil, util.NewError("Resize", "SetSize", err.Error())
- }
- return imageMV, nil
- }
- // 计算 裁切的x、y及宽高
- func (i *Image) Position(position string, width, height uint, size *Size) (dx, dy int, dWidth, dHeight uint) {
- x := 0
- y := 0
- scale := 1.0
- if width < size.Width {
- scale = CustomScale(width, size.Width)
- width = size.Width
- }
- if height < size.Height {
- scale = CustomScale(height, size.Height)
- height = size.Height
- }
- paddingLeft := width - size.Width
- paddingTop := height - size.Height
- if position == "right" {
- x = int(paddingLeft)
- }
- if position == "center" {
- x = int(paddingLeft / 2)
- y = int(paddingTop / 2)
- }
- if position == "right|center" {
- x = int(paddingLeft)
- y = int(paddingTop / 2)
- }
- if position == "left|center" {
- y = int(paddingTop / 2)
- }
- return x, y, uint(float64(size.Width) * scale), uint(float64(size.Height) * scale)
- }
- // Cache 获取缓存文件名
- /*func (i *Image) CacheFile(file string, format string) string {
- if !fileObj.IsDir(i.Cache) {
- if err := os.Mkdir(i.Cache, 0755); err != nil {
- panic(err.Error())
- }
- }
- if file == "" {
- file = util.RandomStr(10)
- }
- destName := fmt.Sprintf("%x", md5.Sum([]byte(file)))
- destFile := fmt.Sprintf("%s/%s.%s", i.Cache, destName, strings.ToLower(format))
- return destFile
- }*/
|