the_image 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +3 -0
- data/Rakefile +2 -0
- data/gem_version.rb +3 -0
- data/lib/the_image.rb +147 -0
- data/lib/the_image/version.rb +1 -0
- data/spec/crop_tool/.DS_Store +0 -0
- data/spec/crop_tool/crop.rb +63 -0
- data/spec/crop_tool/dest_images/test_1.png +0 -0
- data/spec/crop_tool/dest_images/test_2.png +0 -0
- data/spec/crop_tool/dest_images/test_3.png +0 -0
- data/spec/crop_tool/dest_images/test_4.png +0 -0
- data/spec/crop_tool/dest_images/test_5.png +0 -0
- data/spec/crop_tool/src_images/100x100.png +0 -0
- data/spec/crop_tool/src_images/1200x100.png +0 -0
- data/spec/crop_tool/src_images/1504x846.jpg +0 -0
- data/spec/crop_tool/src_images/169x150.jpg +0 -0
- data/spec/crop_tool/src_images/600x1000.jpg +0 -0
- data/the_image.gemspec +25 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11489c85bdd57b5e438f024ccd8d9de61656e452
|
4
|
+
data.tar.gz: e64893b0a1176272fab816e956bcdd8737af6c01
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1c9e0e43b4991970133f4744848327f73b3e867c1a246eade3d8e82f4352efacfbfee7c05b758b88e4fa0784196f26e6f6c78f5724a1926f51f7840a231434f
|
7
|
+
data.tar.gz: 54321deb1897311636a9ffb3374d1f806aef34d08890e7d32ddcb7b733cf2477fcb8fdba2bd21ff64f0fd6298960b565eb2ff54739af5c9c801edaf5a1a94b48
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ilya N. Zykin
|
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
data/Rakefile
ADDED
data/gem_version.rb
ADDED
data/lib/the_image.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require_relative 'the_image/version'
|
2
|
+
require 'mini_magick'
|
3
|
+
|
4
|
+
module TheImage
|
5
|
+
# Helpers
|
6
|
+
def path_to_file file_path
|
7
|
+
file_path.split('/')[0...-1].join('/')
|
8
|
+
end
|
9
|
+
|
10
|
+
def destroy_file file_path
|
11
|
+
FileUtils.rm(file_path , force: true)
|
12
|
+
end
|
13
|
+
|
14
|
+
def destroy_dir dir_path
|
15
|
+
FileUtils.rm_rf dir_path
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_path_for_file file_path
|
19
|
+
FileUtils.mkdir_p path_to_file(file_path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy_dir_of_file file_path
|
23
|
+
FileUtils.rm_rf path_to_file(file_path)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Main methods
|
27
|
+
def landscape? image
|
28
|
+
image[:width] > image[:height]
|
29
|
+
end
|
30
|
+
|
31
|
+
def portrait? image
|
32
|
+
image[:width] < image[:height]
|
33
|
+
end
|
34
|
+
|
35
|
+
def manipulate opts = {}, &block
|
36
|
+
src = opts[:src]
|
37
|
+
dest = opts[:dest]
|
38
|
+
format = opts[:format]
|
39
|
+
|
40
|
+
image = ::MiniMagick::Image.open src
|
41
|
+
image.format(format.to_s.downcase) if format
|
42
|
+
|
43
|
+
image = instance_exec(image, opts, &block)
|
44
|
+
image.write dest
|
45
|
+
end
|
46
|
+
|
47
|
+
# Image manipulate
|
48
|
+
# Resizing can be wrong when .EXT of file is wrong
|
49
|
+
def strict_resize image, w, h
|
50
|
+
image.resize "#{ w }x#{ h }!"
|
51
|
+
image
|
52
|
+
end
|
53
|
+
|
54
|
+
def resize image, w, h
|
55
|
+
image.resize "#{ w }x#{ h }"
|
56
|
+
image
|
57
|
+
end
|
58
|
+
|
59
|
+
def rotate image, angle
|
60
|
+
image.rotate angle
|
61
|
+
image
|
62
|
+
end
|
63
|
+
|
64
|
+
def rotate_left image
|
65
|
+
rotate image, '-90'
|
66
|
+
end
|
67
|
+
|
68
|
+
def rotate_right image
|
69
|
+
rotate image, '90'
|
70
|
+
end
|
71
|
+
|
72
|
+
def biggest_side_not_bigger_than image, size
|
73
|
+
if landscape?(image)
|
74
|
+
image.resize("#{ size }x") if image[:width] > size.to_i
|
75
|
+
else
|
76
|
+
image.resize("x#{ size }") if image[:height] > size.to_i
|
77
|
+
end
|
78
|
+
|
79
|
+
image
|
80
|
+
end
|
81
|
+
|
82
|
+
def strip image
|
83
|
+
image.strip
|
84
|
+
image
|
85
|
+
end
|
86
|
+
|
87
|
+
def auto_orient image
|
88
|
+
image.auto_orient
|
89
|
+
image
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_rect image, width, height, opts = {}
|
93
|
+
default_opts = { valign: :center, align: :center }
|
94
|
+
opts = default_opts.merge(opts)
|
95
|
+
|
96
|
+
align = opts[:align].to_sym
|
97
|
+
valign = opts[:valign].to_sym
|
98
|
+
|
99
|
+
w0, h0 = image[:width].to_f, image[:height].to_f
|
100
|
+
w1, h1 = width.to_f, height.to_f
|
101
|
+
fw, fh = w0, h0
|
102
|
+
|
103
|
+
scale = ((w1 / w0) > (h1 / h0)) ? (w1 / w0) : (h1 / h0)
|
104
|
+
|
105
|
+
fw = (w1 / scale).to_i
|
106
|
+
fh = (h1 / scale).to_i
|
107
|
+
|
108
|
+
x0 = case align
|
109
|
+
when :center
|
110
|
+
((w0 - fw) / 2).to_i
|
111
|
+
when :right
|
112
|
+
(w0 - fw).to_i
|
113
|
+
else
|
114
|
+
0
|
115
|
+
end
|
116
|
+
|
117
|
+
y0 = case valign
|
118
|
+
when :center
|
119
|
+
((h0 - fh) / 2).to_i
|
120
|
+
when :bottom
|
121
|
+
(h0 - fh).to_i
|
122
|
+
else
|
123
|
+
0
|
124
|
+
end
|
125
|
+
|
126
|
+
image.crop "#{ fw }x#{ fh }+#{ x0 }+#{ y0 }"
|
127
|
+
image.resize "#{ width }x#{ height }!"
|
128
|
+
image
|
129
|
+
end
|
130
|
+
|
131
|
+
def to_square image, size, opts = {}
|
132
|
+
to_rect image, size, size, opts
|
133
|
+
end
|
134
|
+
|
135
|
+
# scale = original_iamge[:width].to_f / image_on_screen[:width].to_f
|
136
|
+
# usually scale should be 1
|
137
|
+
def crop image, x0 = 0, y0 = 0, w = 100, h = 100, scale = 1
|
138
|
+
x0 = (x0.to_f * scale).to_i
|
139
|
+
y0 = (y0.to_f * scale).to_i
|
140
|
+
|
141
|
+
w = (w.to_f * scale).to_i
|
142
|
+
h = (h.to_f * scale).to_i
|
143
|
+
|
144
|
+
image.crop "#{ w }x#{ h }+#{ x0 }+#{ y0 }"
|
145
|
+
image
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../../gem_version'
|
Binary file
|
@@ -0,0 +1,63 @@
|
|
1
|
+
_root_ = File.expand_path('../../../', __FILE__)
|
2
|
+
|
3
|
+
require "#{ _root_ }/lib/the_image"
|
4
|
+
|
5
|
+
# Processor
|
6
|
+
class Tool; include TheImage; end
|
7
|
+
tool = Tool.new
|
8
|
+
|
9
|
+
# base paths
|
10
|
+
src_dir = "#{ _root_ }/spec/crop_tool/src_images"
|
11
|
+
dest_dir = "#{ _root_ }/spec/crop_tool/dest_images"
|
12
|
+
|
13
|
+
# reset dest dir
|
14
|
+
tool.destroy_dir dest_dir
|
15
|
+
tool.create_path_for_file "#{ dest_dir }/filename"
|
16
|
+
|
17
|
+
# test_1
|
18
|
+
src = "#{ src_dir }/1200x100.png"
|
19
|
+
dest = "#{ dest_dir }/test_1.png"
|
20
|
+
|
21
|
+
tool.manipulate({ src: src, dest: dest }) do |image, opts|
|
22
|
+
to_rect image, 100, 100
|
23
|
+
end
|
24
|
+
|
25
|
+
# test_2
|
26
|
+
src = "#{ src_dir }/100x100.png"
|
27
|
+
dest = "#{ dest_dir }/test_2.png"
|
28
|
+
|
29
|
+
tool.manipulate({ src: src, dest: dest }) do |image, opts|
|
30
|
+
to_rect image, 100, 100
|
31
|
+
end
|
32
|
+
|
33
|
+
# test_3
|
34
|
+
src = "#{ src_dir }/1504x846.jpg"
|
35
|
+
dest = "#{ dest_dir }/test_3.png"
|
36
|
+
|
37
|
+
tool.manipulate({ src: src, dest: dest }) do |image, opts|
|
38
|
+
to_rect image, 300, 100
|
39
|
+
end
|
40
|
+
|
41
|
+
# test_4
|
42
|
+
src = "#{ src_dir }/600x1000.jpg"
|
43
|
+
dest = "#{ dest_dir }/test_4.png"
|
44
|
+
|
45
|
+
tool.manipulate({ src: src, dest: dest }) do |image, opts|
|
46
|
+
to_rect image, 100, 100, { valign: :top }
|
47
|
+
end
|
48
|
+
|
49
|
+
# test_5
|
50
|
+
src = "#{ src_dir }/169x150.jpg"
|
51
|
+
dest = "#{ dest_dir }/test_5.png"
|
52
|
+
|
53
|
+
img_w = 500
|
54
|
+
x0 = 115
|
55
|
+
y0 = 52
|
56
|
+
w = 348.42857142857144
|
57
|
+
h = 271
|
58
|
+
|
59
|
+
tool.manipulate({ src: src, dest: dest }) do |image, opts|
|
60
|
+
scale = image[:width].to_f / img_w.to_f
|
61
|
+
crop image, x0, y0, w, h, scale
|
62
|
+
to_rect image, 270, 210
|
63
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/the_image.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'the_image/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "the_image"
|
8
|
+
spec.version = TheImage::VERSION
|
9
|
+
spec.authors = ["Ilya N. Zykin"]
|
10
|
+
spec.email = ["zykin-ilya@ya.ru"]
|
11
|
+
spec.summary = %q{TheImage - helpers for image manipulations}
|
12
|
+
spec.description = %q{Helpers for image manipulations}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'mini_magick'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: the_image
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ilya N. Zykin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mini_magick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
description: Helpers for image manipulations
|
56
|
+
email:
|
57
|
+
- zykin-ilya@ya.ru
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- gem_version.rb
|
68
|
+
- lib/the_image.rb
|
69
|
+
- lib/the_image/version.rb
|
70
|
+
- spec/crop_tool/.DS_Store
|
71
|
+
- spec/crop_tool/crop.rb
|
72
|
+
- spec/crop_tool/dest_images/test_1.png
|
73
|
+
- spec/crop_tool/dest_images/test_2.png
|
74
|
+
- spec/crop_tool/dest_images/test_3.png
|
75
|
+
- spec/crop_tool/dest_images/test_4.png
|
76
|
+
- spec/crop_tool/dest_images/test_5.png
|
77
|
+
- spec/crop_tool/src_images/100x100.png
|
78
|
+
- spec/crop_tool/src_images/1200x100.png
|
79
|
+
- spec/crop_tool/src_images/1504x846.jpg
|
80
|
+
- spec/crop_tool/src_images/169x150.jpg
|
81
|
+
- spec/crop_tool/src_images/600x1000.jpg
|
82
|
+
- the_image.gemspec
|
83
|
+
homepage: ''
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.2.2
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: TheImage - helpers for image manipulations
|
107
|
+
test_files:
|
108
|
+
- spec/crop_tool/.DS_Store
|
109
|
+
- spec/crop_tool/crop.rb
|
110
|
+
- spec/crop_tool/dest_images/test_1.png
|
111
|
+
- spec/crop_tool/dest_images/test_2.png
|
112
|
+
- spec/crop_tool/dest_images/test_3.png
|
113
|
+
- spec/crop_tool/dest_images/test_4.png
|
114
|
+
- spec/crop_tool/dest_images/test_5.png
|
115
|
+
- spec/crop_tool/src_images/100x100.png
|
116
|
+
- spec/crop_tool/src_images/1200x100.png
|
117
|
+
- spec/crop_tool/src_images/1504x846.jpg
|
118
|
+
- spec/crop_tool/src_images/169x150.jpg
|
119
|
+
- spec/crop_tool/src_images/600x1000.jpg
|