class PsdToSprite::SpriteMaker

Attributes

frame_height[R]
frame_width[R]
layers[R]
path[R]
psd[R]

Public Class Methods

new(psd_path) click to toggle source
# File lib/psd_to_sprite.rb, line 10
def initialize(psd_path)
  @path = Pathname.new(psd_path)
  @psd = PSD.new(psd_path)
end

Public Instance Methods

process(output_path = nil) click to toggle source
# File lib/psd_to_sprite.rb, line 15
def process(output_path = nil)
  psd.parse!
  root_node = psd.tree
  @frame_height = root_node.height
  @frame_width = root_node.width
  @layers = psd.layers

  output_width = root_node.children.count * frame_width

  img  = save_layers_to_new_image(output_width, frame_height)

  return img.write(output_path) if output_path
  img.write("#{path.dirname}/#{filename_without_ext}.png")
end

Private Instance Methods

filename_without_ext() click to toggle source
# File lib/psd_to_sprite.rb, line 31
def filename_without_ext
  path.basename.to_s.gsub(path.extname, "")
end
save_layers_to_new_image(output_width, output_height) click to toggle source
# File lib/psd_to_sprite.rb, line 35
def save_layers_to_new_image(output_width, output_height)
  img = Magick::Image.new(output_width, output_height) do
    self.background_color = "transparent"
  end

  x = 0
  y = 0

  layers.reverse.each do |layer|
    frame = layer.image.to_png
    frame_img = Magick::Image.from_blob(frame.to_s).first

    img = img.composite(frame_img, x + layer.left, layer.top, Magick::AddCompositeOp)
    x += frame_width
  end
  img
end