leifcr-refile-mini_magick 0.2.1

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: 73967411699252c6e946a016ed98ba58125243c7
4
+ data.tar.gz: ab8981685fa3822069962f2b343c7905fa1a6bc9
5
+ SHA512:
6
+ metadata.gz: 269097afcf4239922b2460c29904c4bec486ab97738c33d3295b83249040dc85d77d231f6f7c2a4b04b758e2ca74e8de2088efb482f7179ff94935e993fb3c80
7
+ data.tar.gz: 7ccbca17ac880a52f155993b28ad576d22fc1fbeffd7f45c6c1a0431bd6bc2d23eab62128bd7eefed591292eaa9f55a4ab8c7db38f6adaa8bc2a4124fd3fed84
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in refile-mini_magick.gemspec
4
+ gemspec
5
+
6
+ gem "refile", github: "refile/refile"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rake"
9
+ gem "pry"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jonas Nicklas
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,41 @@
1
+ # Refile::MiniMagick
2
+
3
+ Image processing for Refile using MiniMagick.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'refile-mini_magick'
9
+ ```
10
+
11
+ Note that the master branch switched to using the [ImageProcessing] gem, which
12
+ actively fixes the bugs reported on this repo, so it's recommended to pull
13
+ refile-mini_magick from master:
14
+
15
+ ```rb
16
+ gem 'refile-mini_magick', github: 'refile/refile-mini_magick'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ The following processing helpers are provided for Refile:
22
+
23
+ * /attachments/:token/:backend\_name**/convert/jpg**/:id
24
+ * /attachments/:token/:backend\_name**/limit/500/500**/:id
25
+ * /attachments/:token/:backend\_name**/fit/500/500**/:id
26
+ * /attachments/:token/:backend\_name**/fill/500/500**/:id
27
+ * /attachments/:token/:backend\_name**/pad/500/500**/:id
28
+ * /attachments/:token/:backend\_name**/resample/500/500**/:id
29
+
30
+ Refile::MiniMagick internally delegates to [ImageProcessing] dependency gem, so
31
+ you can read documentation about these processing actions on that repository.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/refile/refile-mini_magick/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
40
+
41
+ [ImageProcessing]: https://github.com/janko-m/image_processing
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: "spec"
7
+
@@ -0,0 +1,138 @@
1
+ require "refile"
2
+ require "refile/mini_magick/version"
3
+ require "image_processing/mini_magick"
4
+
5
+ module Refile
6
+ # Processes images via MiniMagick, resizing cropping and padding them.
7
+ class MiniMagick
8
+ # @param [Symbol] method The method to invoke on {#call}
9
+ def initialize(method)
10
+ @method = method
11
+ end
12
+
13
+ # Changes the image encoding format to the given format
14
+ #
15
+ # @see http://www.imagemagick.org/script/command-line-options.php#format
16
+ # @param [File] img the image to convert
17
+ # @param [String] format the format to convert to
18
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
19
+ # @return [File, Tempfile]
20
+ def convert(img, format, &block)
21
+ processor.convert!(img, format, &block)
22
+ end
23
+
24
+ # Resize the image to fit within the specified dimensions while retaining
25
+ # the original aspect ratio. Will only resize the image if it is larger
26
+ # than the specified dimensions. The resulting image may be shorter or
27
+ # narrower than specified in either dimension but will not be larger than
28
+ # the specified values.
29
+ #
30
+ # @param [File] img the image to convert
31
+ # @param [#to_s] width the maximum width
32
+ # @param [#to_s] height the maximum height
33
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
34
+ # @return [File, Tempfile]
35
+ def limit(img, width, height, &block)
36
+ processor.resize_to_limit!(img, width, height, &block)
37
+ end
38
+
39
+ # Resize the image to fit within the specified dimensions while retaining
40
+ # the original aspect ratio. The image may be shorter or narrower than
41
+ # specified in the smaller dimension but will not be larger than the
42
+ # specified values.
43
+ #
44
+ # @param [File] img the image to convert
45
+ # @param [#to_s] width the width to fit into
46
+ # @param [#to_s] height the height to fit into
47
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
48
+ # @return [File, Tempfile]
49
+ def fit(img, width, height, &block)
50
+ processor.resize_to_fit!(img, width, height, &block)
51
+ end
52
+
53
+ # Resize the image so that it is at least as large in both dimensions as
54
+ # specified, then crops any excess outside the specified dimensions.
55
+ #
56
+ # The resulting image will always be exactly as large as the specified
57
+ # dimensions.
58
+ #
59
+ # By default, the center part of the image is kept, and the remainder
60
+ # cropped off, but this can be changed via the `gravity` option.
61
+ #
62
+ # @param [File] img the image to convert
63
+ # @param [#to_s] width the width to fill out
64
+ # @param [#to_s] height the height to fill out
65
+ # @param [String] gravity which part of the image to focus on
66
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
67
+ # @return [File, Tempfile]
68
+ # @see http://www.imagemagick.org/script/command-line-options.php#gravity
69
+ def fill(img, width, height, gravity = "Center", &block)
70
+ processor.resize_to_fill!(img, width, height, gravity: gravity, &block)
71
+ end
72
+
73
+ # Resize the image to fit within the specified dimensions while retaining
74
+ # the original aspect ratio in the same way as {#fill}. Unlike {#fill} it
75
+ # will, if necessary, pad the remaining area with the given color, which
76
+ # defaults to transparent where supported by the image format and white
77
+ # otherwise.
78
+ #
79
+ # The resulting image will always be exactly as large as the specified
80
+ # dimensions.
81
+ #
82
+ # By default, the image will be placed in the center but this can be
83
+ # changed via the `gravity` option.
84
+ #
85
+ # @param [MiniMagick::image] img the image to convert
86
+ # @param [#to_s] width the width to fill out
87
+ # @param [#to_s] height the height to fill out
88
+ # @param [string] background the color to use as a background
89
+ # @param [string] gravity which part of the image to focus on
90
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
91
+ # @return [File, Tempfile]
92
+ # @see http://www.imagemagick.org/script/color.php
93
+ # @see http://www.imagemagick.org/script/command-line-options.php#gravity
94
+ def pad(img, width, height, background = "transparent", gravity = "Center", &block)
95
+ processor.resize_and_pad!(img, width, height, background: background, gravity: gravity, &block)
96
+ end
97
+
98
+ # Resample the image to fit within the specified resolution while retaining
99
+ # the original image size.
100
+ #
101
+ # The resulting image will always be the same pixel size as the source with
102
+ # an adjusted resolution dimensions.
103
+ #
104
+ # @param [minimagick::image] img the image to convert
105
+ # @param [#to_s] width the dpi width
106
+ # @param [#to_s] height the dpi height
107
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
108
+ # @return [File, Tempfile]
109
+ # @see http://www.imagemagick.org/script/command-line-options.php#resample
110
+ def resample(img, width, height, &block)
111
+ processor.resample!(img, width, height, &block)
112
+ end
113
+
114
+ # Process the given file. The file will be processed via one of the
115
+ # instance methods of this class, depending on the `method` argument passed
116
+ # to the constructor on initialization.
117
+ #
118
+ # If the format is given it will convert the image to the given file format.
119
+ #
120
+ # @param [Tempfile] file the file to manipulate
121
+ # @param [String] format the file format to convert to
122
+ # @return [File] the processed file
123
+ def call(file, *args, format: nil, &block)
124
+ file = processor.convert!(file, format) if format
125
+ send(@method, file, *args, &block)
126
+ end
127
+
128
+ private
129
+
130
+ def processor
131
+ ImageProcessing::MiniMagick
132
+ end
133
+ end
134
+ end
135
+
136
+ [:fill, :fit, :limit, :pad, :convert, :resample].each do |name|
137
+ Refile.processor(name, Refile::MiniMagick.new(name))
138
+ end
@@ -0,0 +1,5 @@
1
+ module Refile
2
+ class MiniMagick
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'refile/mini_magick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "leifcr-refile-mini_magick"
8
+ spec.version = Refile::MiniMagick::VERSION
9
+ spec.authors = ["Jonas Nicklas"]
10
+ spec.email = ["jonas.nicklas@gmail.com"]
11
+ spec.summary = "Image processing via MiniMagick for Refile"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "refile", "~> 0.5"
21
+ spec.add_dependency "image_processing", ">= 0.4.1", "< 1.0"
22
+ spec.add_dependency "mini_magick", ">= 4.3.5"
23
+ end
Binary file
Binary file
@@ -0,0 +1,97 @@
1
+ require "pry"
2
+ require "refile/mini_magick"
3
+
4
+ RSpec.describe Refile::MiniMagick do
5
+ let(:portrait) { Tempfile.new(["portrait", ".jpg"]) }
6
+ let(:landscape) { Tempfile.new(["landscape", ".jpg"]) }
7
+
8
+ def fixture_path(name)
9
+ File.expand_path("./fixtures/#{name}", File.dirname(__FILE__))
10
+ end
11
+
12
+ before do
13
+ FileUtils.cp(fixture_path("portrait.jpg"), portrait.path)
14
+ FileUtils.cp(fixture_path("landscape.jpg"), landscape.path)
15
+ end
16
+
17
+ describe "#convert" do
18
+ it "changes the image format" do
19
+ file = Refile::MiniMagick.new(:convert).call(portrait, "png")
20
+ expect(::MiniMagick::Image.new(file.path).identify).to match(/PNG/)
21
+ end
22
+
23
+ it "yields the command object" do
24
+ expect { |b| Refile::MiniMagick.new(:convert).call(portrait, "png", &b) }
25
+ .to yield_with_args(MiniMagick::Tool)
26
+ end
27
+ end
28
+
29
+ describe "#limit" do
30
+ it "resizes the image up to a given limit" do
31
+ file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
32
+ result = ::MiniMagick::Image.new(file.path)
33
+ expect(result.width).to eq(300)
34
+ expect(result.height).to eq(400)
35
+ end
36
+
37
+ it "yields the command object" do
38
+ expect { |b| Refile::MiniMagick.new(:limit).call(portrait, "400", "400", &b) }
39
+ .to yield_with_args(MiniMagick::Tool)
40
+ end
41
+ end
42
+
43
+ describe "#fit" do
44
+ it "resizes the image to fit given dimensions" do
45
+ file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
46
+ result = ::MiniMagick::Image.new(file.path)
47
+ expect(result.width).to eq(300)
48
+ expect(result.height).to eq(400)
49
+ end
50
+
51
+ it "yields the command object" do
52
+ expect { |b| Refile::MiniMagick.new(:fit).call(portrait, "400", "400", &b) }
53
+ .to yield_with_args(MiniMagick::Tool)
54
+ end
55
+ end
56
+
57
+ describe "#fill" do
58
+ it "resizes and crops the image to fill out the given dimensions" do
59
+ file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
60
+ result = ::MiniMagick::Image.new(file.path)
61
+ expect(result.width).to eq(400)
62
+ expect(result.height).to eq(400)
63
+ end
64
+
65
+ it "yields the command object" do
66
+ expect { |b| Refile::MiniMagick.new(:fill).call(portrait, "400", "400", &b) }
67
+ .to yield_with_args(MiniMagick::Tool)
68
+ end
69
+ end
70
+
71
+ describe "#pad" do
72
+ it "resizes and fills out the remaining space to fill out the given dimensions" do
73
+ file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400", "red")
74
+ result = ::MiniMagick::Image.new(file.path)
75
+ expect(result.width).to eq(400)
76
+ expect(result.height).to eq(400)
77
+ end
78
+
79
+ it "yields the command object" do
80
+ expect { |b| Refile::MiniMagick.new(:pad).call(portrait, "400", "400", &b) }
81
+ .to yield_with_args(MiniMagick::Tool)
82
+ end
83
+ end
84
+
85
+ describe "#resample" do
86
+ it "downsamples high resolution images to low resolution" do
87
+ file = Refile::MiniMagick.new(:resample).call(landscape, "30", "30")
88
+ result = ::MiniMagick::Image.new(file.path)
89
+ expect(result.resolution).to eq [30, 30]
90
+ end
91
+
92
+ it "yields the command object" do
93
+ expect { |b| Refile::MiniMagick.new(:resample).call(landscape, "30", "30", &b) }
94
+ .to yield_with_args(MiniMagick::Tool)
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leifcr-refile-mini_magick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Nicklas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: refile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_processing
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.1
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '1.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.4.1
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: mini_magick
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 4.3.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 4.3.5
61
+ description:
62
+ email:
63
+ - jonas.nicklas@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - lib/refile/mini_magick.rb
74
+ - lib/refile/mini_magick/version.rb
75
+ - refile-mini_magick.gemspec
76
+ - spec/refile/fixtures/landscape.jpg
77
+ - spec/refile/fixtures/portrait.jpg
78
+ - spec/refile/mini_magick_spec.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.8
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Image processing via MiniMagick for Refile
103
+ test_files:
104
+ - spec/refile/fixtures/landscape.jpg
105
+ - spec/refile/fixtures/portrait.jpg
106
+ - spec/refile/mini_magick_spec.rb