img_overlay 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e52861bbf7dde7b7255a0dd54644b10f9c6e455
4
+ data.tar.gz: 7a49a8193399491524c551bc80401dea944c7588
5
+ SHA512:
6
+ metadata.gz: 53de81a4d242adecc74ada64d86d1a52a05f22ff7ff932592fa81629ef0c6e8fb112a80cdfa8a15babd2225d1e59679a28ba7b3f887097dd9608a5acfecc129c
7
+ data.tar.gz: 6f439f2e743d5e427ab4b32264bf6c83f842b079591ca3a46f3e6964c80cb4c52dc88966e0648964b965a6ba44efe564334b75de6405d43fc0679be117e4557f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.0
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at khoj.badami@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in img_overlay.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Khoj Badami
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,75 @@
1
+ # ImgOverlay
2
+
3
+ This is a simple gem that gives you your image back overlay-ed with some color.
4
+
5
+ ## What is the point of this gem?
6
+
7
+ Well, I originally created it because I wanted a fast way to turn image URLs into hero images to use on web-pages. Hero images, have some text on them usually. So that they text is readable, they need to be overlay-ed.
8
+
9
+ ![](https://static.wixstatic.com/media/09e926b98b2149c7ba461062b8a64f47.jpg)
10
+
11
+ See how the title "The smartest way to manage your data" is on some image that is overlay-ed with a purple color that goes with the sites branding? Thats what this gem does.
12
+
13
+ ## Dependencies
14
+
15
+ The gem depends on ImageMagic to be installed on your computer. It just shells out and runs ImageMagic commands.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'img_overlay'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install img_overlay
32
+
33
+ ## Usage
34
+
35
+ Add the gem to your gem file or install it via the command line as shown in the installation section.
36
+
37
+ To use this, you just have to specify all the info in the form of a hash. There are 3 hashes that are needed:
38
+
39
+ 1. **Input Hash**: This can have the URL of the image / file path of the image
40
+ 2. **Color Hash**: The RGB values of the color for the overlay & the opacity of the overlay layer
41
+ 3. **Output Hash**: A location where the final output image needs to be stored
42
+
43
+ ### Example
44
+
45
+ ```ruby
46
+ require 'img_overlay'
47
+
48
+ include ImgOverlay
49
+
50
+ success = make_image_with_overlay(
51
+ input: {
52
+ url: "http://craftypioneer.com/wp-content/uploads/2016/01/businessMeeting.jpg"
53
+ },
54
+ color: {
55
+ r: 0,
56
+ g: 0,
57
+ b: 0,
58
+ opacity: 0.6
59
+ },
60
+ output: {
61
+ path: '/Users/khojbadami/test.jpg'
62
+ }
63
+ )
64
+ ```
65
+
66
+ That's it. It all works, you will get a true or a false.
67
+
68
+ ## Testing
69
+
70
+ This gem needs more tests and development around validating user input and giving helpful error messages. Just now, it will simply fail. It needs better feedback for users when the inputs are not right. It also needs better error messages when the inputs are funny.
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "img_overlay"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'img_overlay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "img_overlay"
8
+ spec.version = ImgOverlay::VERSION
9
+ spec.authors = ["Khoj Badami"]
10
+ spec.email = ["khoj.badami@gmail.com"]
11
+
12
+ spec.summary = "Simple gem to add an overlay on your image. Input: Image & Color. Output: Image with overlay!"
13
+ spec.description = "Given an image path/URL and a color in RGB, you get back an image with an overlay on it (with the given color)."
14
+ spec.homepage = "https://github.com/scotch-bright/img_overlay"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,22 @@
1
+ module ImgOverlay
2
+
3
+ class FinalImage
4
+
5
+ def initialize(orignal_image_path:, overlay_image_path:, final_image_path:)
6
+ @orignal_image_path = orignal_image_path
7
+ @overlay_image_path = overlay_image_path
8
+ @final_image_path = final_image_path
9
+ end
10
+
11
+ def command
12
+ "composite -gravity Center #{@overlay_image_path} #{@orignal_image_path} #{@final_image_path}"
13
+ end
14
+
15
+ def generate!
16
+ #puts "about to run command:: #{command}"
17
+ `#{command}`
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,46 @@
1
+ require 'tempfile'
2
+
3
+ module ImgOverlay
4
+
5
+ class Helper
6
+
7
+ class << self
8
+
9
+ def get_input_image input_hash
10
+ if is_image_url_given?(input_hash)
11
+ get_image_path_from_url(input_hash[:url])
12
+ else
13
+ input_hash[:path]
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def get_image_path_from_url(url)
20
+ otuput = get_temp_file_path
21
+ id = ImgOverlay::ImageDownloader.new(
22
+ url: url,
23
+ path: otuput
24
+ )
25
+ if id.download!.nil?
26
+ return nil
27
+ else
28
+ return otuput
29
+ end
30
+ end
31
+
32
+ def get_temp_file_path
33
+ temp_file = Tempfile.new('image')
34
+ temp_file.path
35
+ end
36
+
37
+ def is_image_url_given? input_hash
38
+ return false if input_hash[:url].nil?
39
+ return true
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,56 @@
1
+ require "open-uri"
2
+
3
+ module ImgOverlay
4
+
5
+ class ImageDownloader
6
+
7
+ def initialize(url:, path:)
8
+ @image_url = url
9
+ @full_file_path = path
10
+ end
11
+
12
+ def download!
13
+ open_uri_file_object = get_file_object
14
+ if open_uri_file_object.nil?
15
+ return false
16
+ else
17
+ if file_acceptable?(open_uri_file_object)
18
+ save_file open_uri_file_object
19
+ return true
20
+ else
21
+ return false
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def save_file open_uri_file_object
29
+ File.open(@full_file_path, 'wb') do |fo|
30
+ fo.write open_uri_file_object.read
31
+ end
32
+ end
33
+
34
+ def get_file_object
35
+ begin
36
+ return open(@image_url)
37
+ rescue
38
+ return nil
39
+ end
40
+ end
41
+
42
+ def file_acceptable?(open_uri_file_object)
43
+ if open_uri_file_object.status[0] == "200" && accepted_mime_types.include?(open_uri_file_object.content_type)
44
+ return true
45
+ else
46
+ return false
47
+ end
48
+ end
49
+
50
+ def accepted_mime_types
51
+ ['image/gif', 'image/jpeg', 'image/png']
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,62 @@
1
+ require 'open3'
2
+
3
+ module ImgOverlay
4
+
5
+ class ImageSize
6
+
7
+ def initialize(file_path)
8
+ @file_path = file_path
9
+ end
10
+
11
+ def width
12
+ @width ||= get_image_size_hash[:width]
13
+ end
14
+
15
+ def height
16
+ @height ||= get_image_size_hash[:height]
17
+ end
18
+
19
+ private
20
+
21
+ def get_image_size_hash
22
+ @result ||= execute_command
23
+ if @result[:success]
24
+ convert_command_line_result_into_hash
25
+ else
26
+ invalid_image_path_hash
27
+ end
28
+ end
29
+
30
+ def convert_command_line_result_into_hash
31
+ output = @result[:output]
32
+ width_str = output.split("x")[0]
33
+ height_str = output.split("x")[1]
34
+ {
35
+ width: width_str.to_i,
36
+ height: height_str.to_i
37
+ }
38
+ end
39
+
40
+ def invalid_image_path_hash
41
+ {
42
+ width: nil,
43
+ height: nil
44
+ }
45
+ end
46
+
47
+ def execute_command
48
+ stdout, stderr, status = Open3.capture3(commmand)
49
+ return {
50
+ success: status.success?,
51
+ output: stdout,
52
+ error: stderr
53
+ }
54
+ end
55
+
56
+ def commmand
57
+ "convert #{@file_path} -format \"%wx%h\" info:"
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,26 @@
1
+ module ImgOverlay
2
+
3
+ class OverylayImage
4
+
5
+ def initialize(width:, height:, color_r:, color_g:, color_b:, opacity:, output_path:)
6
+ @width = width
7
+ @height = height
8
+ @color_r = color_r
9
+ @color_g = color_g
10
+ @color_b = color_b
11
+ @opacity = opacity
12
+ @output_path = output_path
13
+ end
14
+
15
+ def command
16
+ "convert -size #{@width}x#{@height} canvas:rgba\\(#{@color_r},#{@color_g},#{@color_b},#{@opacity}\\) #{@output_path}"
17
+ end
18
+
19
+ def generate!
20
+ #puts "about to run command:: #{command}"
21
+ `#{command}`
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module ImgOverlay
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,50 @@
1
+ require "img_overlay/version"
2
+ require "img_overlay/image_size"
3
+ require "img_overlay/image_downloader"
4
+ require "img_overlay/overylay_image"
5
+ require "img_overlay/final_image"
6
+ require "img_overlay/helper"
7
+
8
+ module ImgOverlay
9
+
10
+ def make_image_with_overlay(input:, color:, output:)
11
+
12
+ image_path = ImgOverlay::Helper.get_input_image(input)
13
+
14
+ if image_path.nil?
15
+
16
+ return nil
17
+
18
+ else
19
+
20
+ # Get image size
21
+ image_size = ImgOverlay::ImageSize.new(image_path)
22
+
23
+ temp_overlay_image_path = File.join(Dir.pwd, 'overlay.png')
24
+
25
+ # Generate overlay Image
26
+ ImgOverlay::OverylayImage.new(
27
+ width: image_size.width,
28
+ height: image_size.height,
29
+ color_r: color[:r],
30
+ color_g: color[:g],
31
+ color_b: color[:b],
32
+ opacity: color[:opacity],
33
+ output_path: temp_overlay_image_path
34
+ ).generate!
35
+
36
+ # Make Final Image
37
+ ImgOverlay::FinalImage.new(
38
+ orignal_image_path: image_path,
39
+ overlay_image_path: temp_overlay_image_path,
40
+ final_image_path: output[:path]
41
+ ).generate!
42
+
43
+ FileUtils.rm(temp_overlay_image_path)
44
+
45
+ return true
46
+
47
+ end
48
+ end
49
+
50
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: img_overlay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Khoj Badami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Given an image path/URL and a color in RGB, you get back an image with
56
+ an overlay on it (with the given color).
57
+ email:
58
+ - khoj.badami@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - img_overlay.gemspec
74
+ - lib/img_overlay.rb
75
+ - lib/img_overlay/final_image.rb
76
+ - lib/img_overlay/helper.rb
77
+ - lib/img_overlay/image_downloader.rb
78
+ - lib/img_overlay/image_size.rb
79
+ - lib/img_overlay/overylay_image.rb
80
+ - lib/img_overlay/version.rb
81
+ homepage: https://github.com/scotch-bright/img_overlay
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: 'Simple gem to add an overlay on your image. Input: Image & Color. Output:
105
+ Image with overlay!'
106
+ test_files: []