paperclip-optimizer 1.0.3 → 2.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +12 -0
- data/README.md +108 -22
- data/Rakefile +1 -1
- data/lib/paperclip-optimizer.rb +16 -8
- data/lib/paperclip-optimizer/processor.rb +41 -11
- data/lib/paperclip-optimizer/version.rb +1 -1
- data/lib/rails/generators/paperclip_optimizer/install/install_generator.rb +11 -0
- data/lib/rails/generators/paperclip_optimizer/install/templates/initializer.rb +76 -0
- data/paperclip-optimizer.gemspec +51 -19
- data/spec/paperclip-optimizer_spec.rb +30 -25
- data/spec/spec_helper.rb +21 -16
- metadata +44 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c303fcd4e0213be8b4b53d2d6bdcc71df03164a8
|
4
|
+
data.tar.gz: 80a353ec95d63ffd9bad08a797c2812c86a79ff2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c360b3f0331e4f3a3dc1ed367e6d530c970dbde08e4af41ec2742d222e60f4baaeb45c7007202c627f82c92cae83c055f15dd0c4b0d2c7b38bfe5615738fc22
|
7
|
+
data.tar.gz: fa8fc637d6c714dfdbe6a4b34bd8cdef0af78acd98f83cd8e4483a183383b5a602d2e03ad4b00522563a62538c01951299d931059901e83e2202e818f4a9cef5
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 2.0.0.beta
|
4
|
+
|
5
|
+
* **all available optimization libraries are disabled by default**
|
6
|
+
|
7
|
+
Previous versions enabled jpegtran and optipng by default. You will have to
|
8
|
+
re-enable them manually if you wish to retain that behaviour
|
9
|
+
|
10
|
+
* configure PaperclipOptimizer globally, per-attachment and per-style
|
11
|
+
|
12
|
+
Thanks to [danschultzer](https://github.com/danschultzer), [braindeaf](https://github.com/braindeaf) and
|
13
|
+
[tirdadc](https://github.com/tirdadc) for pull requests, input and reports
|
14
|
+
|
3
15
|
## 1.0.3
|
4
16
|
|
5
17
|
* updated tests, compatibility with Paperclip 4 - thanks [Sija](https://github.com/Sija)
|
data/README.md
CHANGED
@@ -4,8 +4,8 @@
|
|
4
4
|
[![Build Status](https://travis-ci.org/janfoeh/paperclip-optimizer.png)](https://travis-ci.org/janfoeh/paperclip-optimizer)
|
5
5
|
[![Dependency Status](https://gemnasium.com/janfoeh/paperclip-optimizer.png)](https://gemnasium.com/janfoeh/paperclip-optimizer)
|
6
6
|
|
7
|
-
PaperclipOptimizer is a
|
8
|
-
|
7
|
+
PaperclipOptimizer is a [Paperclip](https://github.com/thoughtbot/paperclip) processor for
|
8
|
+
optimizing and minifying uploaded images.
|
9
9
|
|
10
10
|
It is just a thin wrapper around [ImageOptim](https://github.com/toy/image_optim),
|
11
11
|
which supports many external optimization libraries like
|
@@ -13,14 +13,30 @@ which supports many external optimization libraries like
|
|
13
13
|
* [advpng](http://advancemame.sourceforge.net/doc-advpng.html) from
|
14
14
|
[AdvanceCOMP](http://advancemame.sourceforge.net/comp-readme.html)
|
15
15
|
* [gifsicle](http://www.lcdf.org/gifsicle/)
|
16
|
+
* [jhead](http://www.sentex.net/~mwandel/jhead/)
|
16
17
|
* [jpegoptim](http://www.kokkonen.net/tjko/projects.html)
|
18
|
+
* [jpeg-recompress](https://github.com/danielgtaylor/jpeg-archive#jpeg-recompress)
|
17
19
|
* jpegtran from [Independent JPEG Group's JPEG library](http://www.ijg.org/)
|
18
20
|
* [optipng](http://optipng.sourceforge.net/)
|
19
21
|
* [pngcrush](http://pmt.sourceforge.net/pngcrush/)
|
20
22
|
* [pngout](http://www.advsys.net/ken/util/pngout.htm)
|
23
|
+
* [pngquant](http://pngquant.org/)
|
24
|
+
* [svgo](https://github.com/svg/svgo)
|
21
25
|
|
22
26
|
### What's new
|
23
27
|
|
28
|
+
**2014-10-13 2.0.0.beta released**
|
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
|
+
* **all available optimization libraries are disabled by default**
|
35
|
+
|
36
|
+
Previous versions enabled jpegtran and optipng by default. You will have to
|
37
|
+
re-enable them manually if you wish to retain that behaviour
|
38
|
+
* optimizers which are enabled but missing or broken are ignored by default
|
39
|
+
|
24
40
|
**2014-05-02 1.0.3 released**
|
25
41
|
|
26
42
|
* updated tests, compatibility with Paperclip 4 - thanks [Sija](https://github.com/Sija)
|
@@ -33,7 +49,7 @@ Read the [CHANGELOG](CHANGELOG.md) for previous changes.
|
|
33
49
|
|
34
50
|
### Dependencies
|
35
51
|
|
36
|
-
PaperclipOptimizer is currently compatible with Paperclip
|
52
|
+
PaperclipOptimizer is currently compatible with Paperclip 4.2.x.
|
37
53
|
|
38
54
|
## Installation
|
39
55
|
|
@@ -44,6 +60,29 @@ Add this line to your application's Gemfile after the Paperclip gem:
|
|
44
60
|
And then execute:
|
45
61
|
|
46
62
|
$ bundle
|
63
|
+
|
64
|
+
If you wish to set global configuration settings, run
|
65
|
+
|
66
|
+
$ rails generate paperclip_optimizer:install
|
67
|
+
|
68
|
+
to generate an initializer in config/initializers.
|
69
|
+
|
70
|
+
### Caution
|
71
|
+
|
72
|
+
image_optim inserts itself into the asset pipeline and tries to compress your applications image assets as well.
|
73
|
+
Since it enables all libraries it supports by default, you might suddenly run into errors if you do not have all
|
74
|
+
of them installed.
|
75
|
+
|
76
|
+
Please note: image_optim settings made through PaperclipOptimizer do not apply here.
|
77
|
+
|
78
|
+
To disable image_optim in your asset pipeline, add
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# config/application.rb
|
82
|
+
config.assets.image_optim = false
|
83
|
+
```
|
84
|
+
|
85
|
+
to your config/application.rb.
|
47
86
|
|
48
87
|
See [ImageOptims README](https://github.com/toy/image_optim#binaries-location)
|
49
88
|
on how to install the various optimization libraries.
|
@@ -58,46 +97,93 @@ optimization binaries, compiled and ready for Herokus environment.
|
|
58
97
|
|
59
98
|
## Usage
|
60
99
|
|
61
|
-
Just add
|
100
|
+
Just add `:paperclip_optimizer` to Paperclips' `:processors` - setting:
|
62
101
|
|
63
102
|
```ruby
|
64
103
|
class User < ActiveRecord::Base
|
65
104
|
attr_accessible :avatar
|
66
105
|
has_attached_file :avatar,
|
67
|
-
:
|
68
|
-
:
|
69
|
-
:processors => [:thumbnail, :paperclip_optimizer]
|
106
|
+
styles: { thumb: "100x100>" },
|
107
|
+
processors: [:thumbnail, :paperclip_optimizer]
|
70
108
|
end
|
71
109
|
```
|
72
110
|
|
73
|
-
Remember to include the
|
111
|
+
Remember to include the `:thumbnail` processor as well if you want to retain
|
74
112
|
Paperclips geometry functionality.
|
75
113
|
|
76
114
|
### Settings
|
77
115
|
|
78
|
-
|
79
|
-
|
116
|
+
You can pass configuration options to ImageOptim in three locations: globally, per attachment and per style.
|
117
|
+
Settings are merged, so more specific settings replace less specific ones.
|
118
|
+
|
119
|
+
**Global settings**
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
Paperclip::PaperclipOptimizer.default_options = {
|
123
|
+
skip_missing_workers: false
|
124
|
+
}
|
125
|
+
```
|
126
|
+
|
127
|
+
Run `rails generate paperclip_optimizer:install` to generate an initializer for global settings.
|
128
|
+
|
129
|
+
**Per-attachment settings**
|
80
130
|
|
81
131
|
```ruby
|
82
132
|
class User < ActiveRecord::Base
|
83
133
|
attr_accessible :avatar
|
84
|
-
has_attached_file :avatar,
|
85
|
-
:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
134
|
+
has_attached_file :avatar,
|
135
|
+
processors: [:thumbnail, :paperclip_optimizer],
|
136
|
+
paperclip_optimizer: {
|
137
|
+
pngout: { strategy: 1 }
|
138
|
+
},
|
139
|
+
styles: {
|
140
|
+
thumb: { geometry: "100x100>" },
|
141
|
+
medium: { geometry: "200x200>" },
|
142
|
+
large: { geometry: "300x300>" }
|
143
|
+
}
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
Just like Paperclips' `:styles` option, you can pass a lambda to `:paperclip_optimizer` which receives the `attachment` as an argument:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class User < ActiveRecord::Base
|
151
|
+
attr_accessible :avatar
|
152
|
+
has_attached_file :avatar,
|
153
|
+
processors: [:thumbnail, :paperclip_optimizer],
|
154
|
+
paperclip_optimizer: ->(attachment) { attachment.instance.my_model_instance_method },
|
155
|
+
styles: {
|
156
|
+
thumb: { geometry: "100x100>" },
|
157
|
+
medium: { geometry: "200x200>" },
|
158
|
+
large: { geometry: "300x300>" }
|
159
|
+
}
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
**Per-style settings**
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class User < ActiveRecord::Base
|
167
|
+
attr_accessible :avatar
|
168
|
+
has_attached_file :avatar,
|
169
|
+
processors: [:thumbnail, :paperclip_optimizer],
|
170
|
+
paperclip_optimizer: {
|
171
|
+
pngout: { strategy: 1 }
|
172
|
+
},
|
173
|
+
styles: {
|
174
|
+
thumb: { geometry: "100x100>" },
|
175
|
+
medium: { geometry: "200x200>" },
|
176
|
+
large: {
|
177
|
+
geometry: "300x300>",
|
178
|
+
paperclip_optimizer: {
|
179
|
+
pngout: { strategy: 4 }
|
92
180
|
}
|
93
181
|
}
|
94
|
-
}
|
95
|
-
:default_url => "/images/:style/missing.png",
|
96
|
-
:processors => [:thumbnail, :paperclip_optimizer]
|
182
|
+
}
|
97
183
|
end
|
98
184
|
```
|
99
185
|
|
100
|
-
See [ImageOptims options](https://github.com/toy/image_optim#options) for
|
186
|
+
See [ImageOptims options](https://github.com/toy/image_optim#options) or the initializer for
|
101
187
|
all available options.
|
102
188
|
|
103
189
|
## Contributing
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/paperclip-optimizer.rb
CHANGED
@@ -1,15 +1,23 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
|
2
|
+
require 'paperclip-optimizer/version'
|
3
|
+
require 'paperclip-optimizer/processor'
|
3
4
|
|
4
5
|
module PaperclipOptimizer
|
5
|
-
|
6
|
-
|
7
|
-
pngout: false,
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
skip_missing_workers: true,
|
8
8
|
advpng: false,
|
9
|
+
gifsicle: false,
|
10
|
+
jhead: false,
|
9
11
|
jpegoptim: false,
|
10
|
-
|
12
|
+
jpegrecompress: false,
|
13
|
+
jpegtran: false,
|
14
|
+
optipng: false,
|
15
|
+
pngcrush: false,
|
16
|
+
pngout: false,
|
17
|
+
pngquant: false,
|
18
|
+
svgo: false
|
11
19
|
}.freeze
|
12
|
-
|
20
|
+
|
13
21
|
# Helper class for capturing ImageOptims error output and redirecting it
|
14
22
|
# to Paperclips logger instance
|
15
23
|
class StdErrCapture
|
@@ -22,7 +30,7 @@ module PaperclipOptimizer
|
|
22
30
|
@logger.send(@log_level, string)
|
23
31
|
end
|
24
32
|
|
25
|
-
alias_method
|
33
|
+
alias_method '<<', :write
|
26
34
|
|
27
35
|
def flush; end
|
28
36
|
end
|
@@ -1,32 +1,62 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'paperclip'
|
2
|
+
require 'image_optim'
|
3
3
|
|
4
4
|
module Paperclip
|
5
|
-
class PaperclipOptimizer < Processor
|
6
|
-
def
|
7
|
-
|
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
|
8
13
|
|
14
|
+
def make
|
9
15
|
src_path = File.expand_path(@file.path)
|
10
16
|
|
11
|
-
if
|
12
|
-
Paperclip.logger.info "optimizing #{src_path} with settings: #{
|
17
|
+
if optimizer_options[:verbose]
|
18
|
+
Paperclip.logger.info "optimizing #{src_path} with settings: #{optimizer_options.inspect}"
|
13
19
|
|
14
20
|
old_stderr = $stderr
|
15
21
|
$stderr = ::PaperclipOptimizer::StdErrCapture.new(Paperclip.logger)
|
16
22
|
end
|
17
23
|
|
18
24
|
begin
|
19
|
-
image_optim = ImageOptim.new(
|
25
|
+
image_optim = ImageOptim.new(optimizer_options)
|
20
26
|
compressed_file_path = image_optim.optimize_image(src_path)
|
21
27
|
ensure
|
22
|
-
$stderr = old_stderr if
|
28
|
+
$stderr = old_stderr if optimizer_options[:verbose]
|
23
29
|
end
|
24
30
|
|
25
|
-
if compressed_file_path && File.
|
31
|
+
if compressed_file_path && File.exist?(compressed_file_path)
|
26
32
|
return File.open(compressed_file_path)
|
27
33
|
else
|
28
34
|
return @file
|
29
35
|
end
|
30
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
|
31
61
|
end
|
32
|
-
end
|
62
|
+
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
|
+
# leve: 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
|
+
# }
|
data/paperclip-optimizer.gemspec
CHANGED
@@ -4,25 +4,57 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'paperclip-optimizer/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'paperclip-optimizer'
|
8
8
|
spec.version = PaperclipOptimizer::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
15
|
-
|
16
|
-
spec.add_runtime_dependency
|
17
|
-
spec.add_runtime_dependency
|
18
|
-
|
19
|
-
spec.add_development_dependency
|
20
|
-
spec.add_development_dependency
|
21
|
-
spec.add_development_dependency 'rspec',
|
22
|
-
spec.add_development_dependency 'rails',
|
23
|
-
spec.add_development_dependency 'sqlite3',
|
24
|
-
|
25
|
-
|
9
|
+
spec.authors = ['Jan-Christian Föh']
|
10
|
+
spec.email = ['jan@programmanstalt.de']
|
11
|
+
spec.description = '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/janfoeh/paperclip-optimizer'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.add_runtime_dependency 'paperclip', '>= 4.2'
|
17
|
+
spec.add_runtime_dependency 'image_optim', '~> 0.19.0'
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.1'
|
21
|
+
spec.add_development_dependency 'rspec', '~> 2.13'
|
22
|
+
spec.add_development_dependency 'rails', '>= 3.2.20', '<= 4.2.0'
|
23
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3.7'
|
24
|
+
spec.add_development_dependency 'rubocop', '~> 0.26.1'
|
25
|
+
|
26
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
26
27
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
27
|
-
spec.require_paths = [
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.post_install_message = %q{
|
31
|
+
Initializer
|
32
|
+
-----------
|
33
|
+
|
34
|
+
run
|
35
|
+
|
36
|
+
rails g paperclip_optimizer:install
|
37
|
+
|
38
|
+
to create an initializer for global configuration.
|
39
|
+
|
40
|
+
Breaking changes from 1.0.3
|
41
|
+
---------------------------
|
42
|
+
|
43
|
+
Please note that Paperclip 2 now disables all optimization libraries by default. Re-enable jpegtran
|
44
|
+
and optipng manually if you wish to retain the previous behaviour.
|
45
|
+
|
46
|
+
See https://github.com/janfoeh/paperclip-optimizer#settings for more information on the new
|
47
|
+
configuration system.
|
48
|
+
|
49
|
+
PLEASE NOTE
|
50
|
+
-----------
|
51
|
+
|
52
|
+
PaperclipOptimizer depends on image_optim, which by default inserts itself into the asset pipeline and
|
53
|
+
tries to compress your application assets. This might fail if you do not have all supported optimization
|
54
|
+
binaries installed. Add
|
55
|
+
|
56
|
+
config.assets.image_optim = false
|
57
|
+
|
58
|
+
to your config/application.rb to disable this.
|
59
|
+
}
|
28
60
|
end
|
@@ -1,14 +1,15 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'fileutils'
|
3
4
|
|
4
5
|
describe Paperclip::PaperclipOptimizer do
|
5
6
|
|
6
7
|
def stubbed_upload_model(*args)
|
7
8
|
stubbed_model = Upload.dup
|
8
9
|
|
9
|
-
# prevent ActiveModel::Validations from blowing up when error messages are
|
10
|
+
# prevent ActiveModel::Validations from blowing up when error messages are
|
10
11
|
# accessed on an anonymous class
|
11
|
-
stubbed_model.stub(:
|
12
|
+
stubbed_model.stub( model_name: ActiveModel::Name.new(self, nil, 'temp') )
|
12
13
|
stubbed_model.any_instance.stub(*args)
|
13
14
|
|
14
15
|
stubbed_model
|
@@ -18,24 +19,27 @@ describe Paperclip::PaperclipOptimizer do
|
|
18
19
|
tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
19
20
|
|
20
21
|
Dir.mkdir(tmp_dir) unless File.directory?(tmp_dir)
|
21
|
-
|
22
|
+
|
22
23
|
Dir.entries(tmp_dir).each do |f|
|
23
|
-
next unless f.end_with?(
|
24
|
+
next unless f.end_with?('.jpg', '.png')
|
24
25
|
|
25
26
|
FileUtils.remove_entry_secure( File.join(tmp_dir, f) )
|
26
27
|
end
|
28
|
+
|
29
|
+
Paperclip::PaperclipOptimizer.default_options = PaperclipOptimizer::DEFAULT_OPTIONS
|
27
30
|
end
|
28
31
|
|
29
|
-
it
|
32
|
+
it 'creates smaller JPEGs with jpegtran' do
|
30
33
|
jpg = get_fixture(:jpg)
|
31
|
-
unoptimized_upload = Upload.new(:
|
34
|
+
unoptimized_upload = Upload.new(image: jpg)
|
32
35
|
jpg.close
|
33
36
|
unoptimized_upload.save
|
34
37
|
|
35
38
|
jpg = get_fixture(:jpg)
|
36
39
|
optimized_upload = stubbed_upload_model(
|
37
|
-
processor_settings: [:thumbnail, :paperclip_optimizer]
|
38
|
-
|
40
|
+
processor_settings: [:thumbnail, :paperclip_optimizer],
|
41
|
+
instance_settings: { jpegtran: true }
|
42
|
+
).new(image: jpg)
|
39
43
|
jpg.close
|
40
44
|
optimized_upload.save
|
41
45
|
|
@@ -45,16 +49,17 @@ describe Paperclip::PaperclipOptimizer do
|
|
45
49
|
expect(optimized_file_size).to be < unoptimized_file_size
|
46
50
|
end
|
47
51
|
|
48
|
-
it
|
52
|
+
it 'creates smaller PNGs with optipng' do
|
49
53
|
png = get_fixture(:png)
|
50
|
-
unoptimized_upload = Upload.new(:
|
54
|
+
unoptimized_upload = Upload.new(image: png)
|
51
55
|
unoptimized_upload.save
|
52
56
|
png.close
|
53
57
|
|
54
58
|
png = get_fixture(:png)
|
55
59
|
optimized_upload = stubbed_upload_model(
|
56
|
-
processor_settings: [:thumbnail, :paperclip_optimizer]
|
57
|
-
|
60
|
+
processor_settings: [:thumbnail, :paperclip_optimizer],
|
61
|
+
instance_settings: { optipng: true }
|
62
|
+
).new(image: png)
|
58
63
|
png.close
|
59
64
|
optimized_upload.save
|
60
65
|
|
@@ -64,24 +69,24 @@ describe Paperclip::PaperclipOptimizer do
|
|
64
69
|
expect(optimized_file_size).to be < unoptimized_file_size
|
65
70
|
end
|
66
71
|
|
67
|
-
it
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
71
77
|
|
72
|
-
ImageOptim.should_receive(:new).with(
|
78
|
+
ImageOptim.should_receive(:new).with(final_merged_options).and_call_original
|
73
79
|
|
74
80
|
jpg = get_fixture(:jpg)
|
75
81
|
|
76
82
|
stubbed_upload_model(
|
77
83
|
processor_settings: [:paperclip_optimizer],
|
78
|
-
|
84
|
+
instance_settings: { gifsicle: false },
|
85
|
+
style_settings: {
|
79
86
|
medium: {
|
80
|
-
paperclip_optimizer: {
|
81
|
-
gifsicle: {:interlace => true}
|
82
|
-
}
|
87
|
+
paperclip_optimizer: { optipng: true }
|
83
88
|
}
|
84
89
|
}
|
85
|
-
).new(:
|
90
|
+
).new(image: jpg)
|
86
91
|
end
|
87
|
-
end
|
92
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,37 +6,42 @@ require 'paperclip-optimizer'
|
|
6
6
|
require 'rspec'
|
7
7
|
require 'rspec/autorun'
|
8
8
|
|
9
|
-
require
|
9
|
+
require 'active_record'
|
10
10
|
|
11
11
|
# taken from https://github.com/tienle/docsplit-paperclip-processor/blob/master/spec/spec_helper.rb
|
12
12
|
|
13
|
-
ActiveRecord::Base.establish_connection
|
14
|
-
"adapter" => "sqlite3",
|
15
|
-
"database" => ":memory:"
|
16
|
-
)
|
13
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
17
14
|
|
18
15
|
ActiveRecord::Base.logger = Logger.new(nil)
|
16
|
+
|
19
17
|
load(File.join(File.dirname(__FILE__), 'schema.rb'))
|
20
18
|
|
21
19
|
Paperclip::Railtie.insert
|
22
20
|
|
23
21
|
class Upload < ActiveRecord::Base
|
24
|
-
self.table_name =
|
22
|
+
self.table_name = 'uploads'
|
25
23
|
|
26
24
|
has_attached_file :image,
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
storage: :filesystem,
|
26
|
+
path: './spec/tmp/:id.:extension',
|
27
|
+
url: '/spec/tmp/:id.:extension',
|
28
|
+
styles: ->(attachment) { attachment.instance.style_settings },
|
29
|
+
processors: ->(instance) { instance.processor_settings },
|
30
|
+
paperclip_optimizer: ->(attachment) { attachment.instance.instance_settings}
|
32
31
|
|
33
32
|
if self.respond_to?(:do_not_validate_attachment_file_type)
|
34
33
|
do_not_validate_attachment_file_type :image
|
35
34
|
end
|
36
35
|
|
37
36
|
def style_settings
|
38
|
-
{
|
39
|
-
medium: { geometry:
|
37
|
+
{
|
38
|
+
medium: { geometry: '500x500>' }
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def instance_settings
|
43
|
+
{
|
44
|
+
|
40
45
|
}
|
41
46
|
end
|
42
47
|
|
@@ -45,9 +50,9 @@ class Upload < ActiveRecord::Base
|
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
48
|
-
def get_fixture(file_type = :jpg, valid =
|
49
|
-
file_name = "#{valid}.#{file_type
|
50
|
-
fixtures_dir = File.join(File.dirname(__FILE__),
|
53
|
+
def get_fixture(file_type = :jpg, valid = 'valid')
|
54
|
+
file_name = "#{valid}.#{file_type}"
|
55
|
+
fixtures_dir = File.join(File.dirname(__FILE__), '../fixtures')
|
51
56
|
fixture_path = File.join(fixtures_dir, file_name)
|
52
57
|
|
53
58
|
File.open(fixture_path)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paperclip-optimizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.beta
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan-Christian Föh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: paperclip
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: image_optim
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.19.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 0.19.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,20 +86,20 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 3.2.
|
90
|
-
- - "
|
89
|
+
version: 3.2.20
|
90
|
+
- - "<="
|
91
91
|
- !ruby/object:Gem::Version
|
92
|
-
version: 4.
|
92
|
+
version: 4.2.0
|
93
93
|
type: :development
|
94
94
|
prerelease: false
|
95
95
|
version_requirements: !ruby/object:Gem::Requirement
|
96
96
|
requirements:
|
97
97
|
- - ">="
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version: 3.2.
|
100
|
-
- - "
|
99
|
+
version: 3.2.20
|
100
|
+
- - "<="
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: 4.
|
102
|
+
version: 4.2.0
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: sqlite3
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,8 +114,22 @@ dependencies:
|
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: 1.3.7
|
117
|
-
|
118
|
-
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rubocop
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 0.26.1
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 0.26.1
|
131
|
+
description: paperclip-optimizer is a Paperclip processor for optimizing and minifying
|
132
|
+
uploaded images.
|
119
133
|
email:
|
120
134
|
- jan@programmanstalt.de
|
121
135
|
executables: []
|
@@ -124,6 +138,7 @@ extra_rdoc_files: []
|
|
124
138
|
files:
|
125
139
|
- ".gitignore"
|
126
140
|
- ".rspec"
|
141
|
+
- ".rubocop.yml"
|
127
142
|
- ".ruby-gemset"
|
128
143
|
- ".ruby-version"
|
129
144
|
- ".travis.yml"
|
@@ -139,6 +154,8 @@ files:
|
|
139
154
|
- lib/paperclip-optimizer.rb
|
140
155
|
- lib/paperclip-optimizer/processor.rb
|
141
156
|
- lib/paperclip-optimizer/version.rb
|
157
|
+
- lib/rails/generators/paperclip_optimizer/install/install_generator.rb
|
158
|
+
- lib/rails/generators/paperclip_optimizer/install/templates/initializer.rb
|
142
159
|
- paperclip-optimizer.gemspec
|
143
160
|
- spec/paperclip-optimizer_spec.rb
|
144
161
|
- spec/schema.rb
|
@@ -147,7 +164,16 @@ homepage: https://github.com/janfoeh/paperclip-optimizer
|
|
147
164
|
licenses:
|
148
165
|
- MIT
|
149
166
|
metadata: {}
|
150
|
-
post_install_message:
|
167
|
+
post_install_message: "\nInitializer\n-----------\n\nrun\n\n rails g paperclip_optimizer:install\n\nto
|
168
|
+
create an initializer for global configuration.\n\nBreaking changes from 1.0.3\n---------------------------\n\nPlease
|
169
|
+
note that Paperclip 2 now disables all optimization libraries by default. Re-enable
|
170
|
+
jpegtran\nand optipng manually if you wish to retain the previous behaviour.\n\nSee
|
171
|
+
https://github.com/janfoeh/paperclip-optimizer#settings for more information on
|
172
|
+
the new \nconfiguration system.\n\nPLEASE NOTE\n-----------\n\nPaperclipOptimizer
|
173
|
+
depends on image_optim, which by default inserts itself into the asset pipeline
|
174
|
+
and \ntries to compress your application assets. This might fail if you do not have
|
175
|
+
all supported optimization \nbinaries installed. Add\n\n config.assets.image_optim
|
176
|
+
= false\n \nto your config/application.rb to disable this.\n"
|
151
177
|
rdoc_options: []
|
152
178
|
require_paths:
|
153
179
|
- lib
|
@@ -158,15 +184,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
184
|
version: '0'
|
159
185
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
186
|
requirements:
|
161
|
-
- - "
|
187
|
+
- - ">"
|
162
188
|
- !ruby/object:Gem::Version
|
163
|
-
version:
|
189
|
+
version: 1.3.1
|
164
190
|
requirements: []
|
165
191
|
rubyforge_project:
|
166
192
|
rubygems_version: 2.2.2
|
167
193
|
signing_key:
|
168
194
|
specification_version: 4
|
169
|
-
summary: Minify Paperclip
|
195
|
+
summary: Minify Paperclip image attachments like JPGs, GIFs or PNGs
|
170
196
|
test_files:
|
171
197
|
- spec/paperclip-optimizer_spec.rb
|
172
198
|
- spec/schema.rb
|