【问题标题】:Basic image resizing in Ruby on RailsRuby on Rails 中的基本图像大小调整
【发布时间】:2011-01-27 09:17:53
【问题描述】:

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

pic.rb
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
<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 %>

谢谢

【问题讨论】:

    标签: ruby-on-rails image-processing upload


    【解决方案1】:

    您可以使用 RMagick gem 调整大小。

    这只是一个你可以适应的例子:

    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")
    }
    

    更多信息在这里:http://www.imagemagick.org/script/api.php#ruby

    【讨论】:

      【解决方案2】:

      有一个 gem 叫做“Refile”。太好了。继续查看本教程以了解如何使用它。 https://gorails.com/episodes/file-uploads-with-refile 这是怎么做的。

      将此添加到您的 gem 文件中

      gem 'refile', '~> 0.4.2', require: ["refile/rails", "refile/image_processing"]
      

      在你的表中使用rails迁移创建一个表字段作为字符串类型的image_id。现在介绍如何插入该字段并显示图像。

      在您的上传表单中使用它,主要是 form_for do |f|

       <%= f.attachment_field :image %>
      

      如果您使用的是 rails 4,请确保您传递 rails strong 参数。

      把它放在你存储图像的 model.rb 文件中(在类模型名下面)

      attachment :image
      

      显示图像很简单。

      <%= image_tag attachment_url(current_user,:image, :fill, 200, 170, format: "jpg") %>
      

      【讨论】:

        【解决方案3】:

        我尝试了以下方式。它工作正常。 希望对大家有所帮助。

        1.在 Gemfile 中添加以下 gem:

        gem "ImageResize", "~> 0.0.5"
        

        2.运行包

        3.在控制器功能中使用:

        require 'rubygems'
        require 'ImageResize'
        
        #input_image_filename, output_image_filename, max_width, max_height
        Image.resize('big.jpg', 'small.jpg', 40, 40)
        

        【讨论】:

          【解决方案4】:

          知道这是一个旧的,但是根据这个主题,这是一个非常好的 railscast:http://railscasts.com/episodes/253-carrierwave-file-uploads 它正在使用 rmagic 和载波。

          【讨论】:

            【解决方案5】:

            只需使用paperclipattachment_fu

            【讨论】:

              猜你喜欢
              • 2012-01-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多