You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.
8000
You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was using RMagick and loving it, but it was eating up huge amounts
of memory. Even a simple script would use over 100MB of Ram. On my
local machine this wasn't a problem, but on my hosting server the
ruby apps would crash because of their 100MB memory limit.
Solution!
Using MiniMagick the ruby processes memory remains small (it spawns
ImageMagick's command line program mogrify which takes up some memory
as well, but is much smaller compared to RMagick)
image=MiniMagick::Image.open("input.jpg")image[:width]# will get the width (you can also use :height and :format)image["EXIF:BitsPerSample"]# It also can get all the EXIF tagsimage["%m:%f %wx%h"]# Or you can use one of the many options of the format command
Unlike RMagick, MiniMagick is a much thinner wrapper around ImageMagick.
To piece together MiniMagick commands refer to the Mogrify Documentation. For instance you can use the -flop option as image.flop.
Operations on a MiniMagick image tend to happen in-place as image.trim, whereas RMagick has both copying and in-place methods like image.trim and image.trim!.
To open files with MiniMagick you use MiniMagick::Image.open as you would Magick::Image.read. To open a file and directly edit it, use MiniMagick::Image.new.
Windows Users
When passing in a blob or IOStream, Windows users need to make sure they read the file in as binary.
# This way works on Windowsbuffer=StringIO.new(File.open(IMAGE_PATH,"rb"){ |f| f.read})MiniMagick::Image.read(buffer)# You may run into problems doing it this waybuffer=StringIO.new(File.read(IMAGE_PATH))
Using GraphicsMagick
Simply set
MiniMagick.processor=:gm
And you are sorted.
Optional image validation
By default, MiniMagick validates image each time it's opening or writing it. Validation means executing identify -quiet -ping command on target image. This adds additional overhead to the whole processing. Sometimes it's safe to assume that all input and output images are valid by default and turn off validation: