spritey 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/spritey +36 -0
- data/lib/spritey.rb +81 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8103d7b2d6c8dc5f905a0a65369befeac96063e2
|
4
|
+
data.tar.gz: d53fd375fc00efedb28371adb3bb57c4da9d68a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b0b9be7db13ee128de8edf971a97a31c53e7d1b1eee61d81fae6c68a23891784624050d3fab55da38214d38be2329e95e1a398b978add65f3352a63da532705a
|
7
|
+
data.tar.gz: b3395fca07e707d112f3f18240e504aeb4c327dd158a120f98147e25d8c1b7a2c48be0f52e45ac745e12a963e531445bb33cbc0a3306a0db96459a1573fd4b09
|
data/bin/spritey
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'spritey'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
@concat = Spritey::Spritey.new
|
7
|
+
options = {}
|
8
|
+
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = "Usage: example.rb [options]"
|
11
|
+
|
12
|
+
opts.on("-o", "--output FILENAME", "specify the output file") do |v|
|
13
|
+
options[:output] = v
|
14
|
+
end
|
15
|
+
opts.on("-i", "--input FILENAMES,", "specify the input file/s separated by comma") do |v|
|
16
|
+
options[:input] = v.split(",")
|
17
|
+
end
|
18
|
+
|
19
|
+
end.parse!
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
options[:input].each do |arg|
|
24
|
+
begin
|
25
|
+
@concat.add_image(arg)
|
26
|
+
rescue Magick::ImageMagickError
|
27
|
+
puts "failed to load image #{arg}"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
@concat.save_css options[:output]
|
data/lib/spritey.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rmagick'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
module Spritey
|
5
|
+
class Spritey
|
6
|
+
attr_accessor :images,:max_height
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@images = []
|
10
|
+
@max_height = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_image(filename)
|
14
|
+
image = load_image(filename)
|
15
|
+
@max_height = image.rows unless image.rows < @max_height
|
16
|
+
@images.push image
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
def save_css(filename)
|
22
|
+
concatenate(filename + ".png")
|
23
|
+
css_file = File.open(filename +".css", "w")
|
24
|
+
css_file.write generate_sprites_css(filename)
|
25
|
+
css_file.close
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_image(filename)
|
29
|
+
Magick::Image::read(filename)[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
def concatenate(dest_image_filename)
|
33
|
+
raise RuntimeError unless @images.any?
|
34
|
+
|
35
|
+
new_image = Magick::Image.new(total_width,@max_height){
|
36
|
+
self.format = 'png'
|
37
|
+
self.background_color = 'none'
|
38
|
+
}
|
39
|
+
|
40
|
+
last_width = 0
|
41
|
+
|
42
|
+
@images.each do |img|
|
43
|
+
img.alpha Magick::BackgroundAlphaChannel
|
44
|
+
new_image.composite!(img,last_width, 0, Magick::OverCompositeOp )
|
45
|
+
last_width += img.columns
|
46
|
+
end
|
47
|
+
puts "Total image size #{total_width},#{@max_height}"
|
48
|
+
new_image.write(dest_image_filename)
|
49
|
+
end
|
50
|
+
|
51
|
+
def generate_sprites_css(filename)
|
52
|
+
last_width = 0
|
53
|
+
dest_css = ""
|
54
|
+
@images.each do |img|
|
55
|
+
image_name = Pathname.new(img.filename).basename.to_s.split(".")
|
56
|
+
dest_css += "##{image_name[0]} {"
|
57
|
+
dest_css += "display:block;"
|
58
|
+
dest_css += "width:#{img.columns}px;"
|
59
|
+
dest_css += "height:#{img.rows}px;"
|
60
|
+
dest_css += "background-image:url(#{filename}.png);"
|
61
|
+
dest_css += "background-repeat:no-repeat;"
|
62
|
+
dest_css += "background-position: -#{last_width}px 0px ;"
|
63
|
+
dest_css += "}\n"
|
64
|
+
last_width += img.columns
|
65
|
+
end
|
66
|
+
dest_css
|
67
|
+
end
|
68
|
+
|
69
|
+
def total_width
|
70
|
+
width = 0
|
71
|
+
@images.each do |image|
|
72
|
+
width += image.columns
|
73
|
+
end
|
74
|
+
width
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spritey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juan GiménezSilva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Tired of writing CSS sprites by hand? well this simple gem will make
|
14
|
+
it easy
|
15
|
+
email: juanfgs@gmail.com
|
16
|
+
executables:
|
17
|
+
- spritey
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/spritey
|
22
|
+
- lib/spritey.rb
|
23
|
+
homepage: http://juanfgs.eosweb.info
|
24
|
+
licenses:
|
25
|
+
- GPL 3.0
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.5
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: A simple sprite generator
|
47
|
+
test_files: []
|