photo-cook 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ee9b263617fe5981ceced2edd99d80b934dcc94
4
- data.tar.gz: b7803adfb283dc1ae2cf6e135ce0acbd107fa0ae
3
+ metadata.gz: 6e15cd4a81a3e2b2873faca2e0c1bc33788b230d
4
+ data.tar.gz: 96578e33f51efdf6b2300f3db3d5d815713883c9
5
5
  SHA512:
6
- metadata.gz: f73d1813612dd47e1974d3463093fd4f5ec13ccb0d0cfd29853cd1e6f7dd10064de340c00ee2e46a5774d73c9a359d9eb6485ee503099356d123791a39b6fb34
7
- data.tar.gz: 3d28240d6232c89f4c91ba206e064a01811953fd2d2d07526930374a2863f85c459861dce91077150931027d994d93c7c6cddf22b6be4fc94727ecbb8b29253d
6
+ metadata.gz: 6b14c7e899302a98bb69d667be131b960587f323bb6c60bd4dfbb1ba40c90287c5883465b2f751e32bf383d46373678a7c86c38803e4bf80d2ad752385296075
7
+ data.tar.gz: c26e25973b242036bca129b90b425321f223f714aa11688ec873940844b5aa703ba4adbfc0d446c33625f2d4876bbfbbaec13e8f3f0c0455f3b8cdf9e82683f2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- photo-cook (1.0.0)
4
+ photo-cook (1.0.1)
5
5
  mini_magick (~> 4.0)
6
6
  rack (~> 1.5)
7
7
 
@@ -9,7 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  mini_magick (4.1.0)
12
- rack (1.5.5)
12
+ rack (1.6.4)
13
13
 
14
14
  PLATFORMS
15
15
  ruby
@@ -1,15 +1,11 @@
1
- # Resize algorithms from
2
- # https://github.com/carrierwaveuploader/carrierwave/blob/71cb18bba4a2078524d1ea683f267d3a97aa9bc8/lib/carrierwave/processing/rmagick.rb
3
-
4
1
  require 'mini_magick'
5
2
 
6
3
  module PhotoCook
7
4
  class Resizer
8
5
  include Singleton
9
6
 
10
- CENTER_GRAVITY = 'Center'
11
- TRANSPARENT_BACKGROUND = 'rgba(255,255,255,0.0)'
12
- PHOTO_QUALITY = 100
7
+ CENTER_GRAVITY = 'Center'.freeze
8
+ TRANSPARENT_BACKGROUND = 'rgba(255,255,255,0.0)'.freeze
13
9
 
14
10
  def resize(photo_path, width, height, crop = false)
15
11
  if crop
@@ -22,50 +18,66 @@ module PhotoCook
22
18
  # Resize the photo to fit within the specified dimensions:
23
19
  # - the original aspect ratio will be kept
24
20
  # - new dimensions will be not larger then the specified
21
+ #
22
+ # https://github.com/carrierwaveuploader/carrierwave/blob/71cb18bba4a2078524d1ea683f267d3a97aa9bc8/lib/carrierwave/processing/mini_magick.rb#L131
25
23
  def resize_to_fit(photo_path, width, height)
24
+
26
25
  # Do nothing if photo is not valid so exceptions will be not thrown
27
26
  return unless (photo = open(photo_path)).try(:valid?)
28
27
 
29
- photo.combine_options do |cmd|
30
- cmd.quality PHOTO_QUALITY
31
- cmd.resize "#{width == 0 ? nil : width}x#{height == 0 ? nil : height}"
28
+ width, height = parse_dimensions(width, height)
29
+ store_path = PhotoCook.assemble_path(photo_path, width, height, false)
30
+
31
+ if width > 0 || height > 0
32
+ photo.combine_options do |cmd|
33
+ cmd.resize "#{width == 0 ? nil : width}x#{height == 0 ? nil : height}>"
34
+ end
32
35
  end
33
36
 
34
- store photo, PhotoCook.assemble_path(photo_path, width, height, false)
37
+ store(photo, store_path)
35
38
  end
36
39
 
37
40
  # Resize the photo to fit within the specified dimensions:
38
41
  # - new dimensions will be the same as specified
39
42
  # - the photo will be cropped if necessary
43
+ #
44
+ # https://github.com/carrierwaveuploader/carrierwave/blob/71cb18bba4a2078524d1ea683f267d3a97aa9bc8/lib/carrierwave/processing/mini_magick.rb#L176
40
45
  def resize_to_fill(photo_path, width, height)
46
+
41
47
  # Do nothing if photo is not valid so exceptions will be not thrown
42
48
  return unless (photo = open(photo_path)).try(:valid?)
43
49
 
44
- cols, rows = photo[:dimensions]
45
- photo.combine_options do |cmd|
46
- if width != cols || height != rows
47
- scale_x = width / cols.to_f
48
- scale_y = height / rows.to_f
49
- if scale_x >= scale_y
50
- cols = (scale_x * (cols + 0.5)).round
51
- rows = (scale_x * (rows + 0.5)).round
52
- cmd.resize "#{cols}"
53
- else
54
- cols = (scale_y * (cols + 0.5)).round
55
- rows = (scale_y * (rows + 0.5)).round
56
- cmd.resize "x#{rows}"
50
+ width, height = parse_dimensions(width, height)
51
+ cols, rows = photo[:dimensions]
52
+ store_path = PhotoCook.assemble_path(photo_path, width, height, true)
53
+
54
+ if width > 0 || height > 0
55
+ photo.combine_options do |cmd|
56
+ if width != cols || height != rows
57
+ scale_x = width / cols.to_f
58
+ scale_y = height / rows.to_f
59
+ if scale_x >= scale_y
60
+ cols = (scale_x * (cols + 0.5)).round
61
+ rows = (scale_x * (rows + 0.5)).round
62
+ cmd.resize "#{cols}>"
63
+ else
64
+ cols = (scale_y * (cols + 0.5)).round
65
+ rows = (scale_y * (rows + 0.5)).round
66
+ cmd.resize "x#{rows}>"
67
+ end
68
+ end
69
+ cmd.gravity CENTER_GRAVITY
70
+ cmd.background TRANSPARENT_BACKGROUND
71
+ if cols != width || rows != height
72
+ cmd.extent "#{width == 0 ? nil : width}x#{height == 0 ? nil : height}>"
57
73
  end
58
74
  end
59
- cmd.gravity CENTER_GRAVITY
60
- cmd.background TRANSPARENT_BACKGROUND
61
- cmd.quality PHOTO_QUALITY
62
- cmd.extent "#{width}x#{height}" if cols != width || rows != height
63
75
  end
64
76
 
65
- store photo, PhotoCook.assemble_path(photo_path, width, height, true)
77
+ store(photo, store_path)
66
78
  end
67
79
 
68
- private
80
+ protected
69
81
 
70
82
  def open(photo_path)
71
83
  begin
@@ -86,8 +98,15 @@ module PhotoCook
86
98
  resized_photo
87
99
  end
88
100
 
89
- def normalize_dimensions(w, h)
90
- [w.to_i == 0 ? nil : w.to_i, h.to_i == 0 ? nil : h.to_i]
101
+ def parse_dimensions(width, height)
102
+ width = width == :auto ? 0 : width.to_i
103
+ height = height == :auto ? 0 : height.to_i
104
+ check_dimensions!(width, height)
105
+ [width, height]
106
+ end
107
+
108
+ def check_dimensions!(width, height)
109
+ raise ArgumentError, 'Expected positive numbers ' if width < 0 || height < 0
91
110
  end
92
111
  end
93
112
  end
@@ -1,3 +1,3 @@
1
1
  module PhotoCook
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
data/lib/photo-cook.rb CHANGED
@@ -16,22 +16,22 @@ module PhotoCook
16
16
  end
17
17
 
18
18
  def self.assemble_path(path, width, height, crop = false)
19
- File.join PhotoCook.assemble_dir(path), PhotoCook.assemble_name(path, width, height, crop)
19
+ File.join(assemble_dir(path), assemble_name(path, width, height, crop))
20
20
  end
21
21
 
22
22
  def self.assemble_dir(path)
23
- File.join File.dirname(path), PhotoCook.resize_dir
23
+ File.join(File.dirname(path), resize_dir)
24
24
  end
25
25
 
26
26
  def self.assemble_name(path, width, height, crop = false)
27
- File.basename(path, '.*') + PhotoCook.assemble_command(width, height, crop) + File.extname(path)
27
+ File.basename(path, '.*') + assemble_command(width, height, crop) + File.extname(path)
28
28
  end
29
29
 
30
30
  def self.assemble_command(width, height, crop = false)
31
- prefix = "-#{width == 0 ? '' : width}x#{height == 0 ? '' : height}"
31
+ width, height = width.to_i, height.to_i
32
+ prefix = "-#{width == 0 ? nil : width}x#{height == 0 ? nil : height}"
32
33
  prefix + (crop ? 'crop' : '')
33
34
  end
34
-
35
35
  end
36
36
 
37
37
  require 'photo-cook/engine' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: photo-cook
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-04 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack