mik*_*gto 4 ruby ruby-on-rails rmagick
需要一种简单的方法来舍入图像.我需要角落是透明的.此链接显示如何通过命令行执行此操作:
http://www.imagemagick.org/Usage/thumbnails/#rounded
我需要的是相应的RMagick\Ruby代码......谢谢!
Fit*_*Man 10
在您的源代码中包含Rmagick.务必将include放在类声明中.
require 'rmagick'
include Magick
Run Code Online (Sandbox Code Playgroud)
创建一个像这样的方法
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
Run Code Online (Sandbox Code Playgroud)
像这样调用方法
source_image = Image.read('my-big-image.jpg').first
thumbnail_image = thumb(source_image, '64x64>', 8)
thumbnail_image.write('thumb.png')
Run Code Online (Sandbox Code Playgroud)
我以这种方式构建它,因为我已经将图像打开以用于另一个目的,我正在创建缩略图.将文件操作放在方法中可能更有意义.
此外,您可能希望了解几何字符串的工作原理http://www.imagemagick.org/RMagick/doc/imusage.html#geometry