kt-paperclip-optimizer 3.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41c33014ee5ae573b28bc3ab5fe9540ef1e125fb2eda1de03dfeceef66c8ba43
4
+ data.tar.gz: 16bc7ae43a6eedccbf1c57d0e136393fba3693cdb778901cecedb2334530747f
5
+ SHA512:
6
+ metadata.gz: 493a1795b44e0a3141c7c55365f5161aead688f9fb76efa5fb9fe6c646fcba5b040f7164c26701088361e1c78f9746845053e2ee89b48b5e5e87792119d1d7bf
7
+ data.tar.gz: 33742273c6a216dc04ba288deea1933d95e020e85daf29999aebdc170e624c55f78e004e0cc7f6c33fb15a47621197a9673ab36388c91ce236cc0bf4aedbe4e6
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ paperclip-optimizer.sublime-project
20
+ paperclip-optimizer.sublime-workspace
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'spec/schema.rb'
4
+
5
+ Style/SpaceInsideParens:
6
+ Enabled: false
7
+ Metrics/LineLength:
8
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ kt-paperclip-optimizer
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.7.1
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.3.0"
4
+ - "2.4.0"
5
+ - "2.5.0"
6
+ - "2.6.0"
7
+ - "2.7.0"
8
+ - "3.0.0"
9
+ before_install:
10
+ - yes | gem update --system --force
11
+ - gem install bundler
12
+ - sudo apt-get install -qq libjpeg-progs optipng
13
+ script: bundle exec rspec -b spec
data/CHANGELOG.md ADDED
@@ -0,0 +1,58 @@
1
+ # CHANGELOG
2
+
3
+ ## 3.0.0
4
+
5
+ * Fork from paperclip-optimizer
6
+
7
+ * Use kt-paperclip instead of paperclip
8
+
9
+ ## 2.0.0
10
+
11
+ * same as Beta 3
12
+
13
+ ## 2.0.0.beta.3
14
+
15
+ * allow minor releases of image_optim above 0.19
16
+
17
+ image_optim 0.20 apparently [fixes an issue](https://github.com/toy/image_optim/issues/74) with Ruby 2.2.0 — thanks [leifcr](https://github.com/leifcr)
18
+
19
+ ## 2.0.0.beta.2
20
+
21
+ * re-enable compatibility with Paperclip 3.4.2
22
+
23
+ ## 2.0.0.beta
24
+
25
+ * **all available optimization libraries are disabled by default**
26
+
27
+ Previous versions enabled jpegtran and optipng by default. You will have to
28
+ re-enable them manually if you wish to retain that behaviour
29
+
30
+ * configure PaperclipOptimizer globally, per-attachment and per-style
31
+
32
+ Thanks to [danschultzer](https://github.com/danschultzer), [braindeaf](https://github.com/braindeaf) and
33
+ [tirdadc](https://github.com/tirdadc) for pull requests, input and reports
34
+
35
+ ## 1.0.3
36
+
37
+ * updated tests, compatibility with Paperclip 4 - thanks [Sija](https://github.com/Sija)
38
+
39
+ ## 1.0.2
40
+
41
+ * relax Paperclip dependency, allow 3.4.x again since it works fine
42
+
43
+ ## 1.0.1
44
+
45
+ * do not produce verbose log output by default - thanks [Sunny Ripert](https://github.com/sunny)
46
+ * declare the dependency on Paperclip only once - thanks [Sunny Ripert](https://github.com/sunny)
47
+
48
+ ## 1.0.0
49
+
50
+ * we're going to 1.0.0 after six months in production
51
+ * use image_optim 0.9 and upwards
52
+ * use Paperclip 3.5.2 and upwards
53
+ * document image_optim_bin for use on Heroku
54
+ * add a CHANGELOG
55
+
56
+ ## 0.2.0
57
+
58
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-optimizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jan-Christian Foeh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # PaperclipOptimizer
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/paperclip-optimizer.png)](http://badge.fury.io/rb/kt-paperclip-optimizer)
4
+
5
+ KtPaperclipOptimizer is a [kt-Paperclip](https://github.com/kreeti/kt-paperclip) processor for
6
+ optimizing and minifying uploaded images.
7
+
8
+ It is just a thin wrapper around [ImageOptim](https://github.com/toy/image_optim),
9
+ which supports many external optimization libraries such as [advpng](http://advancemame.sourceforge.net/doc-advpng.html), [gifsicle](http://www.lcdf.org/gifsicle/),
10
+ [jhead](http://www.sentex.net/~mwandel/jhead/), [jpegoptim](http://www.kokkonen.net/tjko/projects.html), [jpeg-recompress](https://github.com/danielgtaylor/jpeg-archive#jpeg-recompress),
11
+ [jpegtran](http://www.ijg.org/), [optipng](http://optipng.sourceforge.net/), [pngcrush](http://pmt.sourceforge.net/pngcrush/), [pngout](http://www.advsys.net/ken/util/pngout.htm),
12
+ [pngquant](http://pngquant.org/) and [svgo](https://github.com/svg/svgo).
13
+
14
+ ### What's new
15
+
16
+ **2020-03-31 3.0.0**
17
+
18
+ * Use the maintained gem kt-paperclip instead of the deprecated papercip gem.
19
+
20
+ **2015-01-16 2.0.0**
21
+
22
+ * better configuration: set options [globally, per attachment and per style](#settings)
23
+
24
+ Thanks to [danschultzer](https://github.com/danschultzer), [braindeaf](https://github.com/braindeaf) and
25
+ [tirdadc](https://github.com/tirdadc) for pull requests, input and reports
26
+ * all available optimization libraries are disabled by default
27
+
28
+ Previous versions enabled jpegtran and optipng by default. You will have to
29
+ re-enable them manually if you wish to retain that behaviour
30
+ * optimizers which are enabled but missing or broken are ignored by default
31
+
32
+ Read the [CHANGELOG](CHANGELOG.md) for previous changes.
33
+
34
+ ### Dependencies
35
+
36
+ PaperclipOptimizer is currently compatible with Paperclip 3.4.2 up to 4.2.x.
37
+
38
+ ## Installation
39
+
40
+ Add this line to your application's Gemfile after the Paperclip gem:
41
+
42
+ gem 'paperclip-optimizer'
43
+
44
+ And then execute:
45
+
46
+ $ bundle
47
+
48
+ If you wish to set global configuration settings, run
49
+
50
+ $ rails generate paperclip_optimizer:install
51
+
52
+ to generate an initializer in config/initializers.
53
+
54
+ ### CAUTION
55
+
56
+ **image_optim automatically inserts itself into the asset pipeline and tries to compress your `/app/assets/images` as well.
57
+ Since it enables all libraries it supports by default, you might suddenly run into errors if you do not have all
58
+ of them installed.**
59
+
60
+ Please note: settings you made through PaperclipOptimizer only apply to Paperclip attachments, not to image_optims asset compressor.
61
+
62
+ To disable image_optim in your asset pipeline, add
63
+
64
+ ```ruby
65
+ # config/application.rb
66
+ config.assets.image_optim = false
67
+ ```
68
+
69
+ to your config/application.rb.
70
+
71
+ See [ImageOptims README](https://github.com/toy/image_optim#binaries-location)
72
+ on how to install the various optimization libraries.
73
+
74
+ ### Deployment on Heroku
75
+
76
+ If you deploy to Heroku, take a look at the [image_optim_bin](https://github.com/mooktakim/image_optim_bin) gem. It supplies the necessary
77
+ optimization binaries, compiled and ready for Herokus environment. Make sure to include it **before** paperclip-optimizer.
78
+
79
+ gem "image_optim_bin", group: :production
80
+ gem "paperclip-optimizer"
81
+
82
+ ## Usage
83
+
84
+ Just add `:paperclip_optimizer` to Paperclips' `:processors` - setting:
85
+
86
+ ```ruby
87
+ class User < ActiveRecord::Base
88
+ attr_accessible :avatar
89
+ has_attached_file :avatar,
90
+ styles: { thumb: "100x100>" },
91
+ processors: [:thumbnail, :paperclip_optimizer]
92
+ end
93
+ ```
94
+
95
+ Remember to include the `:thumbnail` processor as well if you want to retain
96
+ Paperclips geometry functionality.
97
+
98
+ ### Settings
99
+
100
+ You can pass configuration options to ImageOptim in three locations: globally, per attachment and per style.
101
+ Settings are merged, so more specific settings replace less specific ones.
102
+
103
+ **Global settings**
104
+
105
+ Global settings apply everywhere. You can override them with per-attachment and per-style settings.
106
+
107
+ Run `rails generate paperclip_optimizer:install` to create an initializer for global settings.
108
+
109
+ ```ruby
110
+ # config/initializers/paperclip_optimizer.rb
111
+ Paperclip::PaperclipOptimizer.default_options = {
112
+ skip_missing_workers: false
113
+ }
114
+ ```
115
+
116
+ **Per-attachment settings**
117
+
118
+ Per-attachment settings apply to all styles of a particular attachment. Override them with per-style settings.
119
+
120
+ ```ruby
121
+ class User < ActiveRecord::Base
122
+ attr_accessible :avatar
123
+ has_attached_file :avatar,
124
+ processors: [:thumbnail, :paperclip_optimizer],
125
+ paperclip_optimizer: {
126
+ pngout: { strategy: 1 }
127
+ },
128
+ styles: {
129
+ thumb: { geometry: "100x100>" },
130
+ medium: { geometry: "200x200>" },
131
+ large: { geometry: "300x300>" }
132
+ }
133
+ end
134
+ ```
135
+
136
+ Just like Paperclips' `:styles` option, you can pass a lambda to `:paperclip_optimizer` to configure it at runtime:
137
+
138
+ ```ruby
139
+ class User < ActiveRecord::Base
140
+ attr_accessible :avatar
141
+ has_attached_file :avatar,
142
+ processors: [:thumbnail, :paperclip_optimizer],
143
+ paperclip_optimizer: ->(attachment) { attachment.instance.my_model_instance_method },
144
+ styles: {
145
+ thumb: { geometry: "100x100>" },
146
+ medium: { geometry: "200x200>" },
147
+ large: { geometry: "300x300>" }
148
+ }
149
+ end
150
+ ```
151
+
152
+ **Per-style settings**
153
+
154
+ ```ruby
155
+ class User < ActiveRecord::Base
156
+ attr_accessible :avatar
157
+ has_attached_file :avatar,
158
+ processors: [:thumbnail, :paperclip_optimizer],
159
+ paperclip_optimizer: {
160
+ pngout: { strategy: 1 }
161
+ },
162
+ styles: {
163
+ thumb: { geometry: "100x100>" },
164
+ medium: { geometry: "200x200>" },
165
+ large: {
166
+ geometry: "300x300>",
167
+ paperclip_optimizer: {
168
+ pngout: { strategy: 4 }
169
+ }
170
+ }
171
+ }
172
+ end
173
+ ```
174
+
175
+ See [ImageOptims options](https://github.com/toy/image_optim#options) or the initializer for
176
+ all available options.
177
+
178
+ ## Contributing
179
+
180
+ 1. Fork it
181
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
182
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
183
+ 4. Push to the branch (`git push origin my-new-feature`)
184
+ 5. Create new Pull Request
185
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,29 @@
1
+ # Paperclip::Optimizer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperclip-optimizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperclip-optimizer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,29 @@
1
+ # Paperclip::Optimizer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperclip-optimizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperclip-optimizer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
Binary file
Binary file
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paperclip-optimizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kt-paperclip-optimizer'
8
+ spec.version = PaperclipOptimizer::VERSION
9
+ spec.authors = ['reedstonefood']
10
+ spec.email = ['hello@loco2.com']
11
+ spec.description = 'kt-paperclip-optimizer is a Paperclip processor for optimizing and minifying uploaded images.'
12
+ spec.summary = 'Minify Paperclip image attachments like JPGs, GIFs or PNGs'
13
+ spec.homepage = 'https://github.com/loco2/kt-paperclip-optimizer'
14
+ spec.license = 'MIT'
15
+
16
+ spec.add_runtime_dependency 'kt-paperclip', '>= 6.2'
17
+ spec.add_runtime_dependency 'image_optim', '~> 0.19'
18
+
19
+ spec.add_development_dependency 'bundler', '~> 2.1'
20
+ spec.add_development_dependency 'rake', '~> 12.3'
21
+ spec.add_development_dependency 'rspec', '~> 3.9'
22
+ spec.add_development_dependency 'rails', '>= 3.2.20'
23
+ spec.add_development_dependency 'sqlite3', '~> 1.4.2'
24
+
25
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
26
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.post_install_message = %q{
30
+ Initializer
31
+ -----------
32
+
33
+ run
34
+
35
+ rails g kt_paperclip_optimizer:install
36
+
37
+ to create an initializer for global configuration.
38
+
39
+ Breaking changes from 1.0.3
40
+ ---------------------------
41
+
42
+ By default, all optimization libraries are now disabled. Re-enable jpegtran and optipng manually
43
+ if you wish to retain the behaviour of PaperclipOptimizer 1.0.3.
44
+
45
+ See https://github.com/janfoeh/paperclip-optimizer#settings for more information on the new
46
+ configuration system.
47
+
48
+ =============================================
49
+ IMPORTANT - READ THIS - IMPORTANT - READ THIS
50
+ =============================================
51
+
52
+ PaperclipOptimizer uses image_optim to do the heavy lifting. image_optim automatically inserts itself
53
+ into the asset pipeline and tries to compress your /app/assets/images as well. By default, it enables
54
+ all the optimization libraries it supports, and it will fail if you do not have all of them installed.
55
+
56
+ Either add
57
+
58
+ config.assets.image_optim = false
59
+
60
+ to your config/application.rb to disable this, or check https://github.com/toy/image_optim#from-rails
61
+ for how to configure this properly.
62
+ }
63
+ end
@@ -0,0 +1 @@
1
+ require 'paperclip-optimizer'
@@ -0,0 +1,38 @@
1
+
2
+ require 'paperclip-optimizer/version'
3
+ require 'paperclip-optimizer/processor'
4
+
5
+ module PaperclipOptimizer
6
+ DEFAULT_OPTIONS = {
7
+ skip_missing_workers: true,
8
+ advpng: false,
9
+ gifsicle: false,
10
+ jhead: false,
11
+ jpegoptim: false,
12
+ jpegrecompress: false,
13
+ jpegtran: false,
14
+ optipng: false,
15
+ pngcrush: false,
16
+ pngout: false,
17
+ pngquant: false,
18
+ svgo: false
19
+ }.freeze
20
+
21
+ # Helper class for capturing ImageOptims error output and redirecting it
22
+ # to Paperclips logger instance
23
+ class StdErrCapture
24
+ def initialize(logger, log_level = :error)
25
+ @logger = logger
26
+ @log_level = log_level
27
+ end
28
+
29
+ def write(string)
30
+ @logger.send(@log_level, string)
31
+ end
32
+
33
+ alias_method '<<', :write
34
+
35
+ def flush; end
36
+ end
37
+
38
+ end
@@ -0,0 +1,62 @@
1
+ require 'paperclip'
2
+ require 'image_optim'
3
+
4
+ module Paperclip
5
+ class PaperclipOptimizer < Processor
6
+ def self.default_options
7
+ @default_options ||= ::PaperclipOptimizer::DEFAULT_OPTIONS
8
+ end
9
+
10
+ def self.default_options=(new_options)
11
+ @default_options = new_options
12
+ end
13
+
14
+ def make
15
+ src_path = File.expand_path(@file.path)
16
+
17
+ if optimizer_options[:verbose]
18
+ Paperclip.logger.info "optimizing #{src_path} with settings: #{optimizer_options.inspect}"
19
+
20
+ old_stderr = $stderr
21
+ $stderr = ::PaperclipOptimizer::StdErrCapture.new(Paperclip.logger)
22
+ end
23
+
24
+ begin
25
+ image_optim = ImageOptim.new(optimizer_options)
26
+ compressed_file_path = image_optim.optimize_image(src_path)
27
+ ensure
28
+ $stderr = old_stderr if optimizer_options[:verbose]
29
+ end
30
+
31
+ if compressed_file_path && File.exist?(compressed_file_path)
32
+ return File.open(compressed_file_path)
33
+ else
34
+ return @file
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def optimizer_options
41
+ optimizer_global_options
42
+ .deep_merge(optimizer_instance_options)
43
+ .deep_merge(optimizer_style_options)
44
+ .delete_if { |_key, value| value.nil? }
45
+ end
46
+
47
+ private
48
+
49
+ def optimizer_global_options
50
+ self.class.default_options
51
+ end
52
+
53
+ def optimizer_instance_options
54
+ instance_options = @attachment.options.fetch(:paperclip_optimizer, {})
55
+ instance_options.respond_to?(:call) ? instance_options.call(@attachment) : instance_options
56
+ end
57
+
58
+ def optimizer_style_options
59
+ @options.fetch(:paperclip_optimizer, {})
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module PaperclipOptimizer
2
+ VERSION = '3.0.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ module PaperclipOptimizer
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ desc "creates an initializer file at config/initializers for setting global options"
6
+
7
+ def create_initializer
8
+ copy_file "initializer.rb", "config/initializers/paperclip_optimizer.rb"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,76 @@
1
+ # Set global optimisation options for all Paperclip models
2
+
3
+ # By default, image_optim enables all the compression binaries it supports and
4
+ # requires them to be present on your system, failing if one is actually missing.
5
+
6
+ # We disable everything by default and ignore missing ones with `skip_missing_workers`.
7
+ # This way, should a new version add support for a library we do not yet disable here,
8
+ # things won't suddenly break.
9
+
10
+ Paperclip::PaperclipOptimizer.default_options = {
11
+ skip_missing_workers: true,
12
+ advpng: false,
13
+ gifsicle: false,
14
+ jhead: false,
15
+ jpegoptim: false,
16
+ jpegrecompress: false,
17
+ jpegtran: false,
18
+ optipng: false,
19
+ pngcrush: false,
20
+ pngout: false,
21
+ pngquant: false,
22
+ svgo: false
23
+ }
24
+
25
+ # All available image_optim options. See https://github.com/toy/image_optim for more information
26
+
27
+ # Paperclip::PaperclipOptimizer.default_options = {
28
+ # skip_missing_workers: false, # Skip workers with missing or problematic binaries (defaults to false)
29
+ # nice: 10, # Nice level (defaults to 10)
30
+ # threads: 1, # Number of threads or disable (defaults to number of processors)
31
+ # verbose: false, # Verbose output (defaults to false)
32
+ # pack: nil, # Require image_optim_pack or disable it, by default image_optim_pack will be used if available,
33
+ # # will turn on :skip-missing-workers unless explicitly disabled (defaults to nil)
34
+ # allow_lossy: false, # Allow lossy workers and optimizations (defaults to false)
35
+ # advpng: {
36
+ # level: 4 # Compression level: 0 - don't compress, 1 - fast, 2 - normal, 3 - extra, 4 - extreme (defaults to 4)
37
+ # },
38
+ # gifsicle: {
39
+ # interlace: true, # Interlace: true - interlace on, false - interlace off, nil - as is in original image
40
+ # # (defaults to running two instances, one with interlace off and one with on)
41
+ # level: 3, # Compression level: 1 - light and fast, 2 - normal, 3 - heavy (slower) (defaults to 3)
42
+ # careful: false # Avoid bugs with some software (defaults to false)
43
+ # },
44
+ # jhead: true, # no options
45
+ # jpegoptim: {
46
+ # strip: :all, # List of extra markers to strip: :comments, :exif, :iptc, :icc or :all (defaults to :all)
47
+ # max_quality: 100 # Maximum image quality factor 0..100 (defaults to 100)
48
+ # },
49
+ # jpegrecompress: {
50
+ # quality: 3 # JPEG quality preset: 0 - low, 1 - medium, 2 - high, 3 - veryhigh (defaults to 3)
51
+ # },
52
+ # jpegtran: {
53
+ # copy_chunks: false, # Copy all chunks (defaults to false)
54
+ # progressive: true, # Create progressive JPEG file (defaults to true)
55
+ # jpegrescan: false # Use jpegtran through jpegrescan, ignore progressive option (defaults to false)
56
+ # },
57
+ # optipng: {
58
+ # level: 6, # Optimization level preset: 0 is least, 7 is best (defaults to 6)
59
+ # interlace: false # Interlace: true - interlace on, false - interlace off, nil - as is in original image (defaults to false)
60
+ # },
61
+ # pngcrush: {
62
+ # chunks: :alla, # List of chunks to remove or :alla - all except tRNS/transparency or
63
+ # # :allb - all except tRNS and gAMA/gamma (defaults to :alla)
64
+ # fix: false, # Fix otherwise fatal conditions such as bad CRCs (defaults to false)
65
+ # brute: false # Brute force try all methods, very time-consuming and generally not worthwhile (defaults to false)
66
+ # },
67
+ # pngout: {
68
+ # copy_chunks: false, # Copy optional chunks (defaults to false)
69
+ # strategy: 0 # Strategy: 0 - xtreme, 1 - intense, 2 - longest Match, 3 - huffman Only, 4 - uncompressed (defaults to 0)
70
+ # },
71
+ # pngquant: {
72
+ # quality: 100..100, # min..max - don't save below min, use less colors below max (both in range 0..100; in yaml - !ruby/range 0..100) (defaults to 100..100)
73
+ # speed: 3 # speed/quality trade-off: 1 - slow, 3 - default, 11 - fast & rough (defaults to 3)
74
+ # },
75
+ # svgo: true # no options
76
+ # }
@@ -0,0 +1,92 @@
1
+
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Paperclip::PaperclipOptimizer do
6
+
7
+ def stubbed_upload_model(*args)
8
+ stubbed_model = Upload.dup
9
+
10
+ # prevent ActiveModel::Validations from blowing up when error messages are
11
+ # accessed on an anonymous class
12
+ allow(stubbed_model).to receive(:model_name) { ActiveModel::Name.new(self, nil, 'temp') }
13
+ stubbed_model.any_instance.stub(*args)
14
+
15
+ stubbed_model
16
+ end
17
+
18
+ before(:each) do
19
+ tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
20
+
21
+ Dir.mkdir(tmp_dir) unless File.directory?(tmp_dir)
22
+
23
+ Dir.entries(tmp_dir).each do |f|
24
+ next unless f.end_with?('.jpg', '.png')
25
+
26
+ FileUtils.remove_entry_secure( File.join(tmp_dir, f) )
27
+ end
28
+
29
+ Paperclip::PaperclipOptimizer.default_options = PaperclipOptimizer::DEFAULT_OPTIONS
30
+ end
31
+
32
+ it 'creates smaller JPEGs with jpegtran' do
33
+ jpg = get_fixture(:jpg)
34
+ unoptimized_upload = Upload.new(image: jpg)
35
+ jpg.close
36
+ unoptimized_upload.save
37
+
38
+ jpg = get_fixture(:jpg)
39
+ optimized_upload = stubbed_upload_model(
40
+ processor_settings: [:thumbnail, :paperclip_optimizer],
41
+ instance_settings: { jpegtran: true }
42
+ ).new(image: jpg)
43
+ jpg.close
44
+ optimized_upload.save
45
+
46
+ unoptimized_file_size = File.size(unoptimized_upload.image.path(:medium))
47
+ optimized_file_size = File.size(optimized_upload.image.path(:medium))
48
+
49
+ expect(optimized_file_size).to be < unoptimized_file_size
50
+ end
51
+
52
+ it 'creates smaller PNGs with optipng' do
53
+ png = get_fixture(:png)
54
+ unoptimized_upload = Upload.new(image: png)
55
+ unoptimized_upload.save
56
+ png.close
57
+
58
+ png = get_fixture(:png)
59
+ optimized_upload = stubbed_upload_model(
60
+ processor_settings: [:thumbnail, :paperclip_optimizer],
61
+ instance_settings: { optipng: true }
62
+ ).new(image: png)
63
+ png.close
64
+ optimized_upload.save
65
+
66
+ unoptimized_file_size = File.size(unoptimized_upload.image.path(:medium))
67
+ optimized_file_size = File.size(optimized_upload.image.path(:medium))
68
+
69
+ expect(optimized_file_size).to be < unoptimized_file_size
70
+ end
71
+
72
+ it 'merges global, per-model and per-style options' do
73
+ global_options = PaperclipOptimizer::DEFAULT_OPTIONS.merge( gifsicle: { interlace: true }, jpegtran: true )
74
+ final_merged_options = PaperclipOptimizer::DEFAULT_OPTIONS.merge( jpegtran: true, optipng: true )
75
+
76
+ Paperclip::PaperclipOptimizer.default_options = global_options
77
+
78
+ expect(ImageOptim).to receive(:new).with(final_merged_options).and_call_original
79
+
80
+ jpg = get_fixture(:jpg)
81
+
82
+ stubbed_upload_model(
83
+ processor_settings: [:paperclip_optimizer],
84
+ instance_settings: { gifsicle: false },
85
+ style_settings: {
86
+ medium: {
87
+ paperclip_optimizer: { optipng: true }
88
+ }
89
+ }
90
+ ).new(image: jpg)
91
+ end
92
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table "uploads", :force => true do |t|
3
+ t.string :image_file_name
4
+ t.string :image_content_type
5
+ t.integer :image_updated_at
6
+ t.integer :image_file_size
7
+ end
8
+ end
@@ -0,0 +1,58 @@
1
+ require 'paperclip'
2
+ require 'paperclip/railtie'
3
+ # require 'filemagic'
4
+ require 'paperclip-optimizer'
5
+
6
+ require 'rspec'
7
+
8
+ require 'active_record'
9
+
10
+ # taken from https://github.com/tienle/docsplit-paperclip-processor/blob/master/spec/spec_helper.rb
11
+
12
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
13
+
14
+ ActiveRecord::Base.logger = Logger.new(nil)
15
+
16
+ load(File.join(File.dirname(__FILE__), 'schema.rb'))
17
+
18
+ Paperclip::Railtie.insert
19
+
20
+ class Upload < ActiveRecord::Base
21
+ self.table_name = 'uploads'
22
+
23
+ has_attached_file :image,
24
+ storage: :filesystem,
25
+ path: './spec/tmp/:id.:extension',
26
+ url: '/spec/tmp/:id.:extension',
27
+ styles: ->(attachment) { attachment.instance.style_settings },
28
+ processors: ->(instance) { instance.processor_settings },
29
+ paperclip_optimizer: ->(attachment) { attachment.instance.instance_settings}
30
+
31
+ if self.respond_to?(:do_not_validate_attachment_file_type)
32
+ do_not_validate_attachment_file_type :image
33
+ end
34
+
35
+ def style_settings
36
+ {
37
+ medium: { geometry: '500x500>' }
38
+ }
39
+ end
40
+
41
+ def instance_settings
42
+ {
43
+
44
+ }
45
+ end
46
+
47
+ def processor_settings
48
+ [:thumbnail]
49
+ end
50
+ end
51
+
52
+ def get_fixture(file_type = :jpg, valid = 'valid')
53
+ file_name = "#{valid}.#{file_type}"
54
+ fixtures_dir = File.join(File.dirname(__FILE__), '../fixtures')
55
+ fixture_path = File.join(fixtures_dir, file_name)
56
+
57
+ File.open(fixture_path)
58
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kt-paperclip-optimizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
5
+ platform: ruby
6
+ authors:
7
+ - reedstonefood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kt-paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_optim
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.19'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.19'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.20
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.20
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.4.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.4.2
111
+ description: kt-paperclip-optimizer is a Paperclip processor for optimizing and minifying
112
+ uploaded images.
113
+ email:
114
+ - hello@loco2.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".ruby-gemset"
123
+ - ".ruby-version"
124
+ - ".travis.yml"
125
+ - CHANGELOG.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - fixtures/invalid.jpg
131
+ - fixtures/invalid.png
132
+ - fixtures/valid.jpg
133
+ - fixtures/valid.png
134
+ - kt-paperclip-optimizer.gemspec
135
+ - lib/kt-paperclip-optimizer.rb
136
+ - lib/paperclip-optimizer.rb
137
+ - lib/paperclip-optimizer/processor.rb
138
+ - lib/paperclip-optimizer/version.rb
139
+ - lib/rails/generators/paperclip_optimizer/install/install_generator.rb
140
+ - lib/rails/generators/paperclip_optimizer/install/templates/initializer.rb
141
+ - spec/paperclip-optimizer_spec.rb
142
+ - spec/schema.rb
143
+ - spec/spec_helper.rb
144
+ homepage: https://github.com/loco2/kt-paperclip-optimizer
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message: "\nInitializer\n-----------\n\nrun\n\n rails g kt_paperclip_optimizer:install\n\nto
149
+ create an initializer for global configuration.\n\nBreaking changes from 1.0.3\n---------------------------\n\nBy
150
+ default, all optimization libraries are now disabled. Re-enable jpegtran and optipng
151
+ manually \nif you wish to retain the behaviour of PaperclipOptimizer 1.0.3.\n\nSee
152
+ https://github.com/janfoeh/paperclip-optimizer#settings for more information on
153
+ the new \nconfiguration system.\n\n=============================================\nIMPORTANT
154
+ - READ THIS - IMPORTANT - READ THIS\n=============================================\n\nPaperclipOptimizer
155
+ uses image_optim to do the heavy lifting. image_optim automatically inserts itself
156
+ \ninto the asset pipeline and tries to compress your /app/assets/images as well.
157
+ By default, it enables \nall the optimization libraries it supports, and it will
158
+ fail if you do not have all of them installed.\n\nEither add\n\n config.assets.image_optim
159
+ = false\n \nto your config/application.rb to disable this, or check https://github.com/toy/image_optim#from-rails
160
+ \nfor how to configure this properly.\n"
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubygems_version: 3.1.2
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: Minify Paperclip image attachments like JPGs, GIFs or PNGs
179
+ test_files:
180
+ - spec/paperclip-optimizer_spec.rb
181
+ - spec/schema.rb
182
+ - spec/spec_helper.rb