eltiare (owner)

Revisions

gist: 50647 Download_button fork
public
Public Clone URL: git://gist.github.com/50647.git
spriter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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