Permalink
Browse files

added vertical and horizontal files with readme

  • Loading branch information...
0 parents commit c122c5c65c10829c314bf8b6eaaf106af0363db9 @pacifists committed Oct 7, 2009
Showing with 163 additions and 0 deletions.
  1. +9 −0 README
  2. +77 −0 horizontal.rb
  3. +77 −0 vertical.rb
9 README
@@ -0,0 +1,9 @@
+Sprite maker is a ruby script that concatenates images together to make a single image. It's then easy to use the CSS Sprites technique to place images in a web page. Upon completion, the location of each image is output along with the image itself.
+
+The format of the output image is automatically detected using the extension of the output filename argument.
+
+Sprite maker will work with different image types. If you have markedly different images, or images with transparency, you will probably want to group images of the same type in the same image.
+
+Author: Alex Pooley
+Email: ampooley@gmail.com
+Web: http://www.alexpooley.com
@@ -0,0 +1,77 @@
+#!/usr/bin/env ruby
+#
+# Build a concatenated list of images. Intended to build a sprite suitable
+# for use with the CSS sprite technique.
+#
+# The image grows from left to right.
+#
+# See this: http://www.alistapart.com/articles/sprites
+
+require 'rubygems'
+require 'rmagick'
+
+include Magick
+
+usage =<<-EOD
+ Usage: #{$0} <output_image.ext> <image 1> <image 2> ... <image n>
+
+ The output image must specify an extension!
+EOD
+
+puts usage and return if ARGV.length < 2
+
+class Sprite
+ ImageDetails = Struct.new(:filename, :x, :y, :width, :height)
+
+ def initialize
+ @image = Image.new(0,0)
+ @image_details = []
+ end
+
+ def <<(filename)
+ new_image = Image.read(filename).pop
+ @image_details << ImageDetails.new(
+ filename,
+ @image.columns,
+ 0,
+ new_image.columns,
+ new_image.rows
+ )
+ # Grow the container image to accomodate the new image dimensions.
+ required_cols = new_image.columns + @image.columns
+ required_rows = new_image.rows > @image.rows ? new_image.rows : @image.rows
+ # FIXME resize creates a memory allocation error? OSX.
+ #@image.resize!(required_cols, required_rows)
+ # Resize the container by creating a new image with the required dimensions
+ # and then splatting the container image at 0,0.
+ container = @image.copy
+ @image = Image.new(required_cols, required_rows)
+ @image.composite!(container, 0, 0, OverCompositeOp)
+ # Paste the new image in to the container.
+ @image.composite!(new_image, container.columns, 0, OverCompositeOp)
+ end
+
+ # Print details as to where each image is located, and it's dimensions.
+ def to_s
+ order = [:x, :y, :width, :height, :filename]
+ puts order.join("\t")
+ puts '-' * 40
+ @image_details.each do |details|
+ # Print the dtails in the order required.
+ values = order.clone
+ values.map! {|v| details.send(v)}
+ puts values.join("\t")
+ end
+ end
+
+ def write(filename)
+ @image.write(filename)
+ end
+end
+
+
+sprite = Sprite.new
+output_filename = ARGV.shift
+ARGV.each {|filename| sprite << filename}
+sprite.write(output_filename)
+sprite.to_s
@@ -0,0 +1,77 @@
+#!/usr/bin/env ruby
+#
+# Build a concatenated list of images. Intended to build a sprite suitable
+# for use with the CSS sprite technique.
+#
+# The image grows from left to right.
+#
+# See this: http://www.alistapart.com/articles/sprites
+
+require 'rubygems'
+require 'rmagick'
+
+include Magick
+
+usage =<<-EOD
+ Usage: #{$0} <output_image.ext> <image 1> <image 2> ... <image n>
+
+ The output image must specify an extension!
+EOD
+
+puts usage and return if ARGV.length < 2
+
+class Sprite
+ ImageDetails = Struct.new(:filename, :x, :y, :width, :height)
+
+ def initialize
+ @image = Image.new(0,0)
+ @image_details = []
+ end
+
+ def <<(filename)
+ new_image = Image.read(filename).pop
+ @image_details << ImageDetails.new(
+ filename,
+ 0,
+ @image.rows,
+ new_image.columns,
+ new_image.rows
+ )
+ # Grow the container image to accomodate the new image dimensions.
+ required_cols = new_image.columns > @image.columns ? new_image.columns : @image.columns
+ required_rows = new_image.rows + @image.rows
+ # FIXME resize creates a memory allocation error? OSX.
+ #@image.resize!(required_cols, required_rows)
+ # Resize the container by creating a new image with the required dimensions
+ # and then splatting the container image at 0,0.
+ container = @image.copy
+ @image = Image.new(required_cols, required_rows)
+ @image.composite!(container, 0, 0, OverCompositeOp)
+ # Paste the new image in to the container.
+ @image.composite!(new_image, 0, container.rows, OverCompositeOp)
+ end
+
+ # Print details as to where each image is located, and it's dimensions.
+ def to_s
+ order = [:x, :y, :width, :height, :filename]
+ puts order.join("\t")
+ puts '-' * 40
+ @image_details.each do |details|
+ # Print the dtails in the order required.
+ values = order.clone
+ values.map! {|v| details.send(v)}
+ puts values.join("\t")
+ end
+ end
+
+ def write(filename)
+ @image.write(filename)
+ end
+end
+
+
+sprite = Sprite.new
+output_filename = ARGV.shift
+ARGV.each {|filename| sprite << filename}
+sprite.write(output_filename)
+sprite.to_s

0 comments on commit c122c5c

Please sign in to comment.