mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:39:08 +00:00
* Add MagickWand perf test. * Use MagickWand. * Calculate the right thumbnail dimension. * Remove magick bin file. * Add building dependencies. * Build images. * Add --no-install-recommends * Remove unused field. * Fix typo * Use uint in the worker. * Add guard * Always update apt. * Fix EncodeJpeg() with the quality * Rename the local error with a better name. * Add worker.Terminate() * Fix typo * Rebase with master and rollback the build workflow.
112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
package executable_worker
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/photoview/photoview/api/log"
|
|
"gopkg.in/gographics/imagick.v3/imagick"
|
|
)
|
|
|
|
type MagickWand struct {
|
|
initialized bool
|
|
}
|
|
|
|
func newMagickWand() *MagickWand {
|
|
imagick.Initialize()
|
|
|
|
verstr, vernum := imagick.GetVersion()
|
|
|
|
log.Info("Found magickwand worker: "+verstr, "version", vernum)
|
|
|
|
return &MagickWand{
|
|
initialized: true,
|
|
}
|
|
}
|
|
|
|
func (cli *MagickWand) Terminate() {
|
|
cli.initialized = false
|
|
imagick.Terminate()
|
|
}
|
|
|
|
func (cli *MagickWand) IsInstalled() bool {
|
|
return cli != nil && cli.initialized
|
|
}
|
|
|
|
func (cli *MagickWand) EncodeJpeg(inputPath string, outputPath string, jpegQuality uint) error {
|
|
if !cli.IsInstalled() {
|
|
return fmt.Errorf("ImagickWand is not initialized")
|
|
}
|
|
|
|
wand := imagick.NewMagickWand()
|
|
defer wand.Destroy()
|
|
|
|
if err := wand.ReadImage(inputPath); err != nil {
|
|
return fmt.Errorf("ImagickWand read %q error: %w", inputPath, err)
|
|
}
|
|
|
|
if err := wand.SetFormat("JPEG"); err != nil {
|
|
return fmt.Errorf("ImagickWand set JPEG format for %q error: %w", inputPath, err)
|
|
}
|
|
|
|
if err := wand.SetImageCompressionQuality(jpegQuality); err != nil {
|
|
return fmt.Errorf("ImagickWand set JPEG quality %d for %q error: %w", jpegQuality, inputPath, err)
|
|
}
|
|
|
|
if err := wand.WriteImage(outputPath); err != nil {
|
|
return fmt.Errorf("ImagickWand write %q error: %w", outputPath, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cli *MagickWand) GenerateThumbnail(inputPath string, outputPath string, width, height uint) error {
|
|
if !cli.IsInstalled() {
|
|
return fmt.Errorf("ImagickWand is not initialized")
|
|
}
|
|
|
|
wand := imagick.NewMagickWand()
|
|
defer wand.Destroy()
|
|
|
|
if err := wand.ReadImage(inputPath); err != nil {
|
|
return fmt.Errorf("ImagickWand read %q error: %w", inputPath, err)
|
|
}
|
|
|
|
if err := wand.ThumbnailImage(width, height); err != nil {
|
|
return fmt.Errorf("ImagickWand generate thumbnail for %q error: %w", inputPath, err)
|
|
}
|
|
|
|
if err := wand.SetFormat("JPEG"); err != nil {
|
|
return fmt.Errorf("ImagickWand set JPEG format for %q error: %w", inputPath, err)
|
|
}
|
|
|
|
if err := wand.SetImageCompressionQuality(70); err != nil {
|
|
return fmt.Errorf("ImagickWand set JPEG quality %d for %q error: %w", 70, inputPath, err)
|
|
}
|
|
|
|
if err := wand.WriteImage(outputPath); err != nil {
|
|
return fmt.Errorf("ImagickWand write %q error: %w", outputPath, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cli *MagickWand) IdentifyDimension(inputPath string) (width, height uint, err error) {
|
|
if !cli.IsInstalled() {
|
|
err = fmt.Errorf("ImagickWand is not initialized")
|
|
return
|
|
}
|
|
|
|
wand := imagick.NewMagickWand()
|
|
defer wand.Destroy()
|
|
|
|
if errRI := wand.ReadImage(inputPath); errRI != nil {
|
|
err = fmt.Errorf("ImagickWand read %q error: %w", inputPath, errRI)
|
|
return
|
|
}
|
|
|
|
width = wand.GetImageWidth()
|
|
height = wand.GetImageHeight()
|
|
|
|
return
|
|
}
|