vips-thumbnail 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d4bfaf97d75cf13a513d6280f4afb9ed438e4cb
4
+ data.tar.gz: bb8f1f40de09916e95fe04dbcca29244bb81544d
5
+ SHA512:
6
+ metadata.gz: 4d973f0b0221b82de7f2c43b9259f81a2b6f65dde6ebe9cf20d25503a60eb00cd665b91aa1da701215b05fa861c42f0cb5d3b7c2d96fbca61bc60b244caebbbc
7
+ data.tar.gz: 238c3206f041ec8fd878002b96f0bd63b0cfc4d6ab390bf53485d9e857fbc53b7a4ebc87ece9bfca410209553d96f2fdaf713b82b62268cbe8af0a9dc0476229
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --format documentation
3
+ --backtrace
4
+ --warnings
5
+ --require spec_helper
@@ -0,0 +1,51 @@
1
+ language: ruby
2
+ dist: trusty
3
+ sudo: true
4
+ rvm:
5
+ - 2.1.8
6
+ - 2.2.4
7
+ - 2.3.0
8
+ - 2.4.0
9
+ - ruby-head
10
+ - rbx-2
11
+ env:
12
+ - COVERAGE=true LIBVIPS=8.4.4
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: "rbx-2"
16
+ - rvm: "ruby-head"
17
+
18
+ # Copied from https://github.com/jcupitt/ruby-vips/blob/master/.travis.yml
19
+ before_install:
20
+ - wget http://www.vips.ecs.soton.ac.uk/supported/current/vips-$LIBVIPS.tar.gz
21
+ - tar xvf vips-$LIBVIPS.tar.gz
22
+ - cd vips-$LIBVIPS
23
+ - >
24
+ CXXFLAGS=-D_GLIBCXX_USE_CXX11_ABI=0
25
+ ./configure --prefix=/usr
26
+ - make
27
+ - sudo make install
28
+ - sudo ldconfig
29
+
30
+ addons:
31
+ apt:
32
+ packages:
33
+ - libxml2-dev
34
+ - gettext
35
+ - python-dev
36
+ - liblcms1-dev
37
+ - libmagickwand-dev
38
+ - libopenexr-dev
39
+ - libcfitsio3-dev
40
+ - gobject-introspection
41
+ - libgirepository1.0-dev
42
+ - libgif-dev
43
+ - libgs-dev
44
+ - libgsf-1-dev
45
+ - libmatio-dev
46
+ - libopenslide-dev
47
+ - liborc-0.4-dev
48
+ - libpango1.0-dev
49
+ - libpoppler-glib-dev
50
+ - libwebp-dev
51
+ - libglib2.0-dev
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vips-thumbnail.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development do
8
+ end
9
+
10
+ group :test do
11
+ gem 'simplecov'
12
+ gem 'coveralls', require: false
13
+ end
@@ -0,0 +1,71 @@
1
+ # Vips::Thumbnail
2
+
3
+ An easy to use thumbnail resizer. It's based on libvips so it's fast.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/ioquatix/vips-thumbnail.svg)](http://travis-ci.org/ioquatix/vips-thumbnail)
6
+ [![Code Climate](https://codeclimate.com/github/ioquatix/vips-thumbnail.svg)](https://codeclimate.com/github/ioquatix/vips-thumbnail)
7
+ [![Coverage Status](https://coveralls.io/repos/ioquatix/vips-thumbnail/badge.svg)](https://coveralls.io/r/ioquatix/vips-thumbnail)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'vips-thumbnail'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install vips-thumbnail
24
+
25
+ ## Usage
26
+
27
+ It's super easy:
28
+
29
+ ```ruby
30
+ resizer = Vips::Thumbnail::Resizer.new(input_path)
31
+ if image = resizer.resize_to_fit([800, 600])
32
+ image.write_to_file(output_path)
33
+ else
34
+ # The source image wasn't big enough:
35
+ symlink(input_path, output_path)
36
+ end
37
+ ```
38
+
39
+ There are two main methods, `#resize_to_fit` which preserves aspect ratio, and `#resize_to_fill` which resizes and crops to fit the desired size if needed.
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
48
+
49
+ ## License
50
+
51
+ Released under the MIT license.
52
+
53
+ Copyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining a copy
56
+ of this software and associated documentation files (the "Software"), to deal
57
+ in the Software without restriction, including without limitation the rights
58
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
59
+ copies of the Software, and to permit persons to whom the Software is
60
+ furnished to do so, subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in
63
+ all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71
+ THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative "thumbnail/version"
22
+ require_relative "thumbnail/resizer"
@@ -0,0 +1,117 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'vips'
22
+
23
+ module Vips
24
+ module Thumbnail
25
+ class Resizer
26
+ def initialize(input_path)
27
+ @input_path = input_path
28
+ @input_image = nil
29
+ end
30
+
31
+ attr :input_path
32
+
33
+ def input_image
34
+ unless @input_image
35
+ image = Vips::Image.new_from_file(@input_path)
36
+ @input_image = image.autorot
37
+ end
38
+
39
+ return @input_image
40
+ end
41
+
42
+ # Resize the image to completely fill the desired size, if possible.
43
+ # @return [Vips::Image] if the image could be resized, otherwise `nil`.
44
+ def resize_to_fill(output_size)
45
+ width, height = *output_size
46
+
47
+ # We can only fill and crop if the input image is BIGGER in at least one dimension than the desired output size:
48
+ if input_image.width > width or input_image.height > height
49
+ return fill_and_crop(input_image, width, height)
50
+ end
51
+ end
52
+
53
+ # Resize the image to fit within the given bounds, preserving aspect ratio.
54
+ # @return [Vips::Image] if the image could be resized, otherwise `nil`.
55
+ def resize_to_fit(output_size)
56
+ width, height = *output_size
57
+
58
+ # We can only fit if the input image is BIGGER in at least one dimension than the desired output size:
59
+ if input_image.width > width or input_image.height > height
60
+ return fit(input_image, width, height)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def fit(image, width, height)
67
+ x_scale = Rational(width, image.width)
68
+ y_scale = Rational(height, image.height)
69
+
70
+ scale = [x_scale, y_scale].min
71
+
72
+ if scale < 1.0
73
+ return image.resize(scale)
74
+ end
75
+ end
76
+
77
+ def fill_and_crop(image, width, height)
78
+ x_scale = Rational(width, image.width)
79
+ y_scale = Rational(height, image.height)
80
+
81
+ scale = [x_scale, y_scale].max
82
+ # puts "scale #{scale}"
83
+
84
+ if scale < 1.0
85
+ image = image.resize(scale)
86
+ elsif scale > 1.0
87
+ # Just crop it.. no scale.
88
+ width = image.width if x_scale > 1.0
89
+ height = image.height if y_scale > 1.0
90
+ end
91
+
92
+ return crop(image, width, height)
93
+ end
94
+
95
+ def crop(image, width, height)
96
+ if image.height < height
97
+ throw ArgumentError.new("Scaled image was smaller than expected! Scaled height #{image.height} < #{height}.")
98
+ end
99
+
100
+ if image.width < width
101
+ throw ArgumentError.new("Scaled image was smaller than expected! Scaled width #{image.width} < #{width}.")
102
+ end
103
+
104
+ left = (image.width - width) / 2.0
105
+ top = (image.height - height) / 2.0
106
+
107
+ # puts "image.width #{image.width} image.height: #{image.height}"
108
+ # puts "Left: #{left} Top: #{top} X: #{width} Y: #{height}"
109
+ image = image.extract_area(left, top, width, height)
110
+
111
+ # puts "image.width #{image.width} image.height: #{image.height}"
112
+
113
+ return image
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Vips
22
+ module Thumbnail
23
+ VERSION = "1.0.0"
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ require_relative 'lib/vips/thumbnail/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "vips-thumbnail"
6
+ spec.version = Vips::Thumbnail::VERSION
7
+ spec.authors = ["Samuel Williams"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
+
10
+ spec.summary = %q{Convenient thumbnail resizing using libvips.}
11
+ spec.homepage = "https://github.com/ioquatix/vips-thumbnail"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "ruby-vips", "~> 1.0"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.14"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vips-thumbnail
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-vips
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.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.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - samuel.williams@oriontransfer.co.nz
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - lib/vips/thumbnail.rb
83
+ - lib/vips/thumbnail/resizer.rb
84
+ - lib/vips/thumbnail/version.rb
85
+ - vips-thumbnail.gemspec
86
+ homepage: https://github.com/ioquatix/vips-thumbnail
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.10
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Convenient thumbnail resizing using libvips.
109
+ test_files: []