image_clipper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in image_clipper.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 wrymax
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.
@@ -0,0 +1,78 @@
1
+ # ImageClipper
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Firstly install the ImageMagick components:
8
+
9
+ brew install imagemagick
10
+
11
+ Or go to offcial website and download it:
12
+
13
+ http://www.imagemagick.org/script/binary-releases.php
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'image_clipper'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install image_clipper
26
+
27
+ ## Directly processing image:
28
+
29
+ Read image:
30
+
31
+ image = ImageClipper::Image.new(image_path)
32
+
33
+ Resize image:
34
+
35
+ # overwrite the original image:
36
+ image.resize('200x100')
37
+
38
+ # save to new file
39
+ image.resize('200x100', save_new_file_path)
40
+
41
+ # use percentage
42
+ image.resize('35%')
43
+
44
+ Add watermark:
45
+
46
+ # this will add watermark to bottom right corner as default, and will overwrite the original image
47
+ image.watermarking(watermark_image_path)
48
+
49
+ # config the watermark position:
50
+ # posible selections: %W(top_left top_right bottom_left bottom_right center)
51
+ image.watermarking(watermark_image_path, position: 'center', save_to: your_watermarked_path)
52
+
53
+ # you can also customize the size of watermark picture:
54
+ # using /\d+x\d+/
55
+ image.watermarking(watermark_image_path, save_to: your_watermarked_path, resize_to: '177x20')
56
+
57
+ # using percentage, and set position
58
+ image.watermarking(watermark_image_path, save_to: your_watermarked_path, resize_to: '60%', position: '100,230')
59
+
60
+ ## Work with paperclip and Rails
61
+
62
+ Suppose a model user.rb has :avatar attachment based on paperclip, and I want to add watermark for the existing avatar.
63
+
64
+ Add some code to user.rb: (just for example)
65
+
66
+ def watermark(watermark_path, options = {})
67
+ ImageClipper::Image.new(avatar.path(:original)).watermarking(watermark_path, options)
68
+ update_attribute(:avatar, avatar)
69
+ end
70
+
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'image_clipper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "image_clipper"
8
+ spec.version = ImageClipper::VERSION
9
+ spec.authors = ["max_wong"]
10
+ spec.email = ["wryma@qq.com"]
11
+ spec.description = %q{An easy ruby encapsulation for image processing. All functions are based on ImageMagick.}
12
+ spec.summary = %q{An easy ruby encapsulation for image processing.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "awesome_print"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-debugger"
27
+ end
@@ -0,0 +1,5 @@
1
+ require "image_clipper/version"
2
+
3
+ module ImageClipper
4
+ require 'image_clipper/image'
5
+ end
@@ -0,0 +1,20 @@
1
+ require 'image_clipper'
2
+
3
+ module ImageClipper
4
+ class Geometry
5
+ attr_accessor :width, :height
6
+
7
+ def initialize(attrs)
8
+ case attrs
9
+ when String
10
+ raise ArgumentError unless attrs.match(/^\w+x\w+$/)
11
+ geo = attrs.split('x')
12
+ @width = geo[0].to_i
13
+ @height = geo[1].to_i
14
+ when Hash
15
+ @width = attrs[:width].to_i
16
+ @height = attrs[:height].to_i
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+
3
+ # basic image class to represent an image
4
+
5
+ require 'image_clipper'
6
+ require 'image_clipper/geometry'
7
+
8
+ module ImageClipper
9
+ class Image
10
+ attr_accessor :file, :path, :file_size, :type, :geometry
11
+
12
+ def initialize(file_path)
13
+ @file = File.open(file_path)
14
+ @path = file_path
15
+ attributes = `identify #{file_path}`.split(' ')
16
+ @type = attributes[1]
17
+ @geometry = Geometry.new(attributes[2])
18
+ @file_size = attributes[6]
19
+ end
20
+
21
+ def resize(new_size, new_file_path = self.path)
22
+ geo = case new_size
23
+ when /\d+x\d+/
24
+ Geometry.new(new_size)
25
+ when /\d+%/
26
+ percent = new_size.to_f / 100
27
+ Geometry.new("#{(geometry.width * percent).round}x#{(geometry.height * percent).round}")
28
+ end
29
+
30
+ `convert #{path} -resize '#{geo.width}x#{geo.height}' #{new_file_path}`
31
+
32
+ Image.new(new_file_path)
33
+ end
34
+
35
+ def watermarking(watermark_path, options = {})
36
+ # options:
37
+ # position: the string position of watermark
38
+ # save_to: the path to save watermarked image
39
+ # resize_to: the target size of watermark image
40
+
41
+ # read the watermark
42
+ watermark = Image.new watermark_path
43
+
44
+ # config options
45
+ options = { position: 'bottom_right' }.merge(options)
46
+
47
+ # process resizing watermark
48
+ if options[:resize_to]
49
+ watermark_geo = case options[:resize_to]
50
+ when /\d+%/
51
+ watermark.geometry.width = (watermark.geometry.width * options[:resize_to].to_i / 100).round
52
+ watermark.geometry.height = (watermark.geometry.height * options[:resize_to].to_i / 100).round
53
+ when /\d+x\d+/
54
+ resize_tmp = options[:resize_to].split('x')
55
+ watermark.geometry.width = resize_tmp[0].to_i
56
+ watermark.geometry.height = resize_tmp[1].to_i
57
+ else
58
+ raise ArgumentError, "resize_to argument format error, try '100x100' or '50%'"
59
+ end
60
+ end
61
+ watermark_geo = "#{watermark.geometry.width},#{watermark.geometry.height}"
62
+
63
+ # process watermark position
64
+ pos_coord = case options[:position]
65
+ when 'top_left'
66
+ '0,0'
67
+ when 'top_right'
68
+ "#{geometry.width - watermark.geometry.width},0"
69
+ when 'bottom_left'
70
+ "0,#{geometry.height - watermark.geometry.height}"
71
+ when 'bottom_right'
72
+ "#{geometry.width - watermark.geometry.width},#{geometry.height - watermark.geometry.height}"
73
+ when 'center'
74
+ "#{(geometry.width - watermark.geometry.width) / 2},#{(geometry.height - watermark.geometry.height) / 2}"
75
+ when /\d+,\d+/
76
+ options[:position]
77
+ else
78
+ raise ArgumentError, "position argument missing"
79
+ end
80
+
81
+ # config the watermarked image path
82
+ watermarked_path = options[:save_to] || path
83
+
84
+ # process!
85
+ # ap "convert #{path} -draw \"image SrcOver #{pos_coord} #{watermark_geo} #{watermark_path}\" #{watermarked_path}"
86
+ `convert #{path} -draw "image SrcOver #{pos_coord} #{watermark_geo} #{watermark_path}" #{watermarked_path}`
87
+
88
+ # return the new Image object
89
+ Image.new(watermarked_path)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module ImageClipper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require 'image_clipper/geometry'
3
+
4
+ describe ImageClipper::Geometry do
5
+ it "new geometry with multiple ways" do
6
+ geo1 = ImageClipper::Geometry.new('640x480')
7
+ geo2 = ImageClipper::Geometry.new(width: 640, height: 480)
8
+
9
+ geo1.width.should == 640
10
+ geo1.height.should == 480
11
+
12
+ geo2.width.should == 640
13
+ geo2.height.should == 480
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'image_clipper/image'
3
+
4
+ describe ImageClipper::Image do
5
+ let :image_1 do
6
+ `cp test/tmp/original/angelababy.jpg test/tmp/`
7
+ ImageClipper::Image.new('test/tmp/angelababy.jpg')
8
+ end
9
+
10
+ it "new image and read attributes" do
11
+ image_1.path.should eql('test/tmp/angelababy.jpg')
12
+ image_1.type.should == 'JPEG'
13
+ image_1.file_size.should == '274KB'
14
+ image_1.geometry.width.should == 584
15
+ image_1.geometry.height.should == 900
16
+ end
17
+
18
+ describe 'resize image' do
19
+ it 'resize image to a new file' do
20
+ new_image = image_1.resize('292x450', 'test/tmp/new_angelababy.jpg')
21
+ new_image.path.should == 'test/tmp/new_angelababy.jpg'
22
+ new_image.geometry.width.should == 292
23
+ new_image.geometry.height.should == 450
24
+ end
25
+
26
+ it 'resize image to the original file' do
27
+ new_image = image_1.resize('292x450')
28
+ new_image.path.should == image_1.path
29
+ new_image.geometry.width.should == 292
30
+ new_image.geometry.height.should == 450
31
+ end
32
+
33
+ it 'resize image with percentage' do
34
+ percent = '20%'
35
+ percentage = percent.to_f / 100
36
+ new_image = image_1.resize(percent, "test/tmp/resize_angelababy_#{percent}.jpg")
37
+ new_image.path.should == "test/tmp/resize_angelababy_#{percent}.jpg"
38
+ new_image.geometry.width.should == (image_1.geometry.width * percentage).round
39
+ new_image.geometry.height.should == (image_1.geometry.height * percentage).round
40
+ end
41
+ end
42
+
43
+ describe 'watermarking' do
44
+ it 'add watermark at right bottom corner as default' do
45
+ image_1.watermarking('test/tmp/original/logo.png')
46
+ end
47
+
48
+ it "add watermark at custom position, saving at new image path" do
49
+ %W(top_left top_right bottom_left bottom_right center).each do |pos|
50
+ image_1.watermarking('test/tmp/original/logo.png', position: pos, save_to: "test/tmp/watermarked_#{pos}.jpg")
51
+ end
52
+ end
53
+
54
+
55
+ it "add watermark with special size" do
56
+ image_1.watermarking('test/tmp/original/logo.png', save_to: "test/tmp/watermarked_resize1.jpg", resize_to: '177x20')
57
+ image_1.watermarking('test/tmp/original/logo.png', save_to: "test/tmp/watermarked_resize2.jpg", resize_to: '60%', position: '100,230')
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'awesome_print'
4
+ require 'pry'
5
+ require 'image_clipper'
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_clipper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - max_wong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: awesome_print
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pry-debugger
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: An easy ruby encapsulation for image processing. All functions are based
111
+ on ImageMagick.
112
+ email:
113
+ - wryma@qq.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - image_clipper.gemspec
124
+ - lib/image_clipper.rb
125
+ - lib/image_clipper/geometry.rb
126
+ - lib/image_clipper/image.rb
127
+ - lib/image_clipper/version.rb
128
+ - spec/geometry_spec.rb
129
+ - spec/image_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.28
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: An easy ruby encapsulation for image processing.
156
+ test_files:
157
+ - spec/geometry_spec.rb
158
+ - spec/image_spec.rb
159
+ - spec/spec_helper.rb