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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTQyNzA0NTJhM2ViNDFiOWM4ZjdhNDA4ZDVmMzgxZGEzOTg0ZWRmYw==
4
+ Y2YyMGZmNzllODAzMjlhMzRlYWNhZTBkOWQ3OWRlODViMmY4YTliNA==
5
5
  data.tar.gz: !binary |-
6
- ZWE2YmQ3ODVlYjI1NDM1NTgyYTA3ODFiNDY3ODYxN2VkNmUzNGY4MQ==
6
+ ZTk0OGE4ZjczNmIwZjc2NTMwNzYxZjNjZDQxYjI0ZjU2YTJhOTk0Yw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OTQ3MjBiNDQxMTIyZWRkOWY1NjBmMjRhODM0OTUxY2JmNjBmZjY4NWQxNjgy
10
- ZjcwNjBlNjU2MWFhNTBmYTY1MzM4NWVmMTZjMzIxZDlmNmVmZTg1ODVlMzJh
11
- ZjE5Njg2NWFlNGY1NjIxMjAwMjY3NTA4NDdkMDU0NjNkNmViNGY=
9
+ ZGExOWMzZTU1NzM5NTUxMzIzNzc4ZmUxNGU2MGEzMmU5YzMzOTdjOGQyNWVl
10
+ ZTVjZGYzYTM4ZmM3ZmViYTU5NTE3MzE0ZjUwMDgwNjJmMmQzMTkyMmE0M2E0
11
+ MjA3MzBhY2QxMzAyNGQ2YmU5ZGY4YjlkNGY0YWMyOGJjZWYzYWY=
12
12
  data.tar.gz: !binary |-
13
- YzVlMGU4OTdjN2M2NjNiMTk3OTQ4Mjc2ZDAyZWE5MWE1Mzc1ODBhZjgxODRh
14
- NWQ5NmEyNjg0MjQ2MmQ2Y2Y0NDlhMTkzYzM2MGNhOTk4ODBlMTNkZjk1NTUy
15
- ZDZjN2U1N2U3YzZhM2IxYzY5ZDk3YWQzZGRkYmFkNDRlN2JmMDc=
13
+ NGJkZWUxODAyMWYzODQ4NjJjNjczZGE4MGUxOTYzNjlkZmJhOWZkNTJiYWQ3
14
+ MzUyNjk0NzZlZjAwNDc2YmE3YmZlOTVjMWNiMjIwNjBiMzFiYTdjM2M5ODk3
15
+ NGNlNjUxNTk2MmI4ZTA4NzQwMzlmNmQyYzA1YjZkYTVlYjM4YWE=
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'image_size'
5
- s.version = '1.1.5'
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}"
@@ -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[0, 1] == "\n" then :pcx
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
@@ -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)
@@ -0,0 +1,3 @@
1
+ <svg width="1in" height="100" xmlns="http://www.w3.org/2000/svg">
2
+ <circle cx="36" cy="50" r="30" stroke="black" stroke-width="10" fill="red" />
3
+ </svg>
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.1.5
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: 2013-12-23 00:00:00.000000000 Z
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