For apps built before the image_processing
gem became the default, the migration will involve two steps:
- Migrating to the image processing syntax while still using ImageMagick;
- Switching to Vips and updating the compression options;
Before changing from ImageMagick to Vips, it's better to first test the new syntax and ensure everything is still working.
variant(format: :jpg, strip: true, quality: 80)
variant(format: :jpg, saver: { strip: true, quality: 80 })
If you forget to do this, when you switch to vips you will see this error: VipsOperation: class "quality" not found
.
variant(resize: "750x")
variant(resize_to_limit: [750, nil])
If you forget to do this, when you switch to vips you will see this error: no implicit conversion to float from string
variant(crop: "535x642+3+276")
variant(crop: [3, 276, 535, 642])
If you forget to do this, when you switch to vips you will see this error: unable to call crop: you supplied 2 arguments, but operation needs 5.
Vips is a more strict than ImageMagick when it comes to cropping:
- It will not crop if
x
and/ory
are negative values. e.g.:[-10, -10, 100, 100]
- It will not crop if position (
x
ory
) plus crop dimension (width
,height
) are larger than image dimensions. e.g.: a 125x125 image and a crop of[50, 50, 100, 100]
If you forget to do this, when you switch to vips you will see this error: extract_area: bad extract area
Vips uses black as the default background color resize_and_pad
, instead of white like ImageMagick. Fix that by using the background
option:
variant(resize_and_pad: [300, 300])
variant(resize_and_pad: [300, 300, background: [255]])
After you are sure everything is still working, switch the variant processor to vips, and make the following adjustments
Vips has different compression options compared to ImageMagick. This is a rough equivalent that you can use to update yours
# JPEG
variant(strip: true, quality: 80, interlace: "JPEG", sampling_factor: "4:2:0", colorspace: "sRGB")
variant(saver: { strip: true, quality: 80, interlace: true })
# PNG
variant(strip: true, quality: 75)
variant(saver: { strip: true, compression: 9 })
# WEBP
variant(strip: true, quality: 75, define: { webp: { lossless: false, alpha_quality: 85, thread_level: 1 } })
variant(saver: { strip: true, quality: 75, lossless: false, alpha_q: 85, reduction_effort: 6, smart_subsample: true })
# GIF
variant(layers: "Optimize")
variant(saver: { optimize_gif_frames: true, optimize_gif_transparency: true })
Vips will auto rotate images using the EXIF value when processing variants. If you were storing rotation values from user uploaded photos to apply rotation with ImageMagick, you must stop doing that:
variant(format: :jpg, rotate: rotation_value)
variant(format: :jpg)
Vips uses another options for black and white images
variant(monochrome: true)
variant(colourspace: "b-w")
If you are using fragment caching, make sure you clear your caches when you send vips to production. The urls generated by Active Storage contain an encoded value that represents the transformations that should be performed on the image and they will be in the old format. If you don't clear the caches you will start seeing the errors I mentioned above.
Finally, be prepared to STILL see some of those errors showing up as things outside of your control try to access old image urls.