Reputation:
I'm creating a little photo sharing site for our home's intranet, and I have an upload feature, which uploads the photo at original size into the database. However, I also want to save the photo in four other sizes: W=1024, W=512, W=256 and W=128, but only the sizes smaller than the original size (e.g. if the original width is 511, only generate 256 and 128). The image with a width of 128 should always be generated (because it's a thumbnail). Also, the resization should always be with a proportional width and height. How can I implement this? I already have this code to upload the photo:
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
# here it should generate the smaller sizes
#+and save them to self.binary_data_1024, etc...
end
<h1>New pic</h1>
<% form_for(@pic, :html => {:multipart => true}) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<p>
<%= f.label :image_file %><br />
<%= f.file_field :image_file %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', pics_path %>
Thanks
Upvotes: 14
Views: 37635
Reputation: 7164
You can resize with RMagick gem.
This is just a example that you can adapt:
require 'RMagick'
f = File.new( File.join(save_path, self.filename), "wb" )
f.write form_file.read #remeber to set :html=>{:multipart => true} in form
f.close
image = Magick::Image.read(self.filename).first
image.change_geometry!("640x480") { |cols, rows, img|
newimg = img.resize(cols, rows)
newimg.write("newfilename.jpg")
}
More Info here: http://www.imagemagick.org/script/api.php#ruby
Upvotes: 27
Reputation: 391
There is a gem called "Refile". It is great. Go ahead and check this tutorial on how to use it. https://gorails.com/episodes/file-uploads-with-refile here is how to do it.
Add this to your gem file
gem 'refile', '~> 0.4.2', require: ["refile/rails", "refile/image_processing"]
Create a table field using rails migration in your table as image_id of string type.Now comes how to insert into that field and display the image.
Use this in your upload form, largely form_for do |f|
<%= f.attachment_field :image %>
if you are using rails 4 make sure you pass rails strong parameters.
Put this in your model.rb file where you are storing the image (below class modelname)
attachment :image
displaying the image is simple.
<%= image_tag attachment_url(current_user,:image, :fill, 200, 170, format: "jpg") %>
Upvotes: 1
Reputation: 3156
I tried following way. Its working fine. I hope it will help to some one.
1.add following gem in Gemfile:
gem "ImageResize", "~> 0.0.5"
2.run bundle
3.use this in controller function:
require 'rubygems'
require 'ImageResize'
#input_image_filename, output_image_filename, max_width, max_height
Image.resize('big.jpg', 'small.jpg', 40, 40)
Upvotes: 3
Reputation: 523
Knowing this is an old one, but here is a very nice railscast according to this topic: http://railscasts.com/episodes/253-carrierwave-file-uploads It is using rmagic and carrierwave.
Upvotes: 2