From ca1296858788780dcb5497e86f66b56170cca279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Fri, 6 Apr 2018 01:48:29 +0200 Subject: Use ImageProcessing gem for ActiveStorage variants ImageProcessing gem is a wrapper around MiniMagick and ruby-vips, and implements an interface for common image resizing and processing. This is the canonical image processing gem recommended in [Shrine], and that's where it developed from. The initial implementation was extracted from Refile, which also implements on-the-fly transformations. Some features that ImageProcessing gem adds on top of MiniMagick: * resizing macros - #resize_to_limit - #resize_to_fit - #resize_to_fill - #resize_and_pad * automatic orientation * automatic thumbnail sharpening * avoids the complex and inefficient MiniMagick::Image class * will use "magick" instead of "convert" on ImageMagick 7 However, the biggest feature of the ImageProcessing gem is that it has an alternative implementation that uses libvips. Libvips is an alternative to ImageMagick that can process images very rapidly (we've seen up 10x faster than ImageMagick). What's great is that the ImageProcessing gem provides the same interface for both implementations. The macros are named the same, and the libvips implementation does auto orientation and thumbnail sharpening as well; only the operations/options specific to ImageMagick/libvips differ. The integration provided by this PR should work for both implementations. The plan is to introduce the ImageProcessing backend in Rails 6.0 as the default backend and deprecate the MiniMagick backend, then in Rails 6.1 remove the MiniMagick backend. --- guides/source/active_storage_overview.md | 21 +++++++++++++++------ guides/source/configuring.md | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'guides') diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index d67f65e88a..f56db61538 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -337,14 +337,15 @@ rails_blob_path(user.avatar, disposition: "attachment") Transforming Images ------------------- -To create variation of the image, call `variant` on the Blob. -You can pass any [MiniMagick](https://github.com/minimagick/minimagick) -supported transformation to the method. +To create variation of the image, call `variant` on the Blob. You can pass +any transformation to the method supported by the procecssor. The default +processor is [MiniMagick](https://github.com/minimagick/minimagick), but you +can also use [Vips](http://www.rubydoc.info/gems/ruby-vips/Vips/Image). -To enable variants, add `mini_magick` to your `Gemfile`: +To enable variants, add `image_processing` gem to your `Gemfile`: ```ruby -gem 'mini_magick' +gem 'image_processing', '~> 1.2' ``` When the browser hits the variant URL, Active Storage will lazy transform the @@ -352,7 +353,15 @@ original blob into the format you specified and redirect to its new service location. ```erb -<%= image_tag user.avatar.variant(resize: "100x100") %> +<%= image_tag user.avatar.variant(resize_to_fit: [100, 100]) %> +``` + +To switch to the Vips processor, you would add the following to +`config/application.rb`: + +```ruby +# Use Vips for processing variants. +config.active_storage.processor = :vips ``` Previewing Files diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 7d5ca4b8a7..ec950b89cd 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -778,6 +778,8 @@ normal Rails server. `config.active_storage` provides the following configuration options: +* `config.active_storage.processor` accepts a symbol `:mini_magick` or `:vips`, specifying whether variant transformations will be performed with MiniMagick or ruby-vips. The default is `:mini_magick`. + * `config.active_storage.analyzers` accepts an array of classes indicating the analyzers available for Active Storage blobs. The default is `[ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer]`. The former can extract width and height of an image blob; the latter can extract width, height, duration, angle, and aspect ratio of a video blob. * `config.active_storage.previewers` accepts an array of classes indicating the image previewers available in Active Storage blobs. The default is `[ActiveStorage::Previewer::PDFPreviewer, ActiveStorage::Previewer::VideoPreviewer]`. The former can generate a thumbnail from the first page of a PDF blob; the latter from the relevant frame of a video blob. -- cgit v1.2.3 From f01e2498905ec057e6c8872d15f8dea18d4cbde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Sun, 22 Apr 2018 23:40:42 +0200 Subject: Rename ActiveStorage.processor to .variant_processor --- guides/source/active_storage_overview.md | 2 +- guides/source/configuring.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'guides') diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index f56db61538..c3fb5356d4 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -361,7 +361,7 @@ To switch to the Vips processor, you would add the following to ```ruby # Use Vips for processing variants. -config.active_storage.processor = :vips +config.active_storage.variant_processor = :vips ``` Previewing Files diff --git a/guides/source/configuring.md b/guides/source/configuring.md index ec950b89cd..c98e9b719c 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -778,7 +778,7 @@ normal Rails server. `config.active_storage` provides the following configuration options: -* `config.active_storage.processor` accepts a symbol `:mini_magick` or `:vips`, specifying whether variant transformations will be performed with MiniMagick or ruby-vips. The default is `:mini_magick`. +* `config.active_storage.variant_processor` accepts a symbol `:mini_magick` or `:vips`, specifying whether variant transformations will be performed with MiniMagick or ruby-vips. The default is `:mini_magick`. * `config.active_storage.analyzers` accepts an array of classes indicating the analyzers available for Active Storage blobs. The default is `[ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer]`. The former can extract width and height of an image blob; the latter can extract width, height, duration, angle, and aspect ratio of a video blob. -- cgit v1.2.3 From dc97f6c3fd2769ba29ea06a6015244be18a19893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Mon, 23 Apr 2018 12:35:43 +0200 Subject: Replace :resize_to_fit with :resize in the guide --- guides/source/active_storage_overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides') diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index c3fb5356d4..3f55e82e24 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -353,7 +353,7 @@ original blob into the format you specified and redirect to its new service location. ```erb -<%= image_tag user.avatar.variant(resize_to_fit: [100, 100]) %> +<%= image_tag user.avatar.variant(resize: "100x100") %> ``` To switch to the Vips processor, you would add the following to -- cgit v1.2.3 From eef0bd0c0a4da52a111639cb9fb5823057cbdf27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Mon, 23 Apr 2018 21:04:35 +0200 Subject: Recommend using :resize_to_fit after all --- guides/source/active_storage_overview.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides') diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index 3f55e82e24..8f723e1e9f 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -353,7 +353,7 @@ original blob into the format you specified and redirect to its new service location. ```erb -<%= image_tag user.avatar.variant(resize: "100x100") %> +<%= image_tag user.avatar.variant(resize_to_fit: [100, 100]) %> ``` To switch to the Vips processor, you would add the following to @@ -375,7 +375,7 @@ the box, Active Storage supports previewing videos and PDF documents.
    <% @message.files.each do |file| %>
  • - <%= image_tag file.preview(resize: "100x100>") %> + <%= image_tag file.preview(resize_to_limit: [100, 100]) %>
  • <% end %>
-- cgit v1.2.3 From 7e4ad3f13daa227d4cbf860dbc5399ddb807a35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Mon, 23 Apr 2018 21:06:06 +0200 Subject: Correct typos --- guides/source/active_storage_overview.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides') diff --git a/guides/source/active_storage_overview.md b/guides/source/active_storage_overview.md index 8f723e1e9f..4a915b1083 100644 --- a/guides/source/active_storage_overview.md +++ b/guides/source/active_storage_overview.md @@ -338,11 +338,11 @@ Transforming Images ------------------- To create variation of the image, call `variant` on the Blob. You can pass -any transformation to the method supported by the procecssor. The default +any transformation to the method supported by the processor. The default processor is [MiniMagick](https://github.com/minimagick/minimagick), but you can also use [Vips](http://www.rubydoc.info/gems/ruby-vips/Vips/Image). -To enable variants, add `image_processing` gem to your `Gemfile`: +To enable variants, add the `image_processing` gem to your `Gemfile`: ```ruby gem 'image_processing', '~> 1.2' -- cgit v1.2.3