image_processing 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of image_processing might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/image_processing.gemspec +22 -0
- data/lib/image_processing/mini_magick.rb +179 -0
- data/lib/image_processing/version.rb +3 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d685d2233ad9b0da42966e273cc5a453215eb82a
|
4
|
+
data.tar.gz: 18ce0a061ba6eeff0501c311aa9f217404c25371
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8859c7bcf11803b4e41b8eb03f04fc30ddf65ffd4756c4c4d76907189fb7a7c5fb9bb4f1b6287b34ce21cd07b7c0edd566fb9ec14b30d128bdea910c6f31c802
|
7
|
+
data.tar.gz: 649891929efd850e926f6f30487dfca66c44e12f614dff1b47fa0e4191e368880f331ee88634cebe381aad90fa73824eca8afaf0c432f1a1adc53d651235abd6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Janko Marohnić
|
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,87 @@
|
|
1
|
+
# ImageProcessing
|
2
|
+
|
3
|
+
Provides higher-level helper methods for image processing in Ruby using
|
4
|
+
ImageMagick. This methods are extracted from CarrierWave and Refile, and
|
5
|
+
can be reused in any project.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'image_processing'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Typically you will include the module in your class:
|
16
|
+
|
17
|
+
```rb
|
18
|
+
require "image_processing/mini_magick"
|
19
|
+
|
20
|
+
include ImageProcessing::MiniMagick
|
21
|
+
|
22
|
+
original = File.open("path/to/image.jpg")
|
23
|
+
|
24
|
+
converted = convert(original, "png") # makes a converted copy
|
25
|
+
converted #=> #<Tempfile:/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/mini_magick20151003-23030-9e1vjz.png (closed)>
|
26
|
+
File.exist?(original.path) #=> true
|
27
|
+
|
28
|
+
converted = convert!(original, "png") # destructively converts the file
|
29
|
+
converted #=> #<Tempfile:/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/mini_magick20151003-23030-9e1vjz.png (closed)>
|
30
|
+
File.exist?(original.path) #=> false
|
31
|
+
```
|
32
|
+
|
33
|
+
If you would rather not pollute your namespace, you can also call the methods
|
34
|
+
directly on the module:
|
35
|
+
|
36
|
+
```rb
|
37
|
+
image = File.open("path/to/image.jpg")
|
38
|
+
|
39
|
+
ImageProcessing::MiniMagick.resize_to_fit(image, 400, 400)
|
40
|
+
```
|
41
|
+
|
42
|
+
The following is the list of helper methods that ImageProcessing provides:
|
43
|
+
|
44
|
+
```rb
|
45
|
+
# Converts file to the specified format.
|
46
|
+
convert(file, format) # nondestructive
|
47
|
+
convert!(file, format) # destructive
|
48
|
+
|
49
|
+
# Resizes image to fit the specified dimensions (shrinks if larger, enlarges if
|
50
|
+
# smaller, but keeps the aspect ratio).
|
51
|
+
resize_to_fit(file, width, height) # nondestructive
|
52
|
+
resize_to_fit!(file, width, height) # destructive
|
53
|
+
|
54
|
+
# Resizes image in limit of the specified dimensions (shrinks if larger, keeps
|
55
|
+
# if smaller, but keeps the aspect ratio).
|
56
|
+
resize_to_limit(file, width, height) # nondestructive
|
57
|
+
resize_to_limit!(file, width, height) # destructive
|
58
|
+
|
59
|
+
# Resizes image to fill the specified dimensions (shrinks if larger,
|
60
|
+
# enlarges if smaller, crops the longer side).
|
61
|
+
resize_to_fill(file, width, height, gravity: "Center") # nondestructive
|
62
|
+
resize_to_fill!(file, width, height, gravity: "Center") # destructive
|
63
|
+
|
64
|
+
# Resizes image to the specified dimensions and pads missing space (shrinks if
|
65
|
+
# larger, enlarges if smaller, fills the shorter side with specified color).
|
66
|
+
resize_and_pad(file, width, height, background: "transparent", gravity: "Center") # nondestructive
|
67
|
+
resize_and_pad!(file, width, height, background: "transparent", gravity: "Center") # destructive
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
ImageMagick and GraphicsMagick are both required to be installed, on Mac this is
|
73
|
+
|
74
|
+
```
|
75
|
+
$ brew install imagemagick
|
76
|
+
$ brew install graphicsmagick
|
77
|
+
```
|
78
|
+
|
79
|
+
Run tests with
|
80
|
+
|
81
|
+
```
|
82
|
+
$ rake test
|
83
|
+
```
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
[MIT](LICENSE.txt)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative "lib/image_processing/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "image_processing"
|
5
|
+
spec.version = ImageProcessing::VERSION
|
6
|
+
spec.authors = ["Janko Marohnić"]
|
7
|
+
spec.email = ["janko.marohnic@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Set of higher-level helper methods for image processing."
|
10
|
+
spec.description = "Set of higher-level helper methods for image processing."
|
11
|
+
spec.homepage = "https://github.com/janko-m/image_processing"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = Dir["README.md", "LICENSE.txt", "lib/**/*", "image_processing.gemspec"]
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.add_development_dependency "minitest", "~> 5.8"
|
18
|
+
spec.add_development_dependency "minitest-hooks"
|
19
|
+
spec.add_development_dependency "minispec-metadata"
|
20
|
+
spec.add_development_dependency "mini_magick", ">= 4.3.5"
|
21
|
+
spec.add_development_dependency "phashion"
|
22
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require "image_processing/version"
|
2
|
+
require "mini_magick"
|
3
|
+
require "tempfile"
|
4
|
+
|
5
|
+
if MiniMagick.version < Gem::Version.new("4.3.5")
|
6
|
+
raise "image_processing requires mini_magick version >= 4.3.5"
|
7
|
+
end
|
8
|
+
|
9
|
+
module ImageProcessing
|
10
|
+
module MiniMagick
|
11
|
+
def self.nondestructive_alias(name, original)
|
12
|
+
define_method(name) do |image, *args, &block|
|
13
|
+
send(original, _copy_to_tempfile(image), *args, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module_function
|
18
|
+
|
19
|
+
# Changes the image encoding format to the given format
|
20
|
+
#
|
21
|
+
# @see http://www.imagemagick.org/script/command-line-options.php#format
|
22
|
+
# @param [MiniMagick::Image] image the image to convert
|
23
|
+
# @param [String] format the format to convert to
|
24
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
25
|
+
# @return [File, Tempfile]
|
26
|
+
def convert!(image, format, &block)
|
27
|
+
_with_minimagick(image) do |img|
|
28
|
+
img.format(format.downcase, nil, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
nondestructive_alias :convert, :convert!
|
32
|
+
|
33
|
+
# Resize the image to fit within the specified dimensions while retaining
|
34
|
+
# the original aspect ratio. Will only resize the image if it is larger
|
35
|
+
# than the specified dimensions. The resulting image may be shorter or
|
36
|
+
# narrower than specified in either dimension but will not be larger than
|
37
|
+
# the specified values.
|
38
|
+
#
|
39
|
+
# @param [MiniMagick::Image] image the image to convert
|
40
|
+
# @param [#to_s] width the maximum width
|
41
|
+
# @param [#to_s] height the maximum height
|
42
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
43
|
+
# @return [File, Tempfile]
|
44
|
+
def resize_to_limit!(image, width, height)
|
45
|
+
_with_minimagick(image) do |img|
|
46
|
+
img.combine_options do |cmd|
|
47
|
+
yield cmd if block_given?
|
48
|
+
cmd.resize "#{width}x#{height}>"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
nondestructive_alias :resize_to_limit, :resize_to_limit!
|
53
|
+
|
54
|
+
# Resize the image to fit within the specified dimensions while retaining
|
55
|
+
# the original aspect ratio. The image may be shorter or narrower than
|
56
|
+
# specified in the smaller dimension but will not be larger than the
|
57
|
+
# specified values.
|
58
|
+
#
|
59
|
+
# @param [MiniMagick::Image] image the image to convert
|
60
|
+
# @param [#to_s] width the width to fit into
|
61
|
+
# @param [#to_s] height the height to fit into
|
62
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
63
|
+
# @return [File, Tempfile]
|
64
|
+
def resize_to_fit!(image, width, height)
|
65
|
+
_with_minimagick(image) do |img|
|
66
|
+
img.combine_options do |cmd|
|
67
|
+
yield cmd if block_given?
|
68
|
+
cmd.resize "#{width}x#{height}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
nondestructive_alias :resize_to_fit, :resize_to_fit!
|
73
|
+
|
74
|
+
# Resize the image so that it is at least as large in both dimensions as
|
75
|
+
# specified, then crops any excess outside the specified dimensions.
|
76
|
+
#
|
77
|
+
# The resulting image will always be exactly as large as the specified
|
78
|
+
# dimensions.
|
79
|
+
#
|
80
|
+
# By default, the center part of the image is kept, and the remainder
|
81
|
+
# cropped off, but this can be changed via the `gravity` option.
|
82
|
+
#
|
83
|
+
# @param [MiniMagick::Image] image the image to convert
|
84
|
+
# @param [#to_s] width the width to fill out
|
85
|
+
# @param [#to_s] height the height to fill out
|
86
|
+
# @param [String] gravity which part of the image to focus on
|
87
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
88
|
+
# @return [File, Tempfile]
|
89
|
+
# @see http://www.imagemagick.org/script/command-line-options.php#gravity
|
90
|
+
def resize_to_fill!(image, width, height, gravity: "Center")
|
91
|
+
_with_minimagick(image) do |img|
|
92
|
+
img.combine_options do |cmd|
|
93
|
+
yield cmd if block_given?
|
94
|
+
cmd.resize "#{width}x#{height}^"
|
95
|
+
cmd.gravity gravity
|
96
|
+
cmd.extent "#{width}x#{height}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
nondestructive_alias :resize_to_fill, :resize_to_fill!
|
101
|
+
|
102
|
+
# Resize the image to fit within the specified dimensions while retaining
|
103
|
+
# the original aspect ratio in the same way as {#fill}. Unlike {#fill} it
|
104
|
+
# will, if necessary, pad the remaining area with the given color, which
|
105
|
+
# defaults to transparent where supported by the image format and white
|
106
|
+
# otherwise.
|
107
|
+
#
|
108
|
+
# The resulting image will always be exactly as large as the specified
|
109
|
+
# dimensions.
|
110
|
+
#
|
111
|
+
# By default, the image will be placed in the center but this can be
|
112
|
+
# changed via the `gravity` option.
|
113
|
+
#
|
114
|
+
# @param [MiniMagick::image] image the image to convert
|
115
|
+
# @param [#to_s] width the width to fill out
|
116
|
+
# @param [#to_s] height the height to fill out
|
117
|
+
# @param [string] background the color to use as a background
|
118
|
+
# @param [string] gravity which part of the image to focus on
|
119
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
120
|
+
# @return [File, Tempfile]
|
121
|
+
# @see http://www.imagemagick.org/script/color.php
|
122
|
+
# @see http://www.imagemagick.org/script/command-line-options.php#gravity
|
123
|
+
def resize_and_pad!(image, width, height, background: "transparent", gravity: "Center")
|
124
|
+
_with_minimagick(image) do |img|
|
125
|
+
img.combine_options do |cmd|
|
126
|
+
yield cmd if block_given?
|
127
|
+
cmd.resize "#{width}x#{height}"
|
128
|
+
if background == "transparent"
|
129
|
+
cmd.background "rgba(255, 255, 255, 0.0)"
|
130
|
+
else
|
131
|
+
cmd.background background
|
132
|
+
end
|
133
|
+
cmd.gravity gravity
|
134
|
+
cmd.extent "#{width}x#{height}"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
nondestructive_alias :resize_and_pad, :resize_and_pad!
|
139
|
+
|
140
|
+
# Resample the image to fit within the specified resolution while retaining
|
141
|
+
# the original image size.
|
142
|
+
#
|
143
|
+
# The resulting image will always be the same pixel size as the source with
|
144
|
+
# an adjusted resolution dimensions.
|
145
|
+
#
|
146
|
+
# @param [MiniMagick::Image] img the image to convert
|
147
|
+
# @param [#to_s] width the dpi width
|
148
|
+
# @param [#to_s] height the dpi height
|
149
|
+
# @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
|
150
|
+
# @return [File, Tempfile]
|
151
|
+
# @see http://www.imagemagick.org/script/command-line-options.php#resample
|
152
|
+
def resample!(image, width, height)
|
153
|
+
_with_minimagick(image) do |img|
|
154
|
+
img.combine_options do |cmd|
|
155
|
+
yield cmd if block_given?
|
156
|
+
cmd.resample "#{width}x#{height}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
nondestructive_alias :resample, :resample!
|
161
|
+
|
162
|
+
# Convert an image into a MiniMagick::Image for the duration of the block,
|
163
|
+
# and at the end return a File object.
|
164
|
+
def _with_minimagick(image)
|
165
|
+
image = ::MiniMagick::Image.new(image.path, image)
|
166
|
+
yield image
|
167
|
+
image.instance_variable_get("@tempfile")
|
168
|
+
end
|
169
|
+
|
170
|
+
# Creates a copy of the file and stores it into a Tempfile. Works for any
|
171
|
+
# IO object that responds to `#read(length = nil, outbuf = nil)`.
|
172
|
+
def _copy_to_tempfile(file)
|
173
|
+
args = [File.basename(file.path, ".*"), File.extname(file.path)] if file.respond_to?(:path)
|
174
|
+
tempfile = Tempfile.new(args || "image", binmode: true)
|
175
|
+
IO.copy_stream(file, tempfile.path)
|
176
|
+
tempfile
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_processing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janko Marohnić
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-hooks
|
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: minispec-metadata
|
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: 4.3.5
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.3.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: phashion
|
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: Set of higher-level helper methods for image processing.
|
84
|
+
email:
|
85
|
+
- janko.marohnic@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.md
|
92
|
+
- image_processing.gemspec
|
93
|
+
- lib/image_processing/mini_magick.rb
|
94
|
+
- lib/image_processing/version.rb
|
95
|
+
homepage: https://github.com/janko-m/image_processing
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.5
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Set of higher-level helper methods for image processing.
|
119
|
+
test_files: []
|
120
|
+
has_rdoc:
|