All formats

ruby-vips

LanguageRuby
LicenseMIT (libvips: LGPL-2.1)
Repositorygithub.com/libvips/ruby-vips
Documentationruby-vips on rubydoc, heifsave options
Depends onlibvips built with libheif; libheif with libde265 (decode) and x265 (encode)
Read / writeyes / yes, 8/10/12-bit, alpha, multiple images

ruby-vips binds libvips through FFI, so the gem itself has no C code to compile. libvips reads and writes HEIC through libheif, which in turn needs codec plugins: libde265 to decode HEVC and x265 to encode. Install them with libvips, or HEIC files fail to load with an “unsupported codec” error.

Installation#

Shell
# Debian / Ubuntu
sudo apt install libvips42 libheif-plugin-libde265 libheif-plugin-x265
# macOS
brew install vips
gem install ruby-vips

Basic usage: convert HEIC to JPEG#

Ruby
require "vips"

image = Vips::Image.new_from_file("IMG_0001.heic")
puts "#{image.width} x #{image.height}"
image.write_to_file("IMG_0001.jpg", Q: 90)

The stored rotation is applied while loading, so the JPEG is upright.

How to make a thumbnail from a HEIC photo#

Ruby
thumb = Vips::Image.thumbnail("IMG_0001.heic", 400)  # fit in 400 x 400
thumb.write_to_file("thumb.jpg", Q: 85)

How to save an image as HEIC#

Ruby
image = Vips::Image.new_from_file("photo.png")
image.heifsave("photo.heic", Q: 50, compression: :hevc)  # Q 50 is about JPEG Q 90

How to convert a HEIC upload in Rails#

Active Storage uses ruby-vips through the image_processing gem. A variant converts HEIC to JPEG when it is first requested:

Ruby
# in a view
image_tag photo.image.variant(resize_to_limit: [1200, 1200], format: :jpeg)

How to read Exif data#

Ruby
image = Vips::Image.new_from_file("IMG_0001.heic")
puts image.get("exif-ifd0-Model") if image.get_typeof("exif-ifd0-Model") != 0
exif = image.get("exif-data") if image.get_typeof("exif-data") != 0

Tips#