2011-01-13 22 views
2

画像のサイズを変更したいオリジナルのサイズは300x200または512x600と同じではありません。画像を100x100にリサイズしますが、画像や変化率から何も作っていません。理想的には、画像は最初に長辺を100(アスペクト比)にスケールし、次に小さな辺を白で塗りつぶします。RMagick:サムネイルの画像を拡大/縮小する

.---------. 
|- - - - -| 
| IMAGE | 
|- - - - -| 
'---------' 

私はPaperclipまたはRailsを使用しません。ただRMagickです。

答えて

5

サイズ変更した画像と新しい100x100画像をマージしました。最善の方法がわからないため厥が、それは動作します:

img = Magick::Image.read("file.png").first 
target = Magick::Image.new(100, 100) do 
    self.background_color = 'white' 
end 
img.resize_to_fit!(100, 100) 
target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write("file-small.png) 
0

あなたが、私はFu86の複合トリックはそうのように動作するようになったしばらくそれで遊んで後change_geometry ...

1

を使用したいようだ:

img = Image.read("some_file").first().resize_to_fit!(width, height) 
target = Image.new(width, height) do 
    self.background_color = 'white' 
end 
target.composite(img, CenterGravity, AtopCompositeOp).write("some_new_file") 

AtopCompositeOp何らかの理由で私の背景の黒の部分を回しCopyCompositeOp、より良い動作しているようです。

1
image = Magick::Image.read("filename").first 
resized = image.resize_to_fit(width, height)  # will maintain aspect ratio, so one of the resized dimensions may be less than the specified dimensions 
resized.background_color = "#FFFFFF"    # without a default, background color will vary based on the border of your original image 
x = (resized.columns - width)/2    # calculate necessary translation to center image on background 
y = (resized.rows - height)/2 
resized = resized.extent(width, height, x, y) # 'extent' fills out the resized image if necessary, with the background color, to match the full requested dimensions. the x and y parameters calculated in the previous step center the image on the background. 
resized.write("new_filename") 

注:この投稿のようにImageMagickの6.5.7-8を使用Herokuの上に、私は-1 xとyの翻訳を乗算(正の数字を送信する)ために必要。バージョン6.8.0-10では負の数が必要です。

関連する問題