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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 193039197b19c73705300522aaf23f6ae3b07aee
4
- data.tar.gz: c741743c6dfd4ab233832a46cc97e2b77f8b3901
3
+ metadata.gz: 8d13af7ad87063cbcf447fce7a56a012e3c9e672
4
+ data.tar.gz: 3fbad9f6c8b147e520bb859e9fa9d4bf098f7d8d
5
5
  SHA512:
6
- metadata.gz: 6660d273e74e9772aac18cf4d29c585b517ec4e117f933015c5d0b81c82f9986af371c1c8d10c9c25cced1624db8ea2eeae519d5694ea6b50d0d47ababab371b
7
- data.tar.gz: a3b939d1c3d5d10b022d71cbfc1ab04549e278ba396b9424448e058e77a5a69f814d5e1920b7e77634f6a5cb524a8a62c40615a80727e4a00943cbf5489e8228
6
+ metadata.gz: f9bed0f28b6d36e67b41101170f70ee5a9d3519483d461f13735dd02c063ce1574e25fb4637d10ce7918707b475caa6268fcd06b00e91bfb21e92a3096e3098b
7
+ data.tar.gz: 140b49b05925b9e6d9c35a002a58d55f6a096fc9ced913526e498e45576452fbbefd062525ac77022104870bca0d344746cd387024b548ef261781eb360d2d6a
@@ -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 ![img](assets/chair-x200.jpg) 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
@@ -11,7 +11,7 @@ end
11
11
  geometries = [
12
12
  '200x200',
13
13
  '300x200!',
14
- '200x300#',
14
+ '200x300@',
15
15
  '20x50:ne'
16
16
  ]
17
17
 
@@ -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)
@@ -109,7 +109,7 @@ module Imlib2Processor
109
109
 
110
110
  def get_resize_geometry(w, h, to_longer = true)
111
111
  if to_longer or h.nil?
112
- if image.w < image.h
112
+ if h.nil? || image.w < image.h
113
113
  new_h = ((w.to_f / image.w) * image.h).round
114
114
  return w.to_i, new_h
115
115
  else
@@ -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
@@ -1,6 +1,6 @@
1
1
  module Dimension
2
2
  MAJOR = 0
3
- MINOR = 2
3
+ MINOR = 3
4
4
  PATCH = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].join('.')
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.2.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-08 00:00:00.000000000 Z
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: []