我希望Attachment_fu以与Flickr,Facebook和Twitter的处理方式相似的方式调整我的缩略图:如果我希望100x100缩略图,我希望缩略图完全100x100,并保留了多余的裁剪,以保留纵横比。

有任何想法吗?

有帮助吗?

解决方案 4

我的解决方案是深入探究attactment_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

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

其他提示

要设置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,多余的裁剪,以保留纵横比”。

将其添加到您的模型中

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-images-with-attachment_fu/

规格中可以给出一个种植指令:

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

memonic:'#'看起来像农作物工具。

编辑: 更正

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

先前的方法是用于纸条,其符号不同。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top