MiniMagick
| Language | Ruby |
|---|---|
| License | MIT |
| Repository | github.com/minimagick/minimagick |
| Documentation | README |
| Depends on | ImageMagick command-line tools (magick or convert) |
| Read / write | yes / yes, every entry, BMP and PNG entries; CUR read |
MiniMagick runs ImageMagick commands from Ruby. ImageMagick reads each icon entry as a separate layer and writes one entry per image, so with MiniMagick you can inspect icons, export sizes and build favicons. It uses little memory in your Ruby process because the work happens in a separate ImageMagick process.
ImageMagick must be installed on the machine. It stores entries as BMP unless they’re larger than 256x256 or you set icon:png-compression-size.
Installation#
Shell
gem install mini_magick
# plus ImageMagick
sudo apt install imagemagick # Debian / Ubuntu
brew install imagemagick # macOS
Basic usage: list the sizes in an ICO file#
Ruby
require "mini_magick"
icon = MiniMagick::Image.open("app.ico")
icon.layers.each_with_index do |layer, i|
puts "#{i}: #{layer.width} x #{layer.height}"
end
How to convert ICO to PNG#
Export the largest entry:
Ruby
require "mini_magick"
icon = MiniMagick::Image.open("app.ico")
largest = icon.layers.max_by { |l| l.width * l.height }
largest.write("app.png")
Write every layer in a loop (layer.write("icon-#{i}.png")) to export all sizes.
How to create a multi-size favicon.ico#
icon:auto-resize resizes one square image to every listed size:
Ruby
require "mini_magick"
MiniMagick.convert do |convert|
convert << "logo.png"
convert.resize("256x256")
convert.define("icon:auto-resize=256,64,48,32,16")
convert.define("icon:png-compression-size=256") # 256x256 entry as PNG
convert << "favicon.ico"
end
How to build an ICO from hand-drawn sizes#
Each input file becomes one entry:
Ruby
MiniMagick.convert do |convert|
convert << "icon16.png" << "icon32.png" << "icon48.png" << "icon256.png"
convert << "app.ico"
end
Tips#
MiniMagick.convertrunsmagickon ImageMagick 7 andconverton ImageMagick 6.MiniMagick::Image.opencopies the file to a temporary file first; useMiniMagick::Image.new(path)to work on the file in place.