image_size 1.1.5 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/image_size.gemspec +1 -1
- data/lib/image_size.rb +39 -1
- data/spec/image_size_spec.rb +1 -0
- data/spec/test.svg +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Y2YyMGZmNzllODAzMjlhMzRlYWNhZTBkOWQ3OWRlODViMmY4YTliNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTk0OGE4ZjczNmIwZjc2NTMwNzYxZjNjZDQxYjI0ZjU2YTJhOTk0Yw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGExOWMzZTU1NzM5NTUxMzIzNzc4ZmUxNGU2MGEzMmU5YzMzOTdjOGQyNWVl
|
10
|
+
ZTVjZGYzYTM4ZmM3ZmViYTU5NTE3MzE0ZjUwMDgwNjJmMmQzMTkyMmE0M2E0
|
11
|
+
MjA3MzBhY2QxMzAyNGQ2YmU5ZGY4YjlkNGY0YWMyOGJjZWYzYWY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGJkZWUxODAyMWYzODQ4NjJjNjczZGE4MGUxOTYzNjlkZmJhOWZkNTJiYWQ3
|
14
|
+
MzUyNjk0NzZlZjAwNDc2YmE3YmZlOTVjMWNiMjIwNjBiMzFiYTdjM2M5ODk3
|
15
|
+
NGNlNjUxNTk2MmI4ZTA4NzQwMzlmNmQyYzA1YjZkYTVlYjM4YWE=
|
data/image_size.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'image_size'
|
5
|
-
s.version = '1.
|
5
|
+
s.version = '1.2.0'
|
6
6
|
s.summary = %q{Measure image size using pure Ruby}
|
7
7
|
s.description = %q{Measure following file dimensions: bmp, gif, jpeg, pbm, pcx, pgm, png, ppm, psd, swf, tiff, xbm, xpm}
|
8
8
|
s.homepage = "http://github.com/toy/#{s.name}"
|
data/lib/image_size.rb
CHANGED
@@ -12,6 +12,7 @@ class ImageSize
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class ImageReader # :nodoc:
|
15
|
+
attr_reader :data
|
15
16
|
def initialize(data_or_io)
|
16
17
|
@io = case data_or_io
|
17
18
|
when IO, StringIO, Tempfile
|
@@ -50,6 +51,16 @@ class ImageSize
|
|
50
51
|
open(path, 'rb'){ |f| new(f) }
|
51
52
|
end
|
52
53
|
|
54
|
+
# Used for svg
|
55
|
+
def self.dpi
|
56
|
+
@dpi || 72
|
57
|
+
end
|
58
|
+
|
59
|
+
# Used for svg
|
60
|
+
def self.dpi=(dpi)
|
61
|
+
@dpi = dpi.to_f
|
62
|
+
end
|
63
|
+
|
53
64
|
# Given image as IO, StringIO, Tempfile or String finds its format and dimensions
|
54
65
|
def initialize(data)
|
55
66
|
ir = ImageReader.new(data)
|
@@ -79,6 +90,7 @@ class ImageSize
|
|
79
90
|
|
80
91
|
private
|
81
92
|
|
93
|
+
SVG_R = /<svg\b([^>]*)>/
|
82
94
|
def detect_format(ir)
|
83
95
|
head = ir[0, 1024]
|
84
96
|
case
|
@@ -93,7 +105,10 @@ private
|
|
93
105
|
when head =~ /\/\* XPM \*\// then :xpm
|
94
106
|
when head[0, 4] == '8BPS' then :psd
|
95
107
|
when head =~ /^[FC]WS/ then :swf
|
96
|
-
when head[
|
108
|
+
when head[SVG_R] ||
|
109
|
+
head =~ /<\?xml|<!--/ && ir[0, 4096][SVG_R]
|
110
|
+
then :svg
|
111
|
+
when head[0, 2] =~ /\n[\000-\005]/ then :pcx
|
97
112
|
end
|
98
113
|
end
|
99
114
|
|
@@ -218,4 +233,27 @@ private
|
|
218
233
|
x_min, x_max, y_min, y_max = values
|
219
234
|
[(x_max - x_min) / 20, (y_max - y_min) / 20]
|
220
235
|
end
|
236
|
+
|
237
|
+
DPI = 72
|
238
|
+
def size_of_svg(ir)
|
239
|
+
attributes = {}
|
240
|
+
ir.data[SVG_R, 1].scan(/(\S+)=(?:'([^']*)'|"([^"]*)"|([^'"\s]*))/) do |name, v0, v1, v2|
|
241
|
+
attributes[name] = v0 || v1 || v2
|
242
|
+
end
|
243
|
+
dpi = self.class.dpi
|
244
|
+
[attributes['width'], attributes['height']].map do |length|
|
245
|
+
if length
|
246
|
+
pixels = case length.downcase.strip[/(?:em|ex|px|in|cm|mm|pt|pc|%)\z/]
|
247
|
+
when 'em', 'ex', '%' then nil
|
248
|
+
when 'in' then length.to_f * dpi
|
249
|
+
when 'cm' then length.to_f * dpi / 2.54
|
250
|
+
when 'mm' then length.to_f * dpi / 25.4
|
251
|
+
when 'pt' then length.to_f * dpi / 72
|
252
|
+
when 'pc' then length.to_f * dpi / 6
|
253
|
+
else length.to_f
|
254
|
+
end
|
255
|
+
pixels.round if pixels
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
221
259
|
end
|
data/spec/image_size_spec.rb
CHANGED
@@ -18,6 +18,7 @@ describe ImageSize do
|
|
18
18
|
['test.tif', :tiff, 48, 64],
|
19
19
|
['test.xbm', :xbm, 16, 32],
|
20
20
|
['test.xpm', :xpm, 24, 32],
|
21
|
+
['test.svg', :svg, 72, 100],
|
21
22
|
['image_size_spec.rb', nil, nil, nil],
|
22
23
|
].each do |name, format, width, height|
|
23
24
|
path = File.join(File.dirname(__FILE__), name)
|
data/spec/test.svg
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: image_size
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keisuke Minami
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- spec/test.pgm
|
47
47
|
- spec/test.png
|
48
48
|
- spec/test.psd
|
49
|
+
- spec/test.svg
|
49
50
|
- spec/test.swf
|
50
51
|
- spec/test.tif
|
51
52
|
- spec/test.xbm
|
@@ -86,6 +87,7 @@ test_files:
|
|
86
87
|
- spec/test.pgm
|
87
88
|
- spec/test.png
|
88
89
|
- spec/test.psd
|
90
|
+
- spec/test.svg
|
89
91
|
- spec/test.swf
|
90
92
|
- spec/test.tif
|
91
93
|
- spec/test.xbm
|