Created
January 22, 2009 18:16
-
-
Save eltiare/50647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# A quick and dirty image combiner for CSS sprites | |
# Name your files something along the lines of file_name_1.jpg (all with same extensions, | |
# including casing if important on your OS) and this script will combine them in order, | |
# create a directory named 'sprites' and put the resulting CSS sprite image in that dir. | |
# Run this script in the directory that contains files that you want to combine. Takes | |
# one argument, specifying the highest number that the script will go up to. It defaults to 3. | |
# So if you have # 4 images for each sprite, simply pass the number 4 to the script like so: | |
# ./spriter.rb 4 | |
# Requires the Imagick libraries to be installed along with the rmagick gem. | |
require 'RMagick' | |
include Magick | |
file_bases = {} | |
extensions = ['jpg', 'gif', 'png', 'jpeg'] | |
num_in_sprite = ARGV[0] ? ARGV[0].to_i : 3 | |
Dir.entries('.').each do |file| | |
# Files that have a format of (filename_number.ext) | |
if (file =~ %r"^(.*)_[1-9][0-9]*\.(#{extensions.join('|')})$"i) | |
file_bases[$1] ||= $2 | |
end | |
end | |
Dir.mkdir('./sprites') unless File.directory?('./sprites') | |
file_bases.each do |base, format| | |
sprite = ImageList.new | |
1.upto(num_in_sprite) { |i| sprite << Image::read("#{base}_#{i}\.#{format}").first } | |
sprite = sprite.append(true) | |
sprite.write(File.join('./sprites',"#{base}.#{format}") ) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment