neofiles 1.0.6 → 1.1.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: 27974bef2a62fcb3d25b9e8ac9601652db64d4e5
4
- data.tar.gz: d64f07dafc39d6e299d0bbc5e599380deccdc9fb
3
+ metadata.gz: 6e96aa097e34ac97047248c698a3aeb9b64bc9ab
4
+ data.tar.gz: 66851a10940c7c14edd2eef83354815c857328e2
5
5
  SHA512:
6
- metadata.gz: a08c5998c6085d959aca8683c55d481082cce84a2926c9c811f77bb6dfbe95349178367671414dfedb37d9978bb6e6b573c000906e43516bdd3c3fb7f09528a3
7
- data.tar.gz: e14f40913afe907ded71b6c8eedfd76cd67e2adfbfd8c640e7de1adb6d46053bf0b5043eecfc7087ce73eab033c1ef85ee0bff34a6e8e75a27f5c397d57ff03d
6
+ metadata.gz: d1d781c8139d2f9618f10a5b32f13b195053c722e3a3c5c4b38068870a780ecffe10dc801f63f73f26d6b532c96bd7f6273e350f8ba4f97d30c992aa9f197825
7
+ data.tar.gz: 348b856a3802607b95ab7f0df75b1ed4ed2270e1c82965969134417d3cbf03dd49f6cd99a7ddb6925a64fc2c1a021dcf841824efaad085dc5c0c99eb741aa373
@@ -54,21 +54,25 @@ class Neofiles::ImagesController < ActionController::Metal
54
54
  raise Mongoid::Errors::DocumentNotFound unless width.between?(1, CROP_MAX_WIDTH) and height.between?(1, CROP_MAX_HEIGHT)
55
55
  end
56
56
 
57
- crop_requested = Neofiles.crop_requested? params
58
- need_resize_without_crop = width && height && (image_file.width > width || image_file.height > height)
57
+ crop_requested = Neofiles.crop_requested? params
58
+ resizing = width && height
59
+ need_resize_without_crop = resizing && (image_file.width > width || image_file.height > height)
59
60
 
60
- image.combine_options do |mogrify|
61
- resize_image(mogrify, width, height, crop_requested, need_resize_without_crop) if width && height
62
- compress_image(mogrify, quality) if quality
61
+ image.combine_options('convert') do |convert|
62
+ resize_image convert, width, height, crop_requested, need_resize_without_crop if resizing
63
+
64
+ unless nowm?(image_file)
65
+ wm_width, wm_height = Neofiles::resized_image_dimensions image_file, width, height, params
66
+ add_watermark convert, image, wm_width, wm_height if wm_width && wm_height
67
+ end
68
+
69
+ compress_image convert, quality if quality || resizing
63
70
  end
64
71
 
65
- # use pngquant when quality less than 75
72
+ # use pngquant when quality is less than 75
66
73
  ::PngQuantizator::Image.new(image.path).quantize! if options[:type] == 'image/png' && quality && quality < 75
67
74
 
68
- # set watermark
69
- width, height = image_file.width, image_file.height if !crop_requested && !need_resize_without_crop
70
-
71
- data = set_watermark(image, image_file, width, height)
75
+ data = image.to_blob
72
76
 
73
77
  # stream image headers & bytes
74
78
  send_file_headers! options
@@ -80,6 +84,8 @@ class Neofiles::ImagesController < ActionController::Metal
80
84
  self.response_body = I18n.t 'neofiles.403_access_denied'
81
85
  self.content_type = 'text/plain; charset=utf-8'
82
86
  self.status = 403
87
+ ensure
88
+ image.try :destroy! #delete mini_magick tempfile
83
89
  end
84
90
 
85
91
 
@@ -100,42 +106,37 @@ class Neofiles::ImagesController < ActionController::Metal
100
106
  end
101
107
  end
102
108
 
103
- # Fill mogrify command pipe with resize commands
104
- def resize_image(mogrify, width, height, crop_requested, need_resize_without_crop)
109
+ # Fill convert command pipe with resize commands
110
+ def resize_image(convert, width, height, crop_requested, need_resize_without_crop)
105
111
  if crop_requested
106
- mogrify.resize "#{width}x#{height}^"
107
- mogrify.gravity 'center'
108
- mogrify.extent "#{width}x#{height}"
112
+ convert.resize "#{width}x#{height}^"
113
+ convert.gravity 'center'
114
+ convert.extent "#{width}x#{height}"
109
115
  elsif need_resize_without_crop
110
- mogrify.resize "#{width}x#{height}"
116
+ convert.resize "#{width}x#{height}"
111
117
  end
112
118
  end
113
119
 
114
- # Fill mogrify command pipe with compression commands for JPEG and PNG
120
+ # Fill convert command pipe with compression commands for JPEG and PNG
115
121
  # More information: https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/
116
- def compress_image(mogrify, quality)
117
- mogrify.quality "#{quality}"
118
- mogrify << '-unsharp' << '0.25x0.25+8+0.065'
119
- mogrify << '-dither' << 'None'
120
- #mogrify << '-posterize' << '136' # posterize slows down imagamagick extremely in some env due to buggy libgomp1
121
- mogrify << '-define' << 'jpeg:fancy-upsampling=off'
122
- mogrify << '-define' << 'png:compression-filter=5'
123
- mogrify << '-define' << 'png:compression-level=9'
124
- mogrify << '-define' << 'png:compression-strategy=1'
125
- mogrify << '-define' << 'png:exclude-chunk=all'
126
- mogrify << '-interlace' << 'none'
127
- mogrify << '-colorspace' << 'sRGB'
128
- mogrify.strip
122
+ def compress_image(convert, quality)
123
+ convert.quality "#{quality}" if quality
124
+ convert << '-unsharp' << '0.25x0.25+8+0.065'
125
+ convert << '-dither' << 'None'
126
+ #convert << '-posterize' << '136' # posterize slows down imagamagick extremely in some env due to buggy libgomp1
127
+ convert << '-define' << 'jpeg:fancy-upsampling=off'
128
+ convert << '-define' << 'png:compression-filter=5'
129
+ convert << '-define' << 'png:compression-level=9'
130
+ convert << '-define' << 'png:compression-strategy=1'
131
+ convert << '-define' << 'png:exclude-chunk=all'
132
+ convert << '-interlace' << 'none'
133
+ convert << '-colorspace' << 'sRGB'
134
+ convert.strip
129
135
  end
130
136
 
131
- # Place watermark on the image, if needed
132
- def set_watermark(image, image_file, width, height)
133
- Rails.application.config.neofiles.watermarker.(
134
- image,
135
- no_watermark: nowm?(image_file),
136
- watermark_width: width,
137
- watermark_height: height
138
- )
137
+ # Add watermarks command to a command pipe, if watermarker is present
138
+ def add_watermark(convert, image, width, height)
139
+ Rails.application.config.neofiles.watermarker.try :call, convert, image, width, height
139
140
  end
140
141
 
141
142
  end
@@ -94,6 +94,9 @@ class Neofiles::Image < Neofiles::File
94
94
  tempfile.close
95
95
  tempfile.unlink
96
96
  end
97
+
98
+ ensure
99
+ image.try :destroy! #delete mini_magick tempfile
97
100
  end
98
101
 
99
102
  # Return array with width & height decorated with singleton function to_s returning 'WxH' string.
data/lib/neofiles.rb CHANGED
@@ -52,8 +52,12 @@ module Neofiles
52
52
  image_file_height = image_file[:height]
53
53
  end
54
54
 
55
+ # no input, terminate
55
56
  return if image_file_width.blank? || image_file_height.blank?
56
57
 
58
+ # image fits into requested dimensions, no resizing will occur
59
+ return image_file_width, image_file_height if image_file_width <= width && image_file_height <= height
60
+
57
61
  # ... construct request ...
58
62
  command = MiniMagick::CommandBuilder.new(:convert) # convert input file...
59
63
  command.size([image_file_width, image_file_height].join 'x') # with the given dimensions...
@@ -1,3 +1,3 @@
1
1
  module Neofiles
2
- VERSION = '1.0.6'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neofiles
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konanykhin Ilya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-23 00:00:00.000000000 Z
11
+ date: 2017-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails