保存图像Paperclip中的尺寸(宽度和高度)?

时间:2010-10-31 21:43:06

标签: ruby-on-rails ruby-on-rails-3 paperclip

任何Paperclip向导都知道你是否可以在使用Paperclip保存图像时,还可以将图像尺寸(宽度和高度)保存在2个额外的字段中?如何在Paperclip上传过程中获得此类数据?

6 个答案:

答案 0 :(得分:61)

仅仅为了完整起见,即使先前的答案已经显示出足够好的建议。

您可以使用Paperclip事件处理程序而不是Rails回调。在这种情况下,仅在图像更改时才会重新计算大小。 (如果你使用S3进行存储,这可以节省很多时间)

has_attached_file :image, :styles => ...
after_post_process :save_image_dimensions

def save_image_dimensions
  geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
  self.image_width = geo.width
  self.image_height = geo.height
end

图像甚至不必从S3下载(或从文件中读取),paperclip将其提供给事件处理程序本身。

请参阅the readme事件部分了解详情。

答案 1 :(得分:5)

当用户使用回形针上传图像时,我使用以下模型处理它:

class Picture < ActiveRecord::Base
  has_attached_file :pic, :styles => { :small => "100x100>" }, :whiny => true
  after_save :save_geometry

  def save_geometry
    unless @geometry_saved
      self.original_geometry = get_geometry(:original)
      self.small_geometry = get_geometry(:small)
      @geometry_saved = true
      self.save
    end
  end

  def get_geometry(style = :original)
    begin
      Paperclip::Geometry.from_file(pic.path(style)).to_s
    end
  end
end

get_geometry函数调用ImageMagick identify来查找原始图像和已调整大小的图像的几何图形。

我将结果缓存在数据库字段中。例如,如果我上传了1024x768的图像,则我的缓存字段将包含:

original_geometry = "1024x768"
small_geometry = "100x75"

答案 2 :(得分:0)

您需要'RMagick'

uploaded_image = Magick::Image.read(image).first  #image is what you've specified in paperclip to be your image
width = uploaded_image.columns
height = uploaded_image.rows

不知道如何让它与回调一起使用。也许是这样的:

attr_accessor :write_image_dimensions?
before_save :check_image_changed

def check_image_changed
  self.write_image_dimensions? = image_changed?
end

after_save :write_image_dimensions, :if => :write_image_dimensions?

def write_image_dimensions
  uploaded_image = Magick::Image.read(image).first  #image is what you've specified in paperclip to be your image
  self.width = uploaded_image.columns
  self.height = uploaded_image.rows
  save
end

答案 3 :(得分:0)

使用Rails 4我使用以下关注点来保存图像尺寸:

    module Dimensions

      extend ActiveSupport::Concern

      included do

      end

      module ClassMethods

        def extract_dimensions_for *fields

          define_method(:extract_dimensions_field_list) { fields }

          before_save :extract_dimensions

          fields.each do |f|
            serialize (f.to_s+"_dimensions"), Hash

            class_eval do

              [:width, :height].each do |axis|
                define_method("#{f}_#{axis}") do
                  return send(f.to_s+"_dimensions")[axis]
                end
              end

              define_method("#{f}_dimensions_max") do |width, height=nil|
                dims = send(f.to_s+"_dimensions")
                rw = width.to_f / dims[:width]
                rh = height.to_f / dims[:height] if height
                r = (!height || rw < rh) ? rw : rh
                return {width: (dims[:width] * r).to_i, height: (dims[:height] * r).to_i}
              end

              define_method("#{f}_is_portrait?") do
                dims = send(f.to_s+"_dimensions")
                return dims[:width] <= dims[:height]
              end

              define_method("#{f}_is_landscape?") do
                dims = send(f.to_s+"_dimensions")
                return dims[:width] > dims[:height]
              end

            end

          end

          class_eval do

            def extract_dimensions

              extract_dimensions_field_list.each do |f|

                tempfile = send(f).queued_for_write[:original]
                unless tempfile.nil?
                  geometry = Paperclip::Geometry.from_file(tempfile)
                  self.send(f.to_s+"_dimensions=", {width: geometry.width.to_i, height: geometry.height.to_i})
                end

              end

            end

          end

        end

      end


    end

然后在你的模型中:

...

include Dimensions

extract_dimensions_for :image

...

这会将您的维度保存到名为image_dimensions的序列化字段,并添加一些其他方法image_widthimage_heightimage_dimensions_max(width, height)

答案 4 :(得分:0)

我找到了最简单的解决方案: Fastimage gem (link)

速度快,非常非常简单。 例如:

require 'fastimage'

FastImage.size("/some/local/file.gif")
=> [266, 56]  # width, height
FastImage.type("/some/local/file.gif")
=> :gif

答案 5 :(得分:0)

paperclip-meta gem缓存所有图像样式的图像尺寸和文件大小。它在Paperclip自述文件中引用。 paperclip-meta repo在这里:https://github.com/teeparham/paperclip-meta