首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RMagick圆角

RMagick圆角
EN

Stack Overflow用户
提问于 2010-04-27 03:59:08
回答 5查看 2.4K关注 0票数 5

需要一个简单的四舍五入的图像方式。我需要角落是透明的。此链接显示了如何通过命令行执行此操作:

http://www.imagemagick.org/Usage/thumbnails/#rounded

我需要的是相应的RMagick\Ruby代码...谢谢!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-04-27 04:40:19

如果您正在使用回形针,请检查http://loo.no/articles/rounded-corners-with-paperclip

票数 3
EN

Stack Overflow用户

发布于 2011-09-17 05:01:52

在源代码中包含Rmagick。请确保将include放在类声明中。

代码语言:javascript
复制
require 'rmagick'
include Magick

创建一个方法,如下所示

代码语言:javascript
复制
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

像这样调用该方法

代码语言:javascript
复制
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中工作

票数 11
EN

Stack Overflow用户

发布于 2012-10-26 06:11:23

CarrierWave::RMagick中使用Fitter Man的代码

方法:

代码语言:javascript
复制
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

使用:

代码语言:javascript
复制
process :resize_and_round => ['200x200', 20]
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2716457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档