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 | |
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
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | themagicpipe/imageconverter.go | 54 |
3 files changed, 10 insertions, 47 deletions
@@ -9,7 +9,6 @@ require ( github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 golang.org/x/crypto v0.21.0 - gopkg.in/gographics/imagick.v3 v3.7.0 ) require ( @@ -51,7 +51,5 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/gographics/imagick.v3 v3.7.0 h1:w8iQa58ikuqjX4l2OVML3pgqFcDMD8ywXJ9/cXa33fk= -gopkg.in/gographics/imagick.v3 v3.7.0/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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 } |