2010-02-11 38 views
1

調整我想attachment_fu調整我的縮略圖以類似的方式如何的Flickr,Facebook和Twitter的處理這個問題:如果我想有一個100x100的縮略圖我想縮略圖是完全100x100的任何多餘的剪裁掉,這樣長寬比被保留。Flickr的風格attachment_fu

任何想法?

回答

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 

我現在可以使用「方:100x100的」我的幾何字符串。請注意,上面的代碼假定所需的輸出是平方的。

1

要設置100x100的縮略圖,添加以下模型:

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

(在這個例子中,我創建一個100x100的縮略圖,也是一個800x600的「大」尺寸,額外的飼養。原來的大小)

另外,請記住,縮略圖可能不完全100×100;它的最大尺寸爲100x100。這意味着如果原件的寬高比爲4:3,則縮略圖將爲100x75。我不能完全肯定,如果這是你的意思「究竟100x100的任何多餘的,這樣的長寬比保留裁剪掉。」

+0

的>修改只是設置最大尺寸>我要的是保存下來,不管什麼原始圖像尺寸100×100究竟有縱橫比的圖像。 – 2010-02-11 21:30:04

+0

@Mike薩頓:我有點糊塗了......如果輸入圖像不成方形什麼?您如何獲得精確的100x100縮略圖,同時保留寬高比?假設你有一個1000x750的輸入圖像。結果應該是什麼? – pkaeding 2010-02-11 21:59:07

+0

多餘部分應裁剪掉,以使圖像平整。 – 2010-02-12 18:42:12

0

有可以在說明書中給出的裁剪指令:

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

Memonic:「#」看起來像裁剪工具。

編輯:糾錯

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

先前方法對於回形針具有不同的符號。

+0

「未知幾何字符串」錯誤 – 2010-02-11 21:28:25

+0

也許這就是Paperclip比attachment_fu更好的地方。嗯。 – tadman 2010-02-12 16:34:16

+0

其實,我看過一些舊的attachment_fu東西,我用了'100x100!'指定裁剪。 – tadman 2010-02-12 16:36:49

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/

+0

不適用於模型。我試着編輯插件本身,但它仍然不能保持寬高比。 – 2010-02-11 21:28:00