dimension 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/scanner.rb +26 -0
- data/examples/sinatra.rb +1 -1
- data/lib/dimension/image.rb +2 -2
- data/lib/dimension/processors/imlib2.rb +1 -1
- data/lib/dimension/scanner.rb +63 -0
- data/lib/dimension/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d13af7ad87063cbcf447fce7a56a012e3c9e672
|
4
|
+
data.tar.gz: 3fbad9f6c8b147e520bb859e9fa9d4bf098f7d8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9bed0f28b6d36e67b41101170f70ee5a9d3519483d461f13735dd02c063ce1574e25fb4637d10ce7918707b475caa6268fcd06b00e91bfb21e92a3096e3098b
|
7
|
+
data.tar.gz: 140b49b05925b9e6d9c35a002a58d55f6a096fc9ced913526e498e45576452fbbefd062525ac77022104870bca0d344746cd387024b548ef261781eb360d2d6a
|
data/examples/scanner.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../lib/dimension'
|
2
|
+
require_relative '../lib/dimension/scanner'
|
3
|
+
|
4
|
+
content = %{
|
5
|
+
This is a snippet of text to demonstrate how Dimension's scanner module works.
|
6
|
+
It will detect any link that points to an image containing a geometry (e.g image-100x100.png)
|
7
|
+
and try to generate a thumbnail if it doesn't exist already.
|
8
|
+
|
9
|
+
The parser is quite smart, and will correctly detect images linked using an HTML tag,
|
10
|
+
like <img src="assets/window-200x.jpg" /> or Markdown  or
|
11
|
+
simply a spare URL like assets/the-two-towers-200x200.png.
|
12
|
+
}
|
13
|
+
|
14
|
+
scanner = Dimension::Scanner.new(:root => File.dirname(__FILE__))
|
15
|
+
|
16
|
+
thumbs = scanner.process(content)
|
17
|
+
|
18
|
+
puts thumbs.inspect
|
19
|
+
|
20
|
+
thumbs.each do |thumb|
|
21
|
+
if File.exist?(thumb)
|
22
|
+
puts "Thumbnail generated! #{thumb}"
|
23
|
+
else
|
24
|
+
puts "Huh? There should be an image at #{thumb} but there's nothing there."
|
25
|
+
end
|
26
|
+
end
|
data/examples/sinatra.rb
CHANGED
data/lib/dimension/image.rb
CHANGED
@@ -110,8 +110,8 @@ class Image
|
|
110
110
|
end
|
111
111
|
|
112
112
|
def inspect
|
113
|
-
geometry = get_new_geometry
|
114
|
-
"#<Dimension::Image:#{object_id} @width=#{geometry[0]}, @height=#{geometry[1]}>"
|
113
|
+
# geometry = get_new_geometry
|
114
|
+
"#<Dimension::Image:#{object_id}>" # @width=#{geometry[0]}, @height=#{geometry[1]}>"
|
115
115
|
end
|
116
116
|
|
117
117
|
def to_response(env = nil)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'dimension'
|
2
|
+
require 'logger'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Dimension
|
6
|
+
|
7
|
+
class Scanner
|
8
|
+
|
9
|
+
def initialize(opts = {})
|
10
|
+
raise "Search root path required." unless opts[:root]
|
11
|
+
@root = File.expand_path(opts.delete(:root))
|
12
|
+
@log_output = opts.delete(:log_output) || STDOUT
|
13
|
+
end
|
14
|
+
|
15
|
+
def process(content)
|
16
|
+
images = content.scan(/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i)
|
17
|
+
return [] if images.empty?
|
18
|
+
|
19
|
+
paths = images.map { |img| URI.parse(img[0]).path }.uniq
|
20
|
+
paths.map do |path|
|
21
|
+
|
22
|
+
full = File.join(@root, path)
|
23
|
+
if File.exist?(full)
|
24
|
+
logger.info "Image exists in #{@root}: #{path}"
|
25
|
+
next
|
26
|
+
end
|
27
|
+
|
28
|
+
logger.info "Image not found! #{path}"
|
29
|
+
no_extension = File.basename(path, File.extname(path))
|
30
|
+
|
31
|
+
if geometry = no_extension[/((\d+)?x(\d+)?.?)$/i, 1]
|
32
|
+
original = File.join(@root, path.sub(/-?#{geometry}/, ''))
|
33
|
+
|
34
|
+
if File.exist?(original)
|
35
|
+
if resize_image(original, geometry, full)
|
36
|
+
logger.warn "Generated thumbnail for #{geometry} version or #{original}."
|
37
|
+
full
|
38
|
+
else
|
39
|
+
logger.error "Unable to generate #{geometry} thumbnail for #{original}"
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
else
|
43
|
+
logger.warn "Geometry #{geometry} was requested, but #{original} does not exist."
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end.compact
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def logger
|
53
|
+
Logger.new(@log_output)
|
54
|
+
end
|
55
|
+
|
56
|
+
def resize_image(file, geometry, destination)
|
57
|
+
thumb = Dimension.open(file)
|
58
|
+
thumb.generate!(geometry, destination)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lib/dimension/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dimension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-throttle
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- examples/assets/the-two-towers.png
|
44
44
|
- examples/assets/window.jpg
|
45
45
|
- examples/middleware.rb
|
46
|
+
- examples/scanner.rb
|
46
47
|
- examples/sinatra.rb
|
47
48
|
- lib/dimension.rb
|
48
49
|
- lib/dimension/image.rb
|
@@ -50,6 +51,7 @@ files:
|
|
50
51
|
- lib/dimension/processors/image_magick.rb
|
51
52
|
- lib/dimension/processors/imlib2.rb
|
52
53
|
- lib/dimension/processors/vips.rb
|
54
|
+
- lib/dimension/scanner.rb
|
53
55
|
- lib/dimension/version.rb
|
54
56
|
homepage: https://github.com/tomas/dimension
|
55
57
|
licenses: []
|