画像を四捨五入する簡単な方法が必要です。角を透明にする必要があります。このリンクは、コマンドラインを介してそれを行う方法を示しています。
http://www.imagemagick.org/Usage/thumbnails/#rounded
必要なのは、対応するRMagick \ Rubyコードです...ありがとう!
画像を四捨五入する簡単な方法が必要です。角を透明にする必要があります。このリンクは、コマンドラインを介してそれを行う方法を示しています。
http://www.imagemagick.org/Usage/thumbnails/#rounded
必要なのは、対応するRMagick \ Rubyコードです...ありがとう!
ソースコードにRmagickを含めます。必ずクラス宣言内にインクルードを配置してください。
require 'rmagick'
include Magick
このようなメソッドを作成します
def thumb(source_image, geometry_string, radius = 10)
source_image.change_geometry(geometry_string) do |cols, rows, img|
# Make a resized copy of the image
thumb = img.resize(cols, rows)
# Set a transparent background: pixels that are transparent will be
# discarded from the source image.
mask = Image.new(cols, rows) {self.background_color = 'transparent'}
# Create a white rectangle with rounded corners. This will become the
# mask for the area you want to retain in the original image.
Draw.new.stroke('none').stroke_width(0).fill('white').
roundrectangle(0, 0, cols, rows, radius, radius).
draw(mask)
# Apply the mask and write it out
thumb.composite!(mask, 0, 0, Magick::CopyOpacityCompositeOp)
thumb
end
end
このようなメソッドを呼び出す
source_image = Image.read('my-big-image.jpg').first
thumbnail_image = thumb(source_image, '64x64>', 8)
thumbnail_image.write('thumb.png')
サムネイルを作成している時点で、別の目的で画像を既に開いているため、このように構成しました。ファイル操作をメソッドに正しく配置する方が理にかなっている場合があります。
また、ジオメトリ文字列がどのように機能するかを確認することもできますhttp://www.imagemagick.org/RMagick/doc/imusage.html#geometry
FitterManのコードをCarrierWave::RMagick
方法:
def resize_and_round(geometry_string, radius = 10)
manipulate! do |original|
original.change_geometry(geometry_string) do |cols, rows, img|
# Make a resized copy of the image
thumb = img.resize(cols, rows)
# Set a transparent background: pixels that are transparent will be
# discarded from the source image.
mask = Magick::Image.new(cols, rows) {self.background_color = 'transparent'}
# Create a white rectangle with rounded corners. This will become the
# mask for the area you want to retain in the original image.
Magick::Draw.new.stroke('none').stroke_width(0).fill('white').
roundrectangle(0, 0, cols, rows, radius, radius).
draw(mask)
# Apply the mask and write it out
thumb.composite!(mask, 4,4, Magick::CopyOpacityCompositeOp)
thumb
end
end
end
使用法:
process :resize_and_round => ['200x200', 20]
ペーパークリップを使用している場合は、http://loo.no/articles/rounded-corners-with-paperclipを確認してください。
一般的に、私はRMagickで運が悪かったので、画像を変更するためにコマンドを含むsystem()呼び出しを実行する方が一般的に簡単であることがわかりました。そのアプローチを採用した場合は、参照したリンクのコマンドを正確に使用できます。
これは、CarrierWaveとRMagickを使用して画像に丸みを帯びた角を作成する方法です。
http://dmathieu.com/articles/development/create-your-own-carrierwave-processors/