rails-carrierwave-focuspoint 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDQ0Yjk5ZDAyYzc4NGI5YzE1ZjQ3ZGRhMTE4NTRkZDdmNzFjODQ4Mw==
5
+ data.tar.gz: !binary |-
6
+ YmExYWY2MDViOTBmNWVmNGI4ZGMxYzU0NDk3ODgyMTAyNGVmZmZlOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjA0MjFhM2I4NjgzMDNhMWY3ZDYxNDBkNGVjZDMyN2ZkMTYyYzRhNWU5ZjFi
10
+ MmQzZGNlNjg2NjFjMGE2OTI5ODRhNTFlNjE4Mzc3MTA3ZmJlZWFjNjljOGFm
11
+ NzhkNTFmMWI4NjE1YWFlM2M5OGNlMjk1NjU3NTg5NWY2YjljMWM=
12
+ data.tar.gz: !binary |-
13
+ ZjVhOGZhYzRhNjA4MDBkODI5YTQwMWJlOWRkMjQ2NjE1NTllMzE4YTg0NDg0
14
+ ZjNlOGE5MDQxNzliZmY5YmI1NGY5YzE5N2Q5NjJlMDUxNTQ1MTBkZjU2YWY2
15
+ YWNhNWMyM2NiMzk4YTFjYjkzNzU3ZDBhMDFmNGNjM2NkODc0OWU=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Eugene Gavrilov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # RailsCarrierwaveFocuspoint
2
+
3
+ CarrierWave extension for specifying focus point on image before they upload on server and cropped.
4
+
5
+ This gem based on [helper tool](http://jonom.github.io/jquery-focuspoint/demos/helper/index.html) of jquery.focuspoint javascript library (https://github.com/jonom/jquery-focuspoint)
6
+
7
+ ## Installation
8
+
9
+ in Gemfile
10
+
11
+ ```Ruby
12
+ gem 'rails-carrierwave-focuspoint'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ For using this gem you must add fields `focus_x:decimal` and `focus_y:decimal` to your db table used for storing CarrierWave picture information. Use migration:
18
+
19
+ ```Ruby
20
+ class AddFocusToUsers < ActiveRecord::Migration
21
+ def change
22
+ add_column :users, :focus_x, :decimal
23
+ add_column :users, :focus_y, :decimal
24
+ end
25
+ end
26
+ ```
27
+
28
+ in CarrierWave uploader
29
+
30
+ ```Ruby
31
+ class UserAvatarUploader < CarrierWave::Uploader::Base
32
+ include CarrierWave::MiniMagick
33
+
34
+ version :small do
35
+ process crop_with_focuspoint: [100, 100]
36
+ end
37
+
38
+ version :big do
39
+ process crop_with_focuspoint: [300, 200]
40
+ end
41
+ end
42
+ ```
43
+
44
+ in model
45
+
46
+ ```Ruby
47
+ class User < ActiveRecord::Base
48
+ mount_uploader :avatar, UserAvatarUploader
49
+ end
50
+ ```
51
+
52
+ in .js file
53
+
54
+ ```js
55
+ //= require focuspoint.js
56
+ //= require_self
57
+
58
+ $(document).ready(function() {
59
+ document.focuspoint.init(file_input_id: 'user_avatar');
60
+ });
61
+ ```
62
+
63
+ in .css file
64
+
65
+ ```css
66
+ /*
67
+ *= require focuspoint_control.css
68
+ */
69
+ ```
70
+
71
+ in view template
72
+
73
+ ```haml
74
+ = form_for @user do |f|
75
+ = f.input :avatar
76
+ = f.focuspoint_control :avatar
77
+ ```
78
+
79
+ ## LICENSE
80
+
81
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
@@ -0,0 +1,118 @@
1
+ document.focuspoint = (function() {
2
+ var exports = { };
3
+
4
+ var internal = {
5
+ focusPointAttr: {
6
+ x: 0,
7
+ y: 0
8
+ }
9
+ };
10
+
11
+ internal.bind_click_event = function(e){
12
+ var imageW = $(this).width();
13
+ var imageH = $(this).height();
14
+
15
+ //Calculate FocusPoint coordinates
16
+ var offsetX = e.pageX - $(this).offset().left;
17
+ var offsetY = e.pageY - $(this).offset().top;
18
+ var focusX = (offsetX/imageW - 0.5)*2;
19
+ var focusY = (offsetY/imageH - 0.5)*(-2);
20
+ internal.focusPointAttr.x = focusX;
21
+ internal.focusPointAttr.y = focusY;
22
+ internal.input_x.val(internal.focusPointAttr.x);
23
+ internal.input_y.val(internal.focusPointAttr.y);
24
+
25
+ if (exports.options.debug) {
26
+ console.log(internal.focusPointAttr.x);
27
+ console.log(internal.focusPointAttr.y);
28
+ }
29
+
30
+ //Leave a sweet target reticle at the focus point.
31
+ var percentageX = (offsetX/imageW)*100;
32
+ var percentageY = (offsetY/imageH)*100;
33
+ $('.focuspoint-control-target').css({
34
+ 'top':percentageY+'%',
35
+ 'left':percentageX+'%'
36
+ });
37
+ };
38
+
39
+ internal.init_picture_control = function() {
40
+ exports.focuspoint_control_target_overlay.on('load', function () {
41
+ internal.current_image_width = this.width;
42
+ internal.current_image_height = this.height;
43
+ });
44
+
45
+ exports.focuspoint_control_target_overlay.on('click', internal.bind_click_event);
46
+
47
+ if (exports.focuspoint_control_image.attr('src')) {
48
+ internal.current_image_width = exports.focuspoint_control_image.width();
49
+ internal.current_image_height = exports.focuspoint_control_image.height();
50
+
51
+ $('.focuspoint-control-target').css({
52
+ 'top': (((internal.input_y.val() || 0) / (-2)) + 0.5) * 100 + '%',
53
+ 'left': (((internal.input_x.val() || 0) / 2) + 0.5) * 100 + '%'
54
+ });
55
+ }
56
+
57
+ exports.options.file_input.change(function(){
58
+ if (exports.options.after_change) { exports.options.after_change(); }
59
+ internal.readURL(this);
60
+ });
61
+ };
62
+
63
+ internal.readURL = function(input)
64
+ {
65
+ if (input.files && input.files[0]) {
66
+ var reader = new FileReader();
67
+
68
+ reader.onload = function(e){
69
+ exports.focuspoint_control.show();
70
+ exports.focuspoint_control_image.attr('src', e.target.result);
71
+ exports.focuspoint_control_target_overlay.attr('src', e.target.result);
72
+ };
73
+ reader.readAsDataURL(input.files[0]);
74
+ }
75
+ };
76
+
77
+ internal.init_options = function(options) {
78
+ options = options || {};
79
+
80
+ options.file_input_id = options.file_input_id ? '#' + options.file_input_id : '#file_input_focuspoint';
81
+ options.file_input = $(options.file_input_id);
82
+
83
+ if (options.file_input.length > 0) {
84
+ internal.input_x = $('.focus_x');
85
+ internal.input_y = $('.focus_y');
86
+
87
+ internal.focuspoint_control_selector = '.focuspoint-control';
88
+ exports.focuspoint_control = $(internal.focuspoint_control_selector);
89
+ if (exports.focuspoint_control.length == 0) {
90
+ throw "element '" + internal.focuspoint_control_selector + "' not found";
91
+ }
92
+
93
+ exports.focuspoint_control_image = exports.focuspoint_control.find('img.focuspoint-control-image');
94
+ if (exports.focuspoint_control_image.length == 0) {
95
+ throw "element '" + internal.focuspoint_control_selector + " img.focuspoint-control-image" + "' not found";
96
+ }
97
+
98
+ exports.focuspoint_control_target_overlay = exports.focuspoint_control.find('img.focuspoint-control-target-overlay');
99
+ if (exports.focuspoint_control_target_overlay.length == 0) {
100
+ throw "element '" + internal.focuspoint_control_selector + " img.focuspoint-control-target-overlay" + "' not found";
101
+ }
102
+ }
103
+ exports.options = options;
104
+
105
+ return options;
106
+ };
107
+
108
+ exports.init = function(options) {
109
+ internal.init_options(options);
110
+
111
+ if (options.file_input.length > 0) {
112
+ internal.init_picture_control();
113
+ exports.is_initialized = true;
114
+ }
115
+ };
116
+
117
+ return exports;
118
+ })();
@@ -0,0 +1,38 @@
1
+ .focuspoint-control {
2
+ position: relative;
3
+ width: 300px;
4
+ overflow: hidden;
5
+ margin-bottom: 1em;
6
+ }
7
+
8
+ .focuspoint-control img {
9
+ display: block;
10
+ max-width: 100%;
11
+ height: auto;
12
+ }
13
+
14
+ .focuspoint-control img.focuspoint-control-target {
15
+ -webkit-transform: translate(-50%, -50%);
16
+ -ms-transform: translate(-50%, -50%);
17
+ transform: translate(-50%, -50%);
18
+ top: 50%;
19
+ left: 50%;
20
+ transition: all 500ms ease-in-out;
21
+ -webkit-transition: all 500ms ease-in-out;
22
+ -moz-transition: all 500ms ease-in-out;
23
+ display: block;
24
+ max-width: 100%;
25
+ height: auto;
26
+ position: absolute;
27
+ }
28
+
29
+ .focuspoint-control img.focuspoint-control-target-overlay{
30
+ position: absolute;
31
+ top: 0;
32
+ left: 0;
33
+ }
34
+
35
+ .focuspoint-control img.focuspoint-control-target-overlay {
36
+ cursor: pointer;
37
+ opacity: 0.01;
38
+ }
@@ -0,0 +1,22 @@
1
+ module FocuspointHelper
2
+ def focuspoint_control(attachment, opts = {})
3
+ attachment_instance = self.object.send(attachment)
4
+
5
+ if(attachment_instance.is_a? CarrierWave::Uploader::Base)
6
+ @template.content_tag(:div, class: 'focuspoint-control', style: ('display: none;' if self.object.new_record?)) do
7
+ @template.concat @template.content_tag(:img, nil, src: attachment_instance.url, class: 'focuspoint-control-image')
8
+ @template.concat @template.image_tag('focuspoint-target.png', class: 'focuspoint-control-target')
9
+ @template.concat @template.content_tag(:img, nil, src: attachment_instance.url, class: 'focuspoint-control-target-overlay')
10
+ [:focus_x ,:focus_y].each do |attribute|
11
+ @template.concat self.hidden_field attribute, class: attribute
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ if defined? ActionView::Helpers::FormBuilder
19
+ ActionView::Helpers::FormBuilder.class_eval do
20
+ include FocuspointHelper
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails-carrierwave-focuspoint/engine'
2
+ require 'rails-carrierwave-focuspoint/uploader_additions'
3
+
4
+ module RailsCarrierwaveFocuspoint
5
+ end
@@ -0,0 +1,4 @@
1
+ module RailsCarrierwaveFocuspoint
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,64 @@
1
+ module FocuspointRails
2
+ module UploaderAdditions
3
+ # Performs cropping with focuspoint
4
+ def crop_with_focuspoint(width = nil, height = nil)
5
+ if self.respond_to? "resize_to_limit"
6
+ begin
7
+ x = model.focus_x || 0
8
+ y = -(model.focus_y || 0)
9
+
10
+ manipulate! do |img|
11
+ orig_w = img['width']
12
+ orig_h = img['height']
13
+
14
+ ratio = width.to_f / height
15
+ orig_ratio = orig_w.to_f / orig_h
16
+
17
+ x_offset = 0
18
+ y_offset = 0
19
+ w = orig_w
20
+ h = orig_h
21
+ if ratio < orig_ratio
22
+ w = orig_h * ratio
23
+
24
+ half_w = w / 2.0
25
+ half_orig_w = orig_w / 2.0
26
+
27
+ x_offset = x * half_orig_w
28
+
29
+ x_offset = (x <=> 0.0) * (half_orig_w - half_w) if x != 0 && x_offset.abs > half_orig_w - half_w
30
+ elsif ratio > orig_ratio
31
+ h = orig_w / ratio
32
+
33
+ half_h = h / 2.0
34
+ half_orig_h = orig_h / 2.0
35
+
36
+ y_offset = y * half_orig_h
37
+
38
+ y_offset = (y <=> 0.0) * (half_orig_h - half_h) if y != 0 && y_offset.abs > half_orig_h - half_h
39
+ end
40
+
41
+ img.combine_options do |op|
42
+ op.crop "#{w.to_i}x#{h.to_i}#{'%+d' % x_offset.round}#{'%+d' % y_offset.round}"
43
+ op.gravity 'Center'
44
+ end
45
+ img.resize("#{width}x#{height}")
46
+ img
47
+ end
48
+
49
+ rescue Exception => e
50
+ raise "Failed to crop - #{e.message}"
51
+ end
52
+
53
+ else
54
+ raise "Failed to crop #{attachment}. Add mini_magick."
55
+ end
56
+ end
57
+ end ## End of UploaderAdditions
58
+ end ## End of FocuspointRails
59
+
60
+ if defined? CarrierWave::Uploader::Base
61
+ CarrierWave::Uploader::Base.class_eval do
62
+ include FocuspointRails::UploaderAdditions
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module RailsCarrierwaveFocuspoint
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-carrierwave-focuspoint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - evg2108
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.21
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.21
27
+ description: RailsCarrierwaveFocuspoint is a wrapper for jquery-focuspoint library.
28
+ email:
29
+ - evg2108@yandex.ru
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/images/focuspoint-target.png
38
+ - app/assets/javascripts/focuspoint_control.js
39
+ - app/assets/stylesheets/focuspoint_control.css
40
+ - app/helpers/focuspoint_helper.rb
41
+ - lib/rails-carrierwave-focuspoint.rb
42
+ - lib/rails-carrierwave-focuspoint/engine.rb
43
+ - lib/rails-carrierwave-focuspoint/uploader_additions.rb
44
+ - lib/rails-carrierwave-focuspoint/version.rb
45
+ homepage:
46
+ licenses: []
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: RailsCarrierwaveFocuspoint is a wrapper for jquery-focuspoint library.
68
+ test_files: []