开发者

RMagick Rounded Corners

开发者 https://www.devze.com 2022-12-28 00:58 出处:网络
Need a simple a way of rounding off an Image. I need the corners to be transparent. This link shows how to do it via command line:

Need a simple a way of rounding off an Image. I need the corners to be transparent. This link shows how to do it via command line:

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

What I need is the corresponding RMagick\开发者_Python百科Ruby code... Thanks!


Include Rmagick in your source code. Be sure to place the include inside the class declaration.

require 'rmagick'
include Magick

Create a method, like this

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

Call the method like this

source_image = Image.read('my-big-image.jpg').first
thumbnail_image = thumb(source_image, '64x64>', 8)
thumbnail_image.write('thumb.png')

I structured it this way because I already have the image open for another purpose at the point I'm creating the thumbnail. It might make more sense for you to put the file operations right in the method.

Also, you might want to look at how geometry strings work http://www.imagemagick.org/RMagick/doc/imusage.html#geometry


Using Fitter Man's code with CarrierWave::RMagick

Method:

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

Usage:

process :resize_and_round => ['200x200', 20]


If you are using paperclip, check http://loo.no/articles/rounded-corners-with-paperclip


In general I've had such poor luck with RMagick that I generally find it easier to just do a system() call with the command in it to modify the images. If you took that approach you could use exactly the command in the link you referenced.


This is a method for using CarrierWave and RMagick to create rounded corners on images:

http://dmathieu.com/articles/development/create-your-own-carrierwave-processors/

0

精彩评论

暂无评论...
验证码 换一张
取 消