Class: SmartImage::RMagickCanvas

Inherits:
BaseCanvas show all
Includes:
Magick
Defined in:
lib/smart_image/rmagick_canvas.rb

Overview

Canvas object, backed by RMagick

Instance Method Summary collapse

Methods inherited from BaseCanvas

#destroyed?

Constructor Details

#initialize(width, height) ⇒ RMagickCanvas

Returns a new instance of RMagickCanvas.



9
10
11
12
13
# File 'lib/smart_image/rmagick_canvas.rb', line 9

def initialize(width, height)
  @canvas = Image.new width, height do
    self.background_color = "transparent"
  end
end

Instance Method Details

#alpha_mask(image_data, options = {}) ⇒ Object

Load the given file as an alpha mask for the image



39
40
41
42
43
44
45
46
# File 'lib/smart_image/rmagick_canvas.rb', line 39

def alpha_mask(image_data, options = {})
  mask = ImageList.new
  mask.from_blob image_data
  
  # Disable this image's alpha channel to use the opacity data as a mask
  mask.matte = false
  @canvas.composite! mask, NorthWestGravity, CopyOpacityCompositeOp
end

#composite(image_data, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smart_image/rmagick_canvas.rb', line 19

def composite(image_data, options = {})
  image = ImageList.new
  image.from_blob image_data
  
  opts = {
    :width => image.columns,
    :height => image.rows,
    :x => 0,
    :y => 0
  }.merge(options)      
  
  image.thumbnail! opts[:width], opts[:height]
  begin
  	@canvas.composite! image, opts[:x], opts[:y], OverCompositeOp
  ensure
    image.destroy!
  end
end

#destroyObject



15
16
17
# File 'lib/smart_image/rmagick_canvas.rb', line 15

def destroy
  @canvas.destroy!
end

#encode(format, options = {}) ⇒ Object

Encode this image into the given format (as a file extension)



49
50
51
52
# File 'lib/smart_image/rmagick_canvas.rb', line 49

def encode(format, options = {})
  @canvas.format = format.to_s.upcase
  @canvas.to_blob
end