需要一个简单的四舍五入的图像方式。我需要角落是透明的。此链接显示了如何通过命令行执行此操作:
http://www.imagemagick.org/Usage/thumbnails/#rounded
我需要的是相应的RMagick\Ruby代码...谢谢!
发布于 2010-04-27 04:40:19
如果您正在使用回形针,请检查http://loo.no/articles/rounded-corners-with-paperclip
发布于 2011-09-17 05:01:52
在源代码中包含Rmagick。请确保将include放在类声明中。
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中工作
发布于 2012-10-26 06:11:23
在CarrierWave::RMagick中使用Fitter Man的代码
方法:
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]https://stackoverflow.com/questions/2716457
复制相似问题