diff options
author | vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> | 2024-08-17 18:48:39 +0200 |
---|---|---|
committer | vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> | 2024-08-17 18:48:39 +0200 |
commit | e8d4b6346aa314e26ddee395f5e432df776e2377 (patch) | |
tree | 94e7879971aa6320026c8dfe511b276c7392714c /themagicpipe/imageconverter.go | |
parent | 747cbf84f241eb699d0d00448a71ae2d3cdffb7b (diff) |
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
Diffstat (limited to 'themagicpipe/imageconverter.go')
-rw-r--r-- | themagicpipe/imageconverter.go | 54 |
1 files changed, 10 insertions, 44 deletions
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 } |