1

我希望 attachment_fu 以与 flickr、facebook 和 twitter 处理此类似的方式调整我的缩略图大小:如果我想要一个 100x100 的缩略图,我希望缩略图正好是 100x100,并且剪掉任何多余的部分,以便保留纵横比。

有任何想法吗?

4

4 回答 4

1

要设置 100x100 缩略图,请将以下内容添加到您的模型中:

  has_attachment :content_type => :image,
                 :storage => IMAGE_STORAGE,
                 :max_size => 20.megabytes,
                 :thumbnails => {
                   :thumb  => '100x100>',
                   :large  => '800x600>',
                 }

(在这个例子中,我正在创建一个 100x100 的缩略图,以及一个 800x600 的“大”尺寸,除了保持原始尺寸。)

另外,请记住,缩略图可能不完全是 100x100;它的最大尺寸为 100x100。这意味着如果原件的纵横比为 4:3,则缩略图将为 100x75。我不确定这是否是您所说的“精确 100x100 并裁剪掉任何多余部分以保留纵横比”的意思。

于 2010-02-11T15:15:09.087 回答
0

规范中可以给出一个裁剪指令:

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100#'
}

Memonic: '#' 看起来像裁剪工具。

编辑:更正

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100!'
}

以前的方法是用于具有不同符号的回形针。

于 2010-02-11T15:34:52.680 回答
0

将此添加到您的模型中

protected  

  # Override image resizing method  
  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "crop: WxH"  
    if (size.is_a?(String) && size =~ /^crop: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^crop: (\d*)x(\d*)/i)  
      img.crop_resized!($1.to_i, $2.to_i)  
      # We need to save the resized image in the same way the  
      # orignal does.  
      self.temp_path = write_to_temp_file(img.to_blob)  
    else  
      super # Otherwise let attachment_fu handle it  
    end  
  end

并将缩略图大小更改为:

:thumbnails => {:thumb => 'crop: 100x100' }

来源:

http://stuff-things.net/2008/02/21/quick-and-dirty-cropping-images-with-attachment_fu/

于 2010-02-11T15:35:38.230 回答
0

我的解决方案是深入研究 attachment_fu 插件文件夹(供应商/插件)并编辑 rmagick_processor.rb 文件。首先我将 resize_image 重命名为 resize_image_internal,然后添加:

  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "square: WxH"  
    if (size.is_a?(String) && size =~ /^square: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^square: (\d*)x(\d*)/i)  
        iw, ih = img.columns, img.rows
        aspect = iw.to_f / ih.to_f
        if aspect > 1
            shave_off = (iw - ih) / 2
            img.shave!(shave_off, 0)
        else
            shave_off = (ih-iw) / 2
            img.shave!(0, shave_off)
        end
        resize_image_internal(img, "#{$1}x#{$2}!")
    else  
      resize_image_internal(img, size) # Otherwise let attachment_fu handle it  
    end  
  end

我现在可以使用 'square: 100x100' 作为我的几何字符串。请注意,上面的代码假定所需的输出是正方形。

于 2010-02-13T11:34:59.840 回答