Ruby-on-Rails [SOLVED]: Rails Carrierwave & Imagemagick, using conditions to resize image

Ruby-on-Rails [SOLVED]: Rails Carrierwave & Imagemagick, using conditions to resize image

Home Forums Frameworks Ruby on Rails Tutorials Ruby-on-Rails [SOLVED]: Rails Carrierwave & Imagemagick, using conditions to resize image

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246748

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have been struggling to find any tutorial or question that explains how to upload image and resize it according to certain conditions provided by user.

    I am easily able to upload and resize image using hard coded values, however I am stuck at using user provided parameters to be accessed from the Uploader.

    I want the image to be resized to either 800×600 or 300×300 based on whether the user checks the image as Large or Small.

    For that I have a boolean column named “large” in the model structure.

    In the Uploader I am able to access model and its values easily in the store_dir block, but anywhere outside this block any model attribute returns as nil.

    This is what I want to do:-

    class BannerUploader < CarrierWave::Uploader::Base
        include CarrierWave::MiniMagick
        storage :file
        def store_dir
            "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
        end
        resize_to_fit(800,600) if model.large==true
        resize_to_fit(300,300) if model.large!=true
    end
    

    However this returns the error
    undefined local variable or method `model’ for BannerUploader:Class

    How to go about this issue.

    #246749

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    To process an original file you can specify custom method:

    class BannerUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      storage :file
      def store_dir
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
    
      process :process_original_version
    
      def process_original_version
        if model.large
          resize_to_fit(800,600)
        else
          resize_to_fit(300,300)
        end
      end
    end
    

    For a specific version:

    class BannerUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      storage :file
      def store_dir
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
    
      version :normal do
        if model.large
          process resize_to_fit: [800,600]
        else
          process resize_to_fit: [300,300]
        end
      end
    end
    

    Source: https://stackoverflow.com/questions/47962242/rails-carrierwave-imagemagick-using-conditions-to-resize-image
    Author: Alex Kojin
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.