dimscan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8c3e5ac03d2656716393fde60824ab21e8b5346
4
+ data.tar.gz: 98a593adbda3cb4ae555644193a35eae499ef11d
5
+ SHA512:
6
+ metadata.gz: aaffc8a3d90d43cf0d64868ca4eef3a22828fec7077968ecebb3c344094f934b8778e35e3d364fb1a796ac88a5fe0780f556c14f49cf85a4ff76e9deee36ef6a
7
+ data.tar.gz: ffbfa978cac9741363b3c26f6117038c802aee02344614f1f58f0cbcb40660a2dd61695c5cbcdeb762ac81eb96884f1df2ac91bbf7d5d5f91f661107002a7b4b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dimscan.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015 Joakim Reinert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Dimscan
2
+
3
+ A small library for extracting pixel dimensions for images hosted on web
4
+ servers. Only loads whatever portion is necessary to determine the dimensions.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'dimscan'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install dimscan
21
+
22
+ ## Usage
23
+
24
+ ``` ruby
25
+ scanner = Dimscan::JPEGScanner.new('https://example.com/image.jpg')
26
+ scanner.scan_dimensions
27
+ # => { width: 32, height: 32 }
28
+ ```
29
+
30
+ At the moment only JPEG and PNG are supported.
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
35
+ `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To
38
+ release a new version, update the version number in `version.rb`, and then run
39
+ `bundle exec rake release` to create a git tag for the version, push git
40
+ commits and tags, and push the `.gem` file to
41
+ [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/dimscan/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dimscan"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/dimscan.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dimscan/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dimscan'
8
+ spec.version = Dimscan::VERSION
9
+ spec.authors = ['Joakim Reinert']
10
+ spec.email = ['mail@jreinert.net']
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
14
+ end
15
+
16
+ spec.summary = 'Scans dimensions from hosted images'
17
+ spec.description = 'Bandwith efficient scanner for dimensions of web ' \
18
+ 'hosted images'
19
+ spec.homepage = 'https://github.com/jreinert/dimscan'
20
+ spec.license = 'MIT'
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(/^(test|spec|features)\//)
24
+ end
25
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.8'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.2'
31
+ spec.add_development_dependency 'simplecov', '~> 0.9'
32
+ spec.add_development_dependency 'pry-byebug', '~> 3.0'
33
+
34
+ spec.add_dependency 'mini_magick', '~> 4.1'
35
+ spec.add_dependency 'abstractize', '~> 0.1'
36
+ end
@@ -0,0 +1,34 @@
1
+ require 'uri'
2
+ require 'dimscan/http_byte_enumerator'
3
+ require 'mini_magick'
4
+ require 'abstractize'
5
+
6
+ module Dimscan
7
+ # The abstract base class
8
+ class BaseScanner
9
+ include Abstractize
10
+
11
+ def initialize(url)
12
+ @bytes = HTTPByteEnumerator.new(url)
13
+ @fallback_file = `mktemp`.chomp
14
+ end
15
+
16
+ def scan_dimensions
17
+ dimensions = nil
18
+ File.open(@fallback_file, 'wb') do |file|
19
+ dimensions = scan(@bytes.each) { |b| file.write(b) }
20
+ end
21
+ width, height = (
22
+ dimensions || MiniMagick::Image.new(@fallback_file).dimensions
23
+ )
24
+
25
+ { width: width, height: height }
26
+ end
27
+
28
+ protected
29
+
30
+ def scan(_bytes)
31
+ fail AbstractError, 'not implemented'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module Dimscan
5
+ # A utility class for enumerating over the bytes of a http response body
6
+ class HTTPByteEnumerator
7
+ def initialize(url)
8
+ @uri = URI(url)
9
+ fail ArgumentError, 'invalid scheme' unless @uri.scheme =~ /^https?$/
10
+ end
11
+
12
+ def each(&block)
13
+ return to_enum(__callee__) unless block_given?
14
+ request = Net::HTTP::Get.new(@uri)
15
+ Net::HTTP.start(
16
+ @uri.host, @uri.port, use_ssl: @uri.scheme == 'https'
17
+ ) do |http|
18
+ http.request(request) do |response|
19
+ fail response.body if error?(response)
20
+ enumerate_response(response, &block)
21
+ end
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def error?(response)
28
+ [Net::HTTPError, Net::HTTPClientError].any? do |error|
29
+ response.is_a?(error)
30
+ end
31
+ end
32
+
33
+ def enumerate_response(response, &block)
34
+ response.read_body do |chunk|
35
+ chunk.each_byte(&block)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require 'dimscan/base_scanner'
2
+
3
+ module Dimscan
4
+ # Scans dimensions from a JPEG image
5
+ class JPEGScanner < BaseScanner
6
+ SOF0 = [0xFF, 0xC0, 0x00, 0x11, 0x08]
7
+
8
+ protected
9
+
10
+ def scan(bytes)
11
+ # Dimensions are in bytes 4-8 after SOF0
12
+ last_slice = nil
13
+ bytes.each_cons(SOF0.size + 4) do |slice|
14
+ yield slice.first.chr
15
+ last_slice = slice
16
+ if slice.first(SOF0.size) == SOF0
17
+ return extract_dimensions(slice.last(4))
18
+ end
19
+ end
20
+ yield last_slice[1..-1].map(&:chr).join
21
+ nil
22
+ end
23
+
24
+ def extract_dimensions(bytes)
25
+ # height is before width in jpeg header
26
+ byte_string = bytes.map(&:chr).join
27
+ byte_string.unpack('nn').reverse
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ require 'dimscan/base_scanner'
2
+
3
+ module Dimscan
4
+ # Scans dimensions from a PNG image
5
+ class PNGScanner < BaseScanner
6
+ IHDR = [0x49, 0x48, 0x44, 0x52]
7
+
8
+ protected
9
+
10
+ def scan(bytes)
11
+ # Dimensions are in 8 bytes after IHDR
12
+ last_slice = nil
13
+ bytes.each_cons(IHDR.size + 8) do |slice|
14
+ yield slice.first.chr
15
+ last_slice = slice
16
+ if slice.first(IHDR.size) == IHDR
17
+ return extract_dimensions(slice.last(8))
18
+ end
19
+ end
20
+ yield last_slice[1..-1].map(&:chr).join
21
+ nil
22
+ end
23
+
24
+ def extract_dimensions(bytes)
25
+ byte_string = bytes.map(&:chr).join
26
+ byte_string.unpack('NN')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Dimscan
2
+ VERSION = "0.1.0"
3
+ end
data/lib/dimscan.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "dimscan/version"
2
+
3
+ module Dimscan
4
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dimscan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Reinert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mini_magick
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: abstractize
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.1'
111
+ description: Bandwith efficient scanner for dimensions of web hosted images
112
+ email:
113
+ - mail@jreinert.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".ruby-version"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - MIT-LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - dimscan.gemspec
129
+ - lib/dimscan.rb
130
+ - lib/dimscan/base_scanner.rb
131
+ - lib/dimscan/http_byte_enumerator.rb
132
+ - lib/dimscan/jpeg_scanner.rb
133
+ - lib/dimscan/png_scanner.rb
134
+ - lib/dimscan/version.rb
135
+ homepage: https://github.com/jreinert/dimscan
136
+ licenses:
137
+ - MIT
138
+ metadata:
139
+ allowed_push_host: https://rubygems.org
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Scans dimensions from hosted images
160
+ test_files: []