0

I have carrierwave and minimagick installed and im trying to resize the image if its portrait or if its landscape, ive followed the carrierwave documentation but something is not working and it is creating both portrait and landscape images. Ive played around with it now for some time and im all out of ideas! Any ideas will appreciated.

Here is my code my :uploader.rb

class CoverUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick

      storage :file
      # storage :fog

      def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end

      version :landscape, if: :is_landscape?


        version :landscape do
        process resize_to_fit: [@land_height, 250]
      end

      version :portrait, if: :is_portrait?


        version :portrait do
          process resize_to_fit: [250, @port_width]
        end


      process :store_dimensions

      def extension_whitelist
        %w(jpg jpeg png)
      end


      private

      def store_dimensions
        if file && model
          model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
        end
      end

      def is_landscape? picture
        image = MiniMagick::Image.open(picture.path)
        image[:width] > image[:height]
        width = image[:width]
        aspect = image[:width] / image[:height].to_f
        @height = aspect * width
      end

      def is_portrait? picture
        image = MiniMagick::Image.open(picture.path)
        image[:width] < image[:height]
        height = image[:height]
        aspect = image[:width] / image[:height].to_f
        @width = height / aspect
      end
    end

So this is creating 3 images instead of 2.

Thanks in advance!

john seymour
  • 213
  • 1
  • 12

1 Answers1

0

Just taking a wild guess here: You're overriding version with similar but different blocks which are omitting your conditional. Clean it up.

version :portrait, :if => :is_portrait? do
  process resize_to_fit: [250, @port_width]
end

version :landscape, :if => :is_landscape? do
  process resize_to_fit: [@land_height, 250]
end
Josh Brody
  • 5,153
  • 1
  • 14
  • 25
  • Hi Josh, thanks for replying. the problem was in the methods, i should have had ' image[:width] > image[:height] ' at the bottom as it wasnt returning true or false. – john seymour Jan 09 '18 at 14:40