aspect_ratio 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +89 -0
- data/aspect_ratio.gemspec +21 -0
- data/lib/aspect_ratio.rb +69 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05bd1cc5c2f1747b539c17d8f31bdc03a24d2623
|
4
|
+
data.tar.gz: 42bff77082ba5fa96724a520413675e3b3882b5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42849f7d573819756c8776b3e220824fb028ad619c32b0f8cd7d457f74c0a769e17cd8f8e5e529afaad626ba3a78d3994acb79c89e234fe994c19b66704fa252
|
7
|
+
data.tar.gz: 2df2833c8bcd8ef43d76ea74935ba0f735666a85a2ac426471bb474f8fd7babd8f166ae1699a9614deed8780aaf92622e8b391bc83c953ac4d883c72f6f5f3a7
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Trung Lê
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# aspect_ratio
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/envato/aspect_ratio.svg?branch=master)](https://travis-ci.org/envato/aspect_ratio)
|
4
|
+
|
5
|
+
Image aspect ratio utilities.
|
6
|
+
|
7
|
+
The Ruby port of [node-aspectratio](https://www.npmjs.com/package/aspectratio) npm module.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
Install globally
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install aspect_ratio
|
15
|
+
```
|
16
|
+
|
17
|
+
OR
|
18
|
+
|
19
|
+
Install locally with Bundler
|
20
|
+
|
21
|
+
Please include
|
22
|
+
|
23
|
+
```
|
24
|
+
gem 'aspect_ratio'
|
25
|
+
```
|
26
|
+
|
27
|
+
in your `Gemfile` then `bundle install`
|
28
|
+
|
29
|
+
## Test
|
30
|
+
|
31
|
+
```
|
32
|
+
ruby test/aspect_ratio_test.rb
|
33
|
+
```
|
34
|
+
|
35
|
+
## API
|
36
|
+
|
37
|
+
### crop(**integer** `width`, **integer** `height`, **string** `ratio`)
|
38
|
+
|
39
|
+
Apply a fixed aspect `ratio` crop without distoring the image aspect ratio.
|
40
|
+
|
41
|
+
* **integer** `width` - original image width
|
42
|
+
* **integer** `height` - original image height
|
43
|
+
* **string** `ratio` - new image ratio
|
44
|
+
|
45
|
+
> The `ratio` must be on the following format: `x`:`y` where `x` and `y` are
|
46
|
+
> integers. The order of `x` and `z` does not matter and `3:4` will be treated
|
47
|
+
> as `4:3`.
|
48
|
+
|
49
|
+
> By default #crop() will match the orientation of the original image unless a
|
50
|
+
> forced orientation is given on the follwing format: `x`:`y`!`z` where `z` is
|
51
|
+
> the orientation (`v` for vertical, or `h` for horizontal).
|
52
|
+
|
53
|
+
#### Return
|
54
|
+
|
55
|
+
This will return an `Array` of four values:
|
56
|
+
|
57
|
+
1. **integer** `x` - top lef x coordinate
|
58
|
+
2. **integer** `y` - top lef y coordinate
|
59
|
+
3. **integer** `width` - new image width
|
60
|
+
4. **integer** `height` - new image height
|
61
|
+
|
62
|
+
#### Example
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'aspect_ratio'
|
66
|
+
AspectRatio.crop(2048, 768, '4:3');
|
67
|
+
// [512, 768, 1024, 768]
|
68
|
+
```
|
69
|
+
|
70
|
+
![Crop with fixed ratio](./aspect.png)
|
71
|
+
|
72
|
+
### resize(**integer** `x`, **integer** `y`, **integer** `maxX`, **integer** `maxY`)
|
73
|
+
|
74
|
+
Get resized height and width of an image while perserving the aspect ratio of
|
75
|
+
the image.
|
76
|
+
|
77
|
+
* **integer** `x` - original image width
|
78
|
+
* **integer** `y` - original image height
|
79
|
+
* **integer** `maxX` - max image width
|
80
|
+
* **integer** `maxY` - max image height
|
81
|
+
|
82
|
+
### Return
|
83
|
+
|
84
|
+
Returns an `Array` of the resized `x` and `y` values:
|
85
|
+
|
86
|
+
* **integer** `x` - resized image width
|
87
|
+
* **integer** `y` - resized image height
|
88
|
+
|
89
|
+
## [MIT License](./LICENSE)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'aspect_ratio'
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.date = '2016-04-26'
|
8
|
+
s.summary = 'Image aspect ratio calculation utility'
|
9
|
+
s.description = 'Image aspect ratio calculation utility'
|
10
|
+
s.authors = ['Trung Lê']
|
11
|
+
s.email = 'trung.le@ruby-journal.com'
|
12
|
+
s.files = `git ls-files -z -- lib/* LICENSE README.md aspect_ratio.gemspec`.split("\x0")
|
13
|
+
s.homepage = 'http://github.com/envato/aspect_ratio'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
s.required_ruby_version = '>= 2.2.0'
|
18
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
19
|
+
|
20
|
+
s.add_development_dependency 'minitest', '~> 5'
|
21
|
+
end
|
data/lib/aspect_ratio.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module AspectRatio
|
4
|
+
def self.resize(x, y, x_max = nil, y_max = nil)
|
5
|
+
x = BigDecimal(x)
|
6
|
+
y = BigDecimal(y)
|
7
|
+
|
8
|
+
if x_max && y_max
|
9
|
+
# Maximum values of height and width given, aspect ratio preserved.
|
10
|
+
if y > x
|
11
|
+
return [(y_max * x / y).round, y_max]
|
12
|
+
else
|
13
|
+
return [x_max, (x_max * y / x).round]
|
14
|
+
end
|
15
|
+
elsif x_max
|
16
|
+
# Width given, height automagically selected to preserve aspect ratio.
|
17
|
+
return [x_max, (x_max * y / x).round]
|
18
|
+
else
|
19
|
+
# Height given, width automagically selected to preserve aspect ratio.
|
20
|
+
return [(y_max * x / y).round, y_max]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.crop(x, y, r)
|
25
|
+
orient = r.split('!')[1]
|
26
|
+
ratio = r.split('!')[0].split(':').sort.map { |r| BigDecimal(r) }
|
27
|
+
|
28
|
+
vertical = y > x
|
29
|
+
rotate = y > x && orient == 'h' || x > y && orient == 'v'
|
30
|
+
|
31
|
+
if (vertical || rotate) && !(vertical && rotate)
|
32
|
+
x = x + y
|
33
|
+
y = x - y
|
34
|
+
x = x - y
|
35
|
+
end
|
36
|
+
|
37
|
+
xʹ = x
|
38
|
+
yʹ = x * (ratio[1] / ratio[0])
|
39
|
+
|
40
|
+
if (yʹ > y) || rotate && (yʹ > x)
|
41
|
+
yʹ = y
|
42
|
+
xʹ = y * (ratio[1] / ratio[0])
|
43
|
+
|
44
|
+
if xʹ > x
|
45
|
+
xʹ = x
|
46
|
+
yʹ = x * (ratio[0] / ratio[1])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
Δx = ((x - xʹ) / 2).to_f.floor
|
51
|
+
Δy = ((y - yʹ) / 2).to_f.floor
|
52
|
+
|
53
|
+
if (vertical || rotate) && !(vertical && rotate)
|
54
|
+
[
|
55
|
+
Δy, # crop top left x
|
56
|
+
Δx, # crop top left y
|
57
|
+
y - Δy * 2, # crop width
|
58
|
+
x - Δx * 2 # crop height
|
59
|
+
]
|
60
|
+
else
|
61
|
+
[
|
62
|
+
Δx.to_f, # crop top left x
|
63
|
+
Δy, # crop top left y
|
64
|
+
x - Δx * 2, # crop width
|
65
|
+
y - Δy * 2 # crop height
|
66
|
+
]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aspect_ratio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trung Lê
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-26 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'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
description: Image aspect ratio calculation utility
|
28
|
+
email: trung.le@ruby-journal.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- aspect_ratio.gemspec
|
36
|
+
- lib/aspect_ratio.rb
|
37
|
+
homepage: http://github.com/envato/aspect_ratio
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.2.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.4.5.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Image aspect ratio calculation utility
|
61
|
+
test_files: []
|
62
|
+
has_rdoc:
|