dimanche 2 août 2015

How to: Get version image versions dimensions carrierwave

This is my uploader:

class PostImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper
  storage :fog
  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:

  def store_dir
    "uploads/posts"
  end

  version :medium do
    process :resize_to_limit => [280, nil]
    process :convert => 'jpg'
  end

  def filename
   random_token = Digest::SHA2.hexdigest("#{Time.now.utc}--#{model.id.to_s}").first(20)
   ivar = "@#{mounted_as}_secure_token"    
   token = model.instance_variable_get(ivar)
   token ||= model.instance_variable_set(ivar, random_token)
   "#{model.title.gsub(" ", "-").downcase}-#{token}.jpg" if original_filename
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  process :store_dimensions

  private

  def store_dimensions
    if file && model.post_image.medium.present?
     model.image_width, model.image_height = ::MiniMagick::Image.open(file.file)[:dimensions]
    end
  end 

end

in this tutorial, http://ift.tt/1x3Bu52, it is possible to get original image dimensions. But I want to get version image dimensions. Something like this:

def store_dimensions
        if file && model.post_image.medium.present?
         model.image_width, model.image_height = ::MiniMagick::Image.open(model.post_image.medium)[:dimensions]
        end
      end 

Instead of file.file, model.post_image.medium.

The problem is that this code works when the object has been created previously. Only it works when update the image.

I need that this code works when I create it the first time.

Regards!

Aucun commentaire:

Enregistrer un commentaire