package image import ( "fmt" "gopkg.in/gographics/imagick.v3/imagick" "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) (*imagick.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 := imagick.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, imagick.FILTER_LANCZOS); 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 := imagick.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, imagick.FILTER_LANCZOS); 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 := imagick.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, imagick.FILTER_LANCZOS); 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) (*imagick.MagickWand, error) { imageMV := imagick.NewMagickWand() if err := imageMV.ReadImage(image); err != nil { return nil, util.NewError("Resize", "ReadImage", err.Error()) } if err := imageMV.ResizeImage(width, height, imagick.FILTER_LANCZOS); 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) }