refile-mini_magick 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10851b9908e5028f56547ef5f88631c42c8833d6
4
+ data.tar.gz: 5675f42a9d038b1129c8f7c606ee464a7ee217b8
5
+ SHA512:
6
+ metadata.gz: 9eb1c20278a5f1772aaf60e6d39f98e2940f65fb79f9d31f134fc0f45cd896f6063254b5cd21f247a863c801b6960ae0b645ec4076914eca878e4eb2f83fdeb4
7
+ data.tar.gz: e926a3da9fdfa501630297b702815c560eb776c3075d37a911038786bc6522f086214865580edaccdb961d8defc40bdb0f2f89f04e0ad2ea93e069a5f4946b85
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,10 @@
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"
10
+ gem "phashion"
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,31 @@
1
+ # Refile::MiniMagick
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'refile-mini_magick'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install refile-mini_magick
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/refile-mini_magick/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
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,127 @@
1
+ require "refile"
2
+ require "refile/mini_magick/version"
3
+ require "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 [MiniMagick::Image] img the image to convert
17
+ # @param [String] format the format to convert to
18
+ # @return [void]
19
+ def convert(img, format)
20
+ img.format(format.to_s.downcase, nil)
21
+ end
22
+
23
+ # Resize the image to fit within the specified dimensions while retaining
24
+ # the original aspect ratio. Will only resize the image if it is larger
25
+ # than the specified dimensions. The resulting image may be shorter or
26
+ # narrower than specified in either dimension but will not be larger than
27
+ # the specified values.
28
+ #
29
+ # @param [MiniMagick::Image] img the image to convert
30
+ # @param [#to_s] width the maximum width
31
+ # @param [#to_s] height the maximum height
32
+ # @return [void]
33
+ def limit(img, width, height)
34
+ img.resize "#{width}x#{height}>"
35
+ end
36
+
37
+ # Resize the image to fit within the specified dimensions while retaining
38
+ # the original aspect ratio. The image may be shorter or narrower than
39
+ # specified in the smaller dimension but will not be larger than the
40
+ # specified values.
41
+ #
42
+ # @param [MiniMagick::Image] img the image to convert
43
+ # @param [#to_s] width the width to fit into
44
+ # @param [#to_s] height the height to fit into
45
+ # @return [void]
46
+ def fit(img, width, height)
47
+ img.resize "#{width}x#{height}"
48
+ end
49
+
50
+ # Resize the image so that it is at least as large in both dimensions as
51
+ # specified, then crops any excess outside the specified dimensions.
52
+ #
53
+ # The resulting image will always be exactly as large as the specified
54
+ # dimensions.
55
+ #
56
+ # By default, the center part of the image is kept, and the remainder
57
+ # cropped off, but this can be changed via the `gravity` option.
58
+ #
59
+ # @param [MiniMagick::Image] img the image to convert
60
+ # @param [#to_s] width the width to fill out
61
+ # @param [#to_s] height the height to fill out
62
+ # @param [String] gravity which part of the image to focus on
63
+ # @return [void]
64
+ # @see http://www.imagemagick.org/script/command-line-options.php#gravity
65
+ def fill(img, width, height, gravity = "Center")
66
+ img.combine_options do |cmd|
67
+ cmd.resize "#{width}x#{height}^"
68
+ cmd.gravity gravity
69
+ cmd.extent "#{width}x#{height}"
70
+ end
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
+ # @return [void]
91
+ # @see http://www.imagemagick.org/script/color.php
92
+ # @see http://www.imagemagick.org/script/command-line-options.php#gravity
93
+ def pad(img, width, height, background = "transparent", gravity = "Center")
94
+ img.combine_options do |cmd|
95
+ cmd.thumbnail "#{width}x#{height}>"
96
+ if background == "transparent"
97
+ cmd.background "rgba(255, 255, 255, 0.0)"
98
+ else
99
+ cmd.background background
100
+ end
101
+ cmd.gravity gravity
102
+ cmd.extent "#{width}x#{height}"
103
+ end
104
+ end
105
+
106
+ # Process the given file. The file will be processed via one of the
107
+ # instance methods of this class, depending on the `method` argument passed
108
+ # to the constructor on initialization.
109
+ #
110
+ # If the format is given it will convert the image to the given file format.
111
+ #
112
+ # @param [Tempfile] file the file to manipulate
113
+ # @param [String] format the file format to convert to
114
+ # @return [File] the processed file
115
+ def call(file, *args, format: nil)
116
+ img = ::MiniMagick::Image.new(file.path)
117
+ img.format(format.to_s.downcase, nil) if format
118
+ send(@method, img, *args)
119
+
120
+ ::File.open(img.path, "rb")
121
+ end
122
+ end
123
+ end
124
+
125
+ [:fill, :fit, :limit, :pad, :convert].each do |name|
126
+ Refile.processor(name, Refile::MiniMagick.new(name))
127
+ end
@@ -0,0 +1,5 @@
1
+ module Refile
2
+ class MiniMagick
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
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 = "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 "mini_magick", "~> 4.0"
22
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,125 @@
1
+ require "pry"
2
+ require "refile/mini_magick"
3
+ require "phashion"
4
+
5
+ describe Refile::MiniMagick do
6
+ let(:portrait) { Tempfile.new(["portrait", ".jpg"]) }
7
+ let(:landscape) { Tempfile.new(["landscape", ".jpg"]) }
8
+
9
+ matcher :be_similar_to do |expected|
10
+ match do |actual|
11
+ a = Phashion::Image.new(expected)
12
+ b = Phashion::Image.new(actual)
13
+ @distance = a.distance_from(b).abs
14
+ @distance < allowed_distance
15
+ end
16
+
17
+ failure_message do
18
+ "perceptual hash distance between images should be < #{allowed_distance} but was #{@distance}"
19
+ end
20
+
21
+ def allowed_distance
22
+ 2
23
+ end
24
+ end
25
+
26
+ def fixture_path(name)
27
+ File.expand_path("./fixtures/#{name}", File.dirname(__FILE__))
28
+ end
29
+
30
+ before do
31
+ FileUtils.cp(fixture_path("portrait.jpg"), portrait.path)
32
+ FileUtils.cp(fixture_path("landscape.jpg"), landscape.path)
33
+ end
34
+
35
+ describe "#convert" do
36
+ it "changes the image format" do
37
+ file = Refile::MiniMagick.new(:convert).call(portrait, "png")
38
+ expect(::MiniMagick::Image.new(file.path).identify).to match(/PNG/)
39
+ end
40
+ end
41
+
42
+ describe "#limit" do
43
+ it "resizes the image up to a given limit" do
44
+ file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
45
+ result = ::MiniMagick::Image.new(file.path)
46
+ expect(result.width).to eq(300)
47
+ expect(result.height).to eq(400)
48
+ end
49
+
50
+ it "does not resize the image if it is smaller than the limit" do
51
+ file = Refile::MiniMagick.new(:limit).call(portrait, "1000", "1000")
52
+ result = ::MiniMagick::Image.new(file.path)
53
+ expect(result.width).to eq(600)
54
+ expect(result.height).to eq(800)
55
+ end
56
+
57
+ it "produces correct image" do
58
+ file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
59
+ expect(file.path).to be_similar_to(fixture_path("limit.jpg"))
60
+ end
61
+ end
62
+
63
+ describe "#fit" do
64
+ it "resizes the image to fit given dimensions" do
65
+ file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
66
+ result = ::MiniMagick::Image.new(file.path)
67
+ expect(result.width).to eq(300)
68
+ expect(result.height).to eq(400)
69
+ end
70
+
71
+ it "enlarges image if it is smaller than given dimensions" do
72
+ file = Refile::MiniMagick.new(:fit).call(portrait, "1000", "1000")
73
+ result = ::MiniMagick::Image.new(file.path)
74
+ expect(result.width).to eq(750)
75
+ expect(result.height).to eq(1000)
76
+ end
77
+
78
+ it "produces correct image" do
79
+ file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
80
+ expect(file.path).to be_similar_to(fixture_path("fit.jpg"))
81
+ end
82
+ end
83
+
84
+ describe "#fill" do
85
+ it "resizes and crops the image to fill out the given dimensions" do
86
+ file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
87
+ result = ::MiniMagick::Image.new(file.path)
88
+ expect(result.width).to eq(400)
89
+ expect(result.height).to eq(400)
90
+ end
91
+
92
+ it "enlarges image and crops it if it is smaller than given dimensions" do
93
+ file = Refile::MiniMagick.new(:fill).call(portrait, "1000", "1000")
94
+ result = ::MiniMagick::Image.new(file.path)
95
+ expect(result.width).to eq(1000)
96
+ expect(result.height).to eq(1000)
97
+ end
98
+
99
+ it "produces correct image" do
100
+ file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
101
+ expect(file.path).to be_similar_to(fixture_path("fill.jpg"))
102
+ end
103
+ end
104
+
105
+ describe "#fill" do
106
+ it "resizes and fills out the remaining space to fill out the given dimensions" do
107
+ file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400", "red")
108
+ result = ::MiniMagick::Image.new(file.path)
109
+ expect(result.width).to eq(400)
110
+ expect(result.height).to eq(400)
111
+ end
112
+
113
+ it "enlarges image and fills out the remaining space to fill out the given dimensions" do
114
+ file = Refile::MiniMagick.new(:pad).call(portrait, "1000", "1000", "red")
115
+ result = ::MiniMagick::Image.new(file.path)
116
+ expect(result.width).to eq(1000)
117
+ expect(result.height).to eq(1000)
118
+ end
119
+
120
+ it "produces correct image" do
121
+ file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400")
122
+ expect(file.path).to be_similar_to(fixture_path("pad.jpg"))
123
+ end
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refile-mini_magick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Nicklas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-20 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: mini_magick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ description:
42
+ email:
43
+ - jonas.nicklas@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/refile/mini_magick.rb
54
+ - lib/refile/mini_magick/version.rb
55
+ - refile-mini_magick.gemspec
56
+ - spec/refile/fixtures/fill.jpg
57
+ - spec/refile/fixtures/fit.jpg
58
+ - spec/refile/fixtures/landscape.jpg
59
+ - spec/refile/fixtures/limit.jpg
60
+ - spec/refile/fixtures/pad.jpg
61
+ - spec/refile/fixtures/portrait.jpg
62
+ - spec/refile/mini_magick_spec.rb
63
+ homepage: ''
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Image processing via MiniMagick for Refile
87
+ test_files:
88
+ - spec/refile/fixtures/fill.jpg
89
+ - spec/refile/fixtures/fit.jpg
90
+ - spec/refile/fixtures/landscape.jpg
91
+ - spec/refile/fixtures/limit.jpg
92
+ - spec/refile/fixtures/pad.jpg
93
+ - spec/refile/fixtures/portrait.jpg
94
+ - spec/refile/mini_magick_spec.rb