Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create multiple versions of image #14

Closed
hakunin opened this issue Nov 23, 2015 · 2 comments
Closed

Can't create multiple versions of image #14

hakunin opened this issue Nov 23, 2015 · 2 comments

Comments

@hakunin
Copy link

hakunin commented Nov 23, 2015

I am trying to create two new versions of the uploaded image, one large and one small.

What I found is, the large is always the same size as thumb.

require "image_processing/mini_magick"

class AttachmentUploader < Shrine
  include ImageProcessing::MiniMagick

  plugin :activerecord
  plugin :logging, logger: Rails.logger

  plugin :determine_mime_type
  plugin :store_dimensions
  plugin :direct_upload, max_size: 20*1024*1024, presign: -> (r) do
    r.params["fields"] || {}
  end
  plugin :remove_attachment
  plugin :versions, names: [:original, :large, :thumb]

  def process(io, context)
    if context[:phase] == :store
      file = io.download
      large = resize_to_limit!(file, 1200, 1200)
      thumb = resize_to_limit!(file, 300, 300)

      {
        original: io, 
        large: large,
        thumb: thumb,
      }
    end
  end
end

The data behind this:

pp JSON.parse(Upload.last.attachment_data)
  Upload Load (0.5ms)  SELECT  "uploads".* FROM "uploads"  ORDER BY "uploads"."id" DESC LIMIT 1
{"original"=>
  {"id"=>"3a0768ce990db7637c54126d634a3e2ebee35ee6d3546bf3fba599f46939.jpg",
   "storage"=>"store",
   "metadata"=>
    {"filename"=>"Interstellar UHD 2.jpg",
     "size"=>1691252,
     "mime_type"=>"image/jpeg",
     "width"=>3840,
     "height"=>2160}},
 "large"=>
  {"id"=>"21c487c57e193eae3650025b453b0669ee083603621af0879823f454025c.jpg",
   "storage"=>"store",
   "metadata"=>
    {"filename"=>"Interstellar UHD 2.jpg",
     "size"=>14306,
     "mime_type"=>"image/jpeg",
     "width"=>300,
     "height"=>169}},
 "thumb"=>
  {"id"=>"7bd60d5ac34e0cc8164067571c36914588135bc87673f9f9b68660277ee7.jpg",
   "storage"=>"store",
   "metadata"=>
    {"filename"=>"Interstellar UHD 2.jpg",
     "size"=>14306,
     "mime_type"=>"image/jpeg",
     "width"=>300,
     "height"=>169}}}

@janko
Copy link
Member

janko commented Nov 23, 2015

This is actually expected behaviour. If you use resize_to_limit! (with a bang), that modifies the file in-place. So in your example, large will have the same path as file, and thumb as well. So when you resize_to_limit! the file again and assign it to thumb, it will modify large as well. This is generally how references work.

What is not expected was that I was recommending it in the README. In processing versions you should generally always use the non-bang methods (resize_to_limit instead of resize_to_limit!). I updated the documentation with this recommendation.

@janko janko closed this as completed Nov 23, 2015
@hakunin
Copy link
Author

hakunin commented Nov 23, 2015

👍 Awesome, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants