flickr、facebook、twitter がこれを処理する方法と同様の方法で、attachment_fu でサムネイルのサイズを変更したいと考えています。
何か案は?
flickr、facebook、twitter がこれを処理する方法と同様の方法で、attachment_fu でサムネイルのサイズを変更したいと考えています。
何か案は?
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」の意味であるかどうかは正確にはわかりません。
仕様で指定できるトリミングディレクティブがあります。
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100#'
}
メモ:「#」は切り抜きツールのように見えます。
編集:訂正
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '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-with-attachment_fu/
私の解決策は、attachment_fu プラグイン フォルダー (vendor/plugins) を調べて、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' をジオメトリ文字列として使用できるようになりました。上記のコードは、必要な出力が正方形であると想定していることに注意してください。