class ActiveStorage::Variant
-
{ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
* {ImageProcessing::Vips}[https://github.com/janko/image_processing/blob/master/doc/vips.md#methods]
* {ImageMagick reference}[https://www.imagemagick.org/script/mogrify.php]
* {ImageProcessing::MiniMagick}[https://github.com/janko/image_processing/blob/master/doc/minimagick.md#methods]
Visit the following links for a list of available ImageProcessing commands and ImageMagick/libvips operations:
avatar.variant(resize_to_limit: [800, 800], colourspace: “b-w”, rotate: “-90”)
ImageProcessing gem (such asresize_to_limit):
You can combine any number of ImageMagick/libvips operations into a variant, as well as any macros provided by the
Then it’ll upload said variant to the service according to a derivative key of the blob and the transformations.
This will create and process a variant of the avatar blob that’s constrained to a height and width of 100.
avatar.variant(resize_to_limit: [100, 100]).processed.url
the transformations, upload the variant to the service, and return itself again. Example:
has already been processed and uploaded to the service, and, if so, just return that. Otherwise it will perform
When you do want to actually produce the variant needed, callprocessed. This will check that the variant
can then produce on-demand.
This will create a URL for that specific blob with that specific variant, which the ActiveStorage::RepresentationsController
<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>
by Active Storage like so:
To refer to such a delayed on-demand variant, simply link to the variant through the resolved route provided
ActiveStorage::RepresentationsController.
in a template, for example. Delay the processing to an on-demand controller, like the one provided in
you also want to be considerate about when the variant is actually processed. You shouldn’t be processing variants inline
Note that to create a variant it’s necessary to download the entire blob file from the service. Because of this process,
# => :vips
Rails.application.config.active_storage.variant_processor = :vips
# => :mini_magick
Rails.application.config.active_storage.variant_processor
gem).
processor operated by the ruby-vips}[https://github.com/libvips/ruby-vips]<br>{MiniMagick gem, but you can also switch to the
default, images will be processed with ImageMagick using the
of the file, so you must addgem "image_processing"to your Gemfile if you wish to use variants. By
Variants rely on ImageProcessing gem for the actual transformations
original.
These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the
Image blobs can have variants that are the result of a set of transformations applied to the original.
def download(&block)
Downloads the file associated with this variant. If no block is given, the entire file is read into memory and returned.
def download(&block) service.download key, &block end
def filename
def filename ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}" end
def image
def image self end
def initialize(blob, variation_or_variation_key)
def initialize(blob, variation_or_variation_key) @blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key) end
def key
def key "variants/#{blob.key}/#{OpenSSL::Digest::SHA256.hexdigest(variation.key)}" end
def process
def process blob.open do |input| variation.transform(input) do |output| service.upload(key, output, content_type: content_type) end end end
def processed
def processed process unless processed? self end
def processed?
def processed? service.exist?(key) end
def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline)
for a variant that points to the ActiveStorage::RepresentationsController, which in turn will use this +service_call+ method
Use url_for(variant) (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL
Returns the URL of the blob variant on the service. See {ActiveStorage::Blob#url} for details.
def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline) service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type end