Ruby resize images ignore proportions (stretch or shrink)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi there,

I would like a ruby resize script.

I'm currently using MojoMagick but it crops the image as I would like it to stretch or shrink the image to the full size and not worry about the proportions.

MojoMagick::resize(image_splash_local,full_dir + '/drawable-port-ldpi/splash.png', {:width=>200, :height=>320, :fill => false, :crop => false})

Thanks
- Dave

Crowdsource coding tasks.

2 Solutions


I usually use RMagick gem to do image manipulations.

full_path = 'path/to/file'
image = ::Magick::Image::read(full_path).first 
image = image.first.resize(x, y)
image.write('path/to/file')

Being x, y the values for width and height

With a more elegant solution and several other options.

image.combine_options do |i|
  i.resize "2048x1536"
  i.flip
  i.rotate "-45"
  i.blur "0x15"
end
image.write "path/to/file"

require 'mojo_magick'

def resizeExact(src, dst, width, height)
  MojoMagick::raw_command('convert', "#{src} -resize #{width}x#{height}! #{dst}")
end

resizeExact 'test.jpg', 'test-resized.jpg', 200, 300
View Timeline