Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output ImageMagick Result as Base64 String Instead of File

Tags:

imagemagick

On the command line, I can do:

convert sourceimg.jpg | base64

and receive a string of output representing the image. However, if I add any transformations to the image, nothing is output:

convert sourceimage.jpg -resize 400x400 output.img | base64

Is there a quick way to get the base64 representation of the result of imagemagick commands?

like image 507
Snowball Avatar asked Jul 11 '16 06:07

Snowball


2 Answers

If you want the output image as a PNG, use:

convert input.jpg -resize 400x400 PNG:- | base64

If you want the output image as a JPG, use:

convert input.jpg -resize 400x400 JPG:- | base64
like image 135
Mark Setchell Avatar answered Nov 17 '22 04:11

Mark Setchell


In ImageMagick there is an INLINE: format that will save to base 64. See http://www.imagemagick.org/Usage/files/#inline

So you can also do

variable=$(convert input.jpg -resize 400x400 INLINE:PNG:-)
like image 45
fmw42 Avatar answered Nov 17 '22 04:11

fmw42