paperclip_watermark 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e54792a352790ac6beacfc2a71bc28fd0432a9ff
4
+ data.tar.gz: 7ad6f8fc9ae5dfadb9bd4f20c78a67a5305904cf
5
+ SHA512:
6
+ metadata.gz: cbde95b6ed8673e80aa8222a58271648827d36961c4a79fb4754b347cf57ec6c3e75b87e5188d286a1769f149300a6645a3347101c6dfa92c06f6d4e2b7ae0ce
7
+ data.tar.gz: a6d7557b8c54b18aa3a69b47295bdde2e2893fde3b20ba41230fb76109c3580aa654726a9269180f97c69255448296f043612f3e797df38bcccb22eed4ac22c2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip_watermark.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alessandro Caianiello
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
@@ -0,0 +1,57 @@
1
+ # PaperclipWatermark
2
+
3
+ ## Description
4
+ This is a simple Paperclip processor to apply watermarks on Paperclip's images. The watermark will be resized to fit the base image.
5
+
6
+ Few options are available to specify the position and it opacity:
7
+
8
+ `watermark_distance_from_top`: specify the position from top in percentage
9
+
10
+ `watermark_position`: specify the position (NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast)
11
+
12
+ `watermark_dissolve`: specify the opacity
13
+
14
+ ## Usage
15
+
16
+ ```ruby
17
+ # Paperclip image attachments
18
+ has_attached_file :attachment, :processors => [:thumbnail, :watermark],
19
+ styles: {
20
+ thumb: '250x250>',
21
+ original: {
22
+ geometry: '1280x1280>',
23
+ watermark_dissolve: 30,
24
+ watermark_distance_from_top: 90,
25
+ watermark_path: "#{Rails.root}/public/images/logo.png"
26
+ }
27
+ }
28
+
29
+ ```
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ ```ruby
36
+ gem 'paperclip_watermark'
37
+ ```
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install paperclip_watermark
46
+
47
+ ## Usage
48
+
49
+ TODO: Write usage instructions here
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it ( https://github.com/[my-github-username]/paperclip_watermark/fork )
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,88 @@
1
+ require "paperclip/processor"
2
+
3
+ module Paperclip
4
+ class Watermark < Paperclip::Processor
5
+
6
+ def initialize(file, options = {}, attachment = nil)
7
+ super
8
+ @file = file
9
+ @whiny = options.fetch(:whiny, true)
10
+ @watermark_path = options[:watermark_path]
11
+ @dissolve = options.fetch(:watermark_dissolve, 30)
12
+ @position = options[:watermark_position]
13
+ @distance_from_top = options[:watermark_distance_from_top]
14
+
15
+ raise Paperclip::Error.new('Postion or distance_from_top required') if @position.nil? && @distance_from_top.nil?
16
+ raise Paperclip::Error.new('Missing watermark path') if @watermark_path.nil?
17
+ end
18
+
19
+ def make
20
+ destination = Tempfile.new([file_basename, current_format ? ".#{current_format}" : ''])
21
+ destination.binmode
22
+
23
+ Paperclip.run('composite', composition_options(destination.path))
24
+ destination
25
+ rescue Paperclip::Errors::CommandNotFoundError
26
+ raise Paperclip::Errors::CommandNotFoundError, "There was an error processing the watermark for #{@file.path}" if @whiny
27
+ end
28
+
29
+ def composition_options(destination_path)
30
+ options = []
31
+ options << "-dissolve #{@dissolve}%"
32
+ options << '-quality 100'
33
+ options << watermak_position
34
+ options << watermark_command
35
+ options << @file.path
36
+ options << destination_path
37
+ options.join(' ')
38
+ end
39
+
40
+ def watermak_position
41
+ if @position
42
+ "-gravity #{@position}"
43
+ else
44
+ "-geometry +#{calculated_watermak_position.first}+#{calculated_watermak_position.last}"
45
+ end
46
+ end
47
+
48
+ def watermark_command
49
+ options = []
50
+ options << '\\('
51
+ options << @watermark_path
52
+ options << '-resize'
53
+ options << "#{scaled_size_for_watermark.width}x#{scaled_size_for_watermark.height}"
54
+ options << '\\)'
55
+ options.join(' ')
56
+ end
57
+
58
+ def scaled_size_for_watermark
59
+ destination_width = file_size.width - 20
60
+ calculated_height = watermark_size.height.to_f / watermark_size.width.to_f * destination_width.to_f
61
+ Paperclip::Geometry.new(destination_width, calculated_height.to_i)
62
+ end
63
+
64
+ private
65
+
66
+ def watermark_size
67
+ @watermark_size ||= Paperclip::Geometry.from_file(@watermark_path)
68
+ end
69
+
70
+ def file_size
71
+ @file_size ||= Paperclip::Geometry.from_file(@file.path)
72
+ end
73
+
74
+ def file_basename
75
+ File.basename(@file.path, current_format)
76
+ end
77
+
78
+ def current_format
79
+ File.extname(@file.path)
80
+ end
81
+
82
+ def calculated_watermak_position
83
+ top = ((file_size.height - scaled_size_for_watermark.height)/100) * @distance_from_top
84
+ left = (file_size.width - scaled_size_for_watermark.width)/2
85
+ [left, top]
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module PaperclipWatermark
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "paperclip_watermark/version"
2
+
3
+ module PaperclipWatermark
4
+ end
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'paperclip_watermark/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "paperclip_watermark"
7
+ spec.version = PaperclipWatermark::VERSION
8
+ spec.authors = ["Alessandro Caianiello"]
9
+ spec.email = ["github@caianiello.it"]
10
+ spec.summary = %q{Paperclip Watermark with resize support}
11
+ spec.description = %q{}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "paperclip", "~> 4.2"
23
+ spec.add_development_dependency("rspec")
24
+ end
@@ -0,0 +1,92 @@
1
+ require 'active_support/core_ext/object/blank'
2
+ require 'tempfile'
3
+ require 'paperclip'
4
+ require 'paperclip/watermark'
5
+ require 'paperclip/errors'
6
+ require 'paperclip/geometry'
7
+ require 'paperclip/geometry_detector_factory'
8
+ require 'paperclip/geometry_parser_factory'
9
+ require 'cocaine'
10
+
11
+ describe PaperclipWatermark::Watermark do
12
+ let(:file) { File.new(File.expand_path('../../support/dummy.jpg', __FILE__)) }
13
+ let(:watermark_path) { File.new(File.expand_path('../../support/logo.png', __FILE__)) }
14
+ let(:position) { 'Center' }
15
+ let(:watermark_distance_from_top) { nil }
16
+ let(:options) do
17
+ {
18
+ watermark_path: watermark_path,
19
+ watermark_position: position,
20
+ watermark_distance_from_top: watermark_distance_from_top
21
+ }
22
+ end
23
+
24
+ subject{ described_class.new(file, options) }
25
+
26
+ describe "#make" do
27
+ let(:composition_options) { 'composition_options' }
28
+
29
+ before do
30
+ allow(subject).to receive(:composition_options).and_return(composition_options)
31
+ end
32
+
33
+ it "applies a scaled watermark" do
34
+ expect(Paperclip).to receive(:run).with('composite', composition_options)
35
+ subject.make
36
+ end
37
+ end
38
+
39
+ describe "#composition_options" do
40
+ let(:destination) { 'destination' }
41
+ let(:watermark_command) { 'watermark_command' }
42
+
43
+ before do
44
+ allow(subject).to receive(:watermark_command).and_return(watermark_command)
45
+ end
46
+
47
+ it "returns the right command" do
48
+ command = subject.composition_options(destination)
49
+ expected_command = "-dissolve 30% -quality 100 -gravity Center #{watermark_command} #{file.path} destination"
50
+ expect(command).to eq(expected_command)
51
+ end
52
+ end
53
+
54
+ describe "#watermark_command" do
55
+ let(:watermark_path) { 'watermark_path' }
56
+ before do
57
+ allow(subject).to receive(:scaled_size_for_watermark).and_return(Paperclip::Geometry.new(100,100))
58
+ end
59
+
60
+ it "returns the right command" do
61
+ expected_command = '\\( watermark_path -resize 100.0x100.0 \\)'
62
+ expect(subject.watermark_command).to eq(expected_command)
63
+ end
64
+ end
65
+
66
+ describe "#watermak_position" do
67
+ let(:position) { 'Center' }
68
+
69
+ context "with a position" do
70
+ it "returns the gravity" do
71
+ expect(subject.watermak_position).to eq('-gravity Center')
72
+ end
73
+ end
74
+
75
+ context "without a position" do
76
+ let(:position) { nil }
77
+ let(:watermark_distance_from_top) { 80 }
78
+
79
+ it "returns the geometry" do
80
+ expect(subject.watermak_position).to eq('-geometry +10.0+208.0')
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#scaled_size_for_watermark" do
86
+ it "returns the scaled size for the watermark" do
87
+ result = subject.scaled_size_for_watermark
88
+ expect(result.width).to eq(280)
89
+ expect(result.height).to eq(40.0)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,92 @@
1
+ require 'paperclip'
2
+ require 'paperclip-watermark'
3
+ require File.expand_path('../../config/environment', __FILE__)
4
+
5
+ # This file was generated by the `rails generate rspec:install` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
8
+ # file to always be loaded, without a need to explicitly require it in any files.
9
+ #
10
+ # Given that it is always loaded, you are encouraged to keep this file as
11
+ # light-weight as possible. Requiring heavyweight dependencies from this file
12
+ # will add to the boot time of your test suite on EVERY test run, even for an
13
+ # individual file that may not need all of that loaded. Instead, consider making
14
+ # a separate helper file that requires the additional dependencies and performs
15
+ # the additional setup, and require it from the spec files that actually need it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+
22
+ ActiveRecord::Migration.maintain_test_schema!
23
+
24
+ RSpec.configure do |config|
25
+ # rspec-expectations config goes here. You can use an alternate
26
+ # assertion/expectation library such as wrong or the stdlib/minitest
27
+ # assertions if you prefer.
28
+ config.expect_with :rspec do |expectations|
29
+ # This option will default to `true` in RSpec 4. It makes the `description`
30
+ # and `failure_message` of custom matchers include text for helper methods
31
+ # defined using `chain`, e.g.:
32
+ # be_bigger_than(2).and_smaller_than(4).description
33
+ # # => "be bigger than 2 and smaller than 4"
34
+ # ...rather than:
35
+ # # => "be bigger than 2"
36
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
37
+ end
38
+
39
+ # rspec-mocks config goes here. You can use an alternate test double
40
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
41
+ config.mock_with :rspec do |mocks|
42
+ # Prevents you from mocking or stubbing a method that does not exist on
43
+ # a real object. This is generally recommended, and will default to
44
+ # `true` in RSpec 4.
45
+ mocks.verify_partial_doubles = true
46
+ end
47
+
48
+ # The settings below are suggested to provide a good initial experience
49
+ # with RSpec, but feel free to customize to your heart's content.
50
+ =begin
51
+ # These two settings work together to allow you to limit a spec run
52
+ # to individual examples or groups you care about by tagging them with
53
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
54
+ # get run.
55
+ config.filter_run :focus
56
+ config.run_all_when_everything_filtered = true
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
59
+ # For more details, see:
60
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
+ config.disable_monkey_patching!
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip_watermark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Caianiello
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-22 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: paperclip
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ''
70
+ email:
71
+ - github@caianiello.it
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/paperclip/watermark.rb
82
+ - lib/paperclip_watermark.rb
83
+ - lib/paperclip_watermark/version.rb
84
+ - paperclip_watermark.gemspec
85
+ - spec/paperclip/watermark_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/dummy.jpg
88
+ - spec/support/logo.png
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Paperclip Watermark with resize support
113
+ test_files:
114
+ - spec/paperclip/watermark_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/support/dummy.jpg
117
+ - spec/support/logo.png