lib/carrierwave/processing/rmagick.rb in carrierwave-0.2.3 vs lib/carrierwave/processing/rmagick.rb in carrierwave-0.2.4

- old
+ new

@@ -87,10 +87,37 @@ img end end ## + # Resize the image to fit within the specified dimensions while retaining + # the original aspect ratio. Will only resize the image if it is larger than the + # specified dimensions. The resulting image may be shorter or narrower than specified + # in the smaller dimension but will not be larger than the specified values. + # + # === Parameters + # + # [width (Integer)] the width to scale the image to + # [height (Integer)] the height to scale the image to + # + # === Yields + # + # [Magick::Image] additional manipulations to perform + # + def resize_to_limit(width, height) + manipulate! do |img| + geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry) + new_img = img.change_geometry(geometry) do |new_width, new_height| + img.resize(new_width, new_height) + end + destroy_image(img) + new_img = yield(new_img) if block_given? + new_img + end + end + + ## # From the RMagick documentation: "Resize the image to fit within the # specified dimensions while retaining the original aspect ratio. The # image may be shorter or narrower than specified in the smaller dimension # but will not be larger than the specified values." # @@ -165,13 +192,13 @@ if background == :transparent filled = new_img.matte_floodfill(1, 1) else filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background)) end - new_img.destroy! + destroy_image(new_img) filled.composite!(img, gravity, ::Magick::OverCompositeOp) - img.destroy! + destroy_image(img) filled = yield(filled) if block_given? filled end end @@ -202,17 +229,23 @@ list = ::Magick::ImageList.new image.each do |frame| list << yield( frame ) end list.write(current_path) - list.destroy! + destroy_image(list) else frame = image.first yield( frame ).write(current_path) - frame.destroy! + destroy_image(frame) end rescue ::Magick::ImageMagickError => e raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}") + end + + private + + def destroy_image(image) + image.destroy! if image.respond_to?(:destroy!) end end # RMagick end # CarrierWave