Ruby on Rails中的基本图像大小调整 [英] Basic image resizing in Ruby on Rails

查看:90
本文介绍了Ruby on Rails中的基本图像大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们家庭的内部网创建一个小型照片共享网站,我有一个上传功能,可以将原始大小的照片上传到数据库中。但是,我还想将照片保存为其他四种尺寸:W = 1024,W = 512,W = 256和W = 128,但只有小于原始尺寸的尺寸(例如,如果原始宽度为511,则仅生成256和128)。应始终生成宽度为128的图像(因为它是缩略图)。此外,重新调整应始终具有成比例的宽度和高度。我该如何实现呢?
我已经有了上传照片的代码:

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



new.rb< - view



new.rb <-- view

<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 %>

谢谢

推荐答案

只需使用回形针 attachment_fu

这篇关于Ruby on Rails中的基本图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆