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!