carrierwave-optimize_image 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e3cc95b0412f0cbe6b6f47dbd7837ee625cd484
4
+ data.tar.gz: b81a6e963eec254af21acdeb732a88152065f1e2
5
+ SHA512:
6
+ metadata.gz: 8fb59213ea4fd79a9b38d71d866af1cced81975a162e065c5a23677a6f193ac7fb8411d5331b02afea61414bf3fb3b00e33408f744468d1b0de5f45a08cb6465
7
+ data.tar.gz: 6303e1f19462f126475615a36614bd74fc36894e683b93082e4ab3117685e06de61f012ce2c954cef94cf1bb5dee59777f97ffdb5a049b0656562f2671e45ba1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /tmp/
6
+ /pkg/
7
+ /spec/reports/
8
+ /coverage/
9
+ /doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.3.0
5
+ gemfile:
6
+ - Gemfile
7
+ - gemfiles/activesupport5.gemfile
8
+ before_install:
9
+ - sudo apt-get install jpegoptim pngquant
10
+ - gem update bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in carrierwave-optimize_image.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kazuki Yamaguchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # CarrierWave OptimizeImage
2
+
3
+ This gem allows you to simply optimize [CarrierWave](https://github.com/carrierwaveuploader/carrierwave) images via jpegoptim or optipng using the image_optimizer gem.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ **This gem uses the following utilities for optimizing images:**
9
+
10
+ - Install for [pngquant](https://github.com/pornel/pngquant)
11
+ - Install for [jpegoptim](https://github.com/tjko/jpegoptim)
12
+
13
+ Or install the utilities via homebrew:
14
+
15
+ $ brew install pngquant jpegoptim
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem "carrierwave-optimize_image"
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install carrierwave-optimize_image
30
+
31
+
32
+ ## Usage
33
+
34
+ To add image optimization to your CarrierWave uploader, first include the module:
35
+
36
+ ```ruby
37
+ class ImageUploader < CarrierWave::Uploader::Base
38
+ include CarrierWave::OptimizeImage
39
+ ...
40
+ end
41
+ ```
42
+
43
+ Then apply to all versions via:
44
+
45
+ ```ruby
46
+ class ImageUploader < CarrierWave::Uploader::Base
47
+ include CarrierWave::OptimizeImage
48
+ ...
49
+ process :optimize
50
+ end
51
+ ```
52
+
53
+ Or to a single image version via:
54
+
55
+ ```ruby
56
+ class ImageUploader < CarrierWave::Uploader::Base
57
+ ...
58
+ version :thumbnail do
59
+ process :optimize
60
+ end
61
+ end
62
+ ```
63
+
64
+ Pass an optional quality parameter to target a specific lossy JPG and PNG quality level (0-100)
65
+
66
+ ```ruby
67
+ class ImageUploader < CarrierWave::Uploader::Base
68
+ version :thumbnail do
69
+ process optimize: [{ quality: 60 }]
70
+ end
71
+ end
72
+ ```
73
+
74
+ **Quiet Mode**
75
+
76
+ Pass an optional ```quiet``` parameter to compress images without logging progress.
77
+
78
+ ```ruby
79
+ class ImageUploader < CarrierWave::Uploader::Base
80
+ ...
81
+ version :thumbnail do
82
+ process optimize: [{ quiet: true }]
83
+ end
84
+ end
85
+ ```
86
+
87
+
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b new_feature_branch`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin new_feature_branch`)
95
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ begin
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ rescue LoadError
8
+ end
9
+
10
+ task default: :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "carrierwave/optimize_image/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "carrierwave-optimize_image"
7
+ spec.version = CarrierWave::OptimizeImage::VERSION
8
+ spec.authors = ["kazuki YAMAGUCHI"]
9
+ spec.email = ["y.kazuki0614n@gmail.com"]
10
+ spec.homepage = "https://github.com/yamakichi/carrierwave-optimize_image"
11
+ spec.description = %q{A simple image optimize for CarrierWave}
12
+ spec.summary = %q{Simple optimize CarrierWave via jpegoptim and pngquant}
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files`.split("\n")
15
+ spec.require_paths = ["lib"]
16
+ spec.required_ruby_version = '>= 2.2.0'
17
+
18
+ spec.add_development_dependency "rake"
19
+ spec.add_development_dependency "rspec"
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "mini_magick"
22
+ spec.add_development_dependency "carrierwave"
23
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activesupport", "~> 5.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave::OptimizeImage
4
+ class OptimizeError < StandardError; end
5
+ class NotFoundPngquant < OptimizeError; end
6
+ class NotFoundJpegoptim < OptimizeError; end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "carrierwave/optimize_image/image_optimizer"
4
+ require "carrierwave/optimize_image/version"
5
+ require "carrierwave/exceptions"
6
+
7
+ module CarrierWave
8
+ module OptimizeImage
9
+ def optimize(opts = {})
10
+ ImageOptimizer.optimize(current_path, opts)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module OptimizeImage
5
+ module ImageOptimizer
6
+ class << self
7
+ def optimize(path, opts = {})
8
+ raise NotFoundPngquant unless which_pngquant?
9
+ raise NotFoundJpegoptim unless which_jpegoptim?
10
+ optimize_for(path, opts)
11
+ end
12
+
13
+ private
14
+
15
+ def which_pngquant?
16
+ system! "which pngquant"
17
+ end
18
+
19
+ def which_jpegoptim?
20
+ system! "which jpegoptim"
21
+ end
22
+
23
+ def optimize_for(path, opts)
24
+ case mimetype(path)
25
+ when "jpeg" then optimize_jpg(path, opts)
26
+ when "png" then optimize_png(path, opts)
27
+ end
28
+ end
29
+
30
+ def mimetype(path)
31
+ IO.popen(["file", "--brief", "--mime-type", path], in: :close, err: :close) { |io| io.read.chomp.sub(/image\//, "") }
32
+ end
33
+
34
+ def system!(*args)
35
+ system(*args) || abort("\n== Command #{args} failed ==")
36
+ end
37
+
38
+ def optimize_png(path, opts = {})
39
+ options = { verbose: false, quality: 100 }.merge!(opts)
40
+ quality = (0..100).include?(options[:quality]) ? options[:quality] : 100
41
+ vo = options[:verbose] ? " --verbose" : "--quiet"
42
+ path.gsub!(/([\(\)\[\]\{\}\*\?\\])/, '\\\\\1')
43
+ system! "pngquant --quality #{quality} #{vo} #{path}"
44
+ end
45
+
46
+ def optimize_jpg(path, opts = {})
47
+ options = { verbose: false, quality: 100 }.merge!(opts)
48
+ quality = (0..100).include?(options[:quality]) ? options[:quality] : 100
49
+ vo = options[:verbose] ? "--verbose" : "--quiet"
50
+ path.gsub!(/([\(\)\[\]\{\}\*\?\\])/, '\\\\\1')
51
+ system! "jpegoptim --force --max=#{quality} #{vo} #{path}"
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module OptimizeImage
5
+ VERSION = "0.2.0"
6
+ end
7
+ end
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe CarrierWave::OptimizeImage do
4
+ it "should have a VERSION constant" do
5
+ expect(subject.const_get("VERSION")).to_not be_empty
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+ require "carrierwave/optimize_image/image_optimizer"
3
+ require "carrierwave/optimize_image"
4
+
5
+ class MiniMagickUploader < CarrierWave::Uploader::Base
6
+ include CarrierWave::MiniMagick
7
+ include CarrierWave::OptimizeImage
8
+ end
9
+
10
+ describe CarrierWave::OptimizeImage::ImageOptimizer do
11
+ describe "#optimize" do
12
+ let(:sample_jpg_path) { File.join('spec', 'assets', 'sample.jpg') }
13
+ let(:sample_jpg_image) { File.open(sample_jpg_path) }
14
+
15
+ let(:sample_png_path) { File.join('spec', 'assets', 'sample.png') }
16
+ let(:sample_png_image) { File.open(sample_png_path) }
17
+
18
+ def upload_file(uploader, image)
19
+ uploader.store!(image)
20
+ uploader.manipulate! do |img|
21
+ img
22
+ end
23
+ end
24
+
25
+ it "calls the uploader's `current_path` when MiniMagick is used" do
26
+ uploader = MiniMagickUploader.new
27
+ upload_file(uploader, sample_jpg_image)
28
+ expect(CarrierWave::OptimizeImage::ImageOptimizer).to receive(:optimize).with(uploader.current_path, {})
29
+ uploader.optimize
30
+ end
31
+
32
+ it "calls `optimize_png` if the .png files MIME type is that of a PNG" do
33
+ uploader = MiniMagickUploader.new
34
+ upload_file(uploader, sample_png_image)
35
+ expect(CarrierWave::OptimizeImage::ImageOptimizer).to receive(:optimize_png).with(uploader.current_path, {})
36
+ uploader.optimize
37
+ end
38
+
39
+ it "calls `optimize_jpg` if the .jpg files MIME type is that of a JPEG" do
40
+ uploader = MiniMagickUploader.new
41
+ upload_file(uploader, sample_jpg_image)
42
+ expect(CarrierWave::OptimizeImage::ImageOptimizer).to receive(:optimize_jpg).with(uploader.current_path, {})
43
+ uploader.optimize
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ require "carrierwave"
2
+ require "carrierwave/optimize_image"
3
+ require "carrierwave/optimize_image/image_optimizer"
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-optimize_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - kazuki YAMAGUCHI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mini_magick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: carrierwave
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A simple image optimize for CarrierWave
84
+ email:
85
+ - y.kazuki0614n@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - carrierwave-optimize_image.gemspec
98
+ - gemfiles/activesupport5.gemfile
99
+ - lib/carrierwave/exceptions.rb
100
+ - lib/carrierwave/optimize_image.rb
101
+ - lib/carrierwave/optimize_image/image_optimizer.rb
102
+ - lib/carrierwave/optimize_image/version.rb
103
+ - spec/assets/sample.jpg
104
+ - spec/assets/sample.png
105
+ - spec/optimize_image/version_spec.rb
106
+ - spec/optimize_image_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: https://github.com/yamakichi/carrierwave-optimize_image
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.2.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.4
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Simple optimize CarrierWave via jpegoptim and pngquant
132
+ test_files: []