From e8d4b6346aa314e26ddee395f5e432df776e2377 Mon Sep 17 00:00:00 2001 From: vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> Date: Sat, 17 Aug 2024 18:48:39 +0200 Subject: back to basics Sorry Tyler, but your code was too hard to work with, caused more problems while basically solving none. At first I wanted to solve the background problem (no, replacing fully transparent pixels with white is NOT a good way to make a white background) but the API is complicated when just using the convert command is like one (1) line of code compared to the long-ass thing that magickwand requires I don't think it is any less safe to do it that way compared to what you were doing, also I am using convert directly as opposed to before when I used to pipe user input to sh like a psychopath so that's an improvement i guess --- themagicpipe/imageconverter.go | 54 ++++++++---------------------------------- 1 file changed, 10 insertions(+), 44 deletions(-) (limited to 'themagicpipe/imageconverter.go') diff --git a/themagicpipe/imageconverter.go b/themagicpipe/imageconverter.go index e6592d0..e85a4e0 100644 --- a/themagicpipe/imageconverter.go +++ b/themagicpipe/imageconverter.go @@ -1,9 +1,10 @@ package themagicpipe import ( + "os/exec" "encoding/base64" "errors" - "gopkg.in/gographics/imagick.v3/imagick" + "bytes" "strings" ) @@ -18,51 +19,16 @@ func DataURLConverter(dataURL string) (string, error) { if err != nil { return "", err } - // doing conversion inline soothes the soul - imagick.Initialize() - defer imagick.Terminate() - magick := imagick.NewMagickWand() - defer magick.Destroy() - // read in from base64 - err = magick.ReadImageBlob(imageRaw) - if err != nil { - return "", err - } - // swap all transparent pixels with white - transparent := imagick.NewPixelWand() - defer transparent.Destroy() - transparent.SetColor("none") - whiteFill := imagick.NewPixelWand() - defer whiteFill.Destroy() - whiteFill.SetColor("#ffffff") - err = magick.OpaquePaintImage(transparent, whiteFill, 5, false) - if err != nil { - return "", err - } - err = magick.ResizeImage(400, 200, imagick.FILTER_POINT) - if err != nil { - return "", err - } - err = magick.SetCompression(imagick.COMPRESSION_LZMA) - if err != nil { - return "", err - } - err = magick.StripImage() - if err != nil { - return "", err - } - err = magick.SetDepth(8) - if err != nil { - return "", err - } - err = magick.QuantizeImage(8,imagick.COLORSPACE_UNDEFINED,0,imagick.DITHER_METHOD_NO,false) - if err != nil { - return "", err - } - imageBytesProcessed, err := magick.GetImageBlob() + + converter := exec.Command("convert", "-", "-background", "white", "-flatten", "-resize", "400x200!", "-colors", "8", "PNG8:-") + converter.Stdin = bytes.NewBuffer(imageRaw) + + var output []byte + output, err = converter.Output() if err != nil { return "", err } - imageBase64Processed := base64.StdEncoding.EncodeToString(imageBytesProcessed) + + imageBase64Processed := base64.StdEncoding.EncodeToString(output) return "data:image/png;base64," + imageBase64Processed, nil } -- cgit v1.2.3