Dynamic versionning based on original ratio

29 views
Skip to first unread message

Maxence M

unread,
Aug 7, 2019, 7:51:18 AM8/7/19
to Shrine
I am trying to version my original file depending if the image is landscape or portrait.
Then I tried this in my uploader: 

if pipeline.width > pipeline.height
      versions[:default]  = pipeline.quality(80).resize_to_fit!(1200, 1200)
else
      versions[:default]  = pipeline.quality(80).resize_to_fit!(400, 600)
end

Though everytime, it is the first version that is applied.

I tried to replace pipeline.width and pipeline.height with actual figures and then it works flawlessly.
I am not sure what the ImageMagick methods are actually returning. I tried a binding.pry but can't get it working..

Maxence M

unread,
Aug 8, 2019, 7:53:41 AM8/8/19
to Shrine
I kinda found a monkey patch by using FastImage that Shrine requires anyway for some plugin: 

if FastImage.size(io)[0] > FastImage.size(io)[1]

The above works well.
Yet I don't really understand what fails with MiniMagick. (I may switch to libvips anyway so using Fastimage is a good alternative)

Janko Marohnić

unread,
Aug 8, 2019, 11:40:43 AM8/8/19
to Shrine, Maxence M
Hello,

The ImageProcessing gem cannot be used to retrieve image metadata, it's not related to whether you're using the MiniMagick or Vips backend. Your example doesn't throw an error because ImageProcessing builder object uses #method_missing to add operations, so the result of `pipeline.width > pipeline.height` is an ImageProcessing::Builder object, which is truthy.

You can use the store_dimensions plugin to store dimensions metadata on the cached file, and then use those in the conditional:

  plugin :store_dimensions

  process(:store) do |io, context|
    if io.width < io.height
      # ...
    end
      # ...
    end
  end

If you're using direct uploads, you'll have to also load the restore_cached_data plugin, or use refresh_metadata plugin in the process block.

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/1911e5b6-58ef-4b30-89e6-4e3d0884894f%40googlegroups.com.

Maxence M

unread,
Aug 8, 2019, 5:11:31 PM8/8/19
to Shrine
Thanks Janko.
I don't know why I didn't try with io as well as pipeline. 
Your solution works perfect 
Reply all
Reply to author
Forward
0 new messages