diff --git a/main.go b/main.go index f07d64ed9e4411e1a21aa5f84093d1d8b42e30de..f9c783f4ba651cec70f13067910c0bde9027597a 100644 --- a/main.go +++ b/main.go @@ -19,9 +19,10 @@ func returnResult(config *Config, client *redis.Client, result Result) { client.Publish(config.ResultChannel, string(raw)) } -func generateSize(errorChannel chan error, config *Config, image Image, definition SizeDefinition) { +func generateSize(errorChannel chan error, wand *imagick.MagickWand, wandLinear *imagick.MagickWand, config *Config, image Image, definition SizeDefinition) { errorChannel <- resize( - filepath.Join(config.SourceFolder, image.Id), + wand, + wandLinear, definition.Size, config.Quality, filepath.Join(config.TargetFolder, fmt.Sprintf("%s%s", image.Id, definition.Suffix)), @@ -37,8 +38,22 @@ func processImage(config *Config, client *redis.Client, value string) { errorChannel := make(chan error) + wand := imagick.NewMagickWand() + defer wand.Destroy() + + err := wand.ReadImage(filepath.Join(config.SourceFolder, image.Id)) + if err != nil { + panic(err) + } + + wandLinear := wand.Clone() + err = wandLinear.TransformImageColorspace(imagick.COLORSPACE_RGB) + if err != nil { + panic(err) + } + for _, definition := range config.Sizes { - go generateSize(errorChannel, config, image, definition) + go generateSize(errorChannel, wand, wandLinear, config, image, definition) } errors := make([]string, 0) diff --git a/util.go b/util.go index 3e251f49033506ab3431c20b42dba38d8d6ca33d..ae7ce287447975d6065905443d6fd1e0b96e7349 100644 --- a/util.go +++ b/util.go @@ -7,81 +7,76 @@ import ( "math" ) -func resize(source string, size Size, quality Quality, target string) error { +func resize(wand *imagick.MagickWand, wandLinear *imagick.MagickWand, size Size, quality Quality, target string) error { var err error - - mw := imagick.NewMagickWand() + var mw *imagick.MagickWand defer mw.Destroy() - err = mw.ReadImage(source) - if err != nil { - return err - } - - width := mw.GetImageWidth() - height := mw.GetImageHeight() - - aspectRatio := float64(width) / float64(height) - - var nWidth, nHeight uint - if size.Height != 0 && size.Width != 0 { - if size.Format == sizeFormatContain { - if aspectRatio > 1 { - nWidth = uint(math.Min(float64(size.Width), float64(width))) - nHeight = uint(math.Round(float64(nWidth) / aspectRatio)) - } else { - nHeight = uint(math.Min(float64(size.Height), float64(height))) - nWidth = uint(math.Round(aspectRatio * float64(nHeight))) - } - } else if size.Format == sizeFormatCover { - var targetAspectRatio = float64(size.Width) / float64(size.Height) - var cWidth, cHeight uint - if targetAspectRatio > aspectRatio { - cWidth = width - cHeight = uint(math.Round(float64(width) / targetAspectRatio)) + if size.Width == 0 && size.Height == 0 { + mw = wand.Clone() + } else { + mw = wandLinear.Clone() + + width := mw.GetImageWidth() + height := mw.GetImageHeight() + + aspectRatio := float64(width) / float64(height) + + var nWidth, nHeight uint + if size.Height != 0 && size.Width != 0 { + if size.Format == sizeFormatContain { + if aspectRatio > 1 { + nWidth = uint(math.Min(float64(size.Width), float64(width))) + nHeight = uint(math.Round(float64(nWidth) / aspectRatio)) + } else { + nHeight = uint(math.Min(float64(size.Height), float64(height))) + nWidth = uint(math.Round(aspectRatio * float64(nHeight))) + } + } else if size.Format == sizeFormatCover { + var targetAspectRatio = float64(size.Width) / float64(size.Height) + var cWidth, cHeight uint + if targetAspectRatio > aspectRatio { + cWidth = width + cHeight = uint(math.Round(float64(width) / targetAspectRatio)) + } else { + cHeight = height + cWidth = uint(math.Round(targetAspectRatio * float64(height))) + } + + dx := int((width - cWidth) / 2) + dy := int((height - cHeight) / 2) + + err = mw.CropImage(cWidth, cHeight, dx, dy) + if err != nil { + return err + } + + nHeight = uint(math.Min(float64(size.Height), float64(cHeight))) + nWidth = uint(math.Min(float64(size.Width), float64(cWidth))) } else { - cHeight = height - cWidth = uint(math.Round(targetAspectRatio * float64(height))) + return errors.New(fmt.Sprintf("Format type not recognized: %s", size.Format)) } + } else if size.Height != 0 { + nHeight = uint(math.Min(float64(size.Height), float64(height))) + nWidth = uint(math.Round(aspectRatio * float64(nHeight))) + } else if size.Width != 0 { + nWidth = uint(math.Min(float64(size.Width), float64(width))) + nHeight = uint(math.Round(float64(nWidth) / aspectRatio)) + } else { + nWidth = width + nHeight = height + } - dx := int((width - cWidth) / 2) - dy := int((height - cHeight) / 2) - - err = mw.CropImage(cWidth, cHeight, dx, dy) + if (width != nWidth) || (height != nHeight) { + err = mw.ResizeImage(nWidth, nHeight, imagick.FILTER_LANCZOS, 1) if err != nil { return err } - nHeight = uint(math.Min(float64(size.Height), float64(cHeight))) - nWidth = uint(math.Min(float64(size.Width), float64(cWidth))) - } else { - return errors.New(fmt.Sprintf("Format type not recognized: %s", size.Format)) - } - } else if size.Height != 0 { - nHeight = uint(math.Min(float64(size.Height), float64(height))) - nWidth = uint(math.Round(aspectRatio * float64(nHeight))) - } else if size.Width != 0 { - nWidth = uint(math.Min(float64(size.Width), float64(width))) - nHeight = uint(math.Round(float64(nWidth) / aspectRatio)) - } else { - nWidth = width - nHeight = height - } - - if (width != nWidth) || (height != nHeight) { - err = mw.TransformImageColorspace(imagick.COLORSPACE_RGB) - if err != nil { - return err - } - - err = mw.ResizeImage(nWidth, nHeight, imagick.FILTER_LANCZOS, 1) - if err != nil { - return err - } - - err = mw.TransformImageColorspace(imagick.COLORSPACE_SRGB) - if err != nil { - return err + err = mw.TransformImageColorspace(imagick.COLORSPACE_SRGB) + if err != nil { + return err + } } }