carrierwave-crop-on-fly 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -2
- data/lib/carrierwave/crop/uploader_additions.rb +49 -26
- data/lib/carrierwave/crop/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f927f9ed855671678507c3161d27858bf319b856
|
4
|
+
data.tar.gz: 1e7f42e6eaa650022d24d1d2779c1fdb2fe7f012
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63b28fa20eb4e74a03efaae32e3f8f10ab17ea4816dcaf34f93fe0dfcdd9d32b0a0a78e23e5e02eef31356f093f805046b82036bc6217604057e03e9030427f4
|
7
|
+
data.tar.gz: 203a5878dabdcbc68b539e2959e89203f5f7242555a239f111b73cddc3410d6fbc4bd5602a9333565713d57190a6de3ffb7bf57aa1978ea5562328a808a7e7ba
|
data/README.md
CHANGED
@@ -8,19 +8,33 @@ this gem based on [carrierwave-crop](https://github.com/kirtithorat/carrierwave-
|
|
8
8
|
|
9
9
|
Install the latest stable release:
|
10
10
|
|
11
|
+
```Shell
|
11
12
|
$[sudo] gem install carrierwave-crop-on-fly
|
13
|
+
```
|
12
14
|
|
13
15
|
In Rails, add it to your Gemfile:
|
14
16
|
|
17
|
+
```Ruby
|
15
18
|
gem 'carrierwave-crop-on-fly'
|
19
|
+
```
|
16
20
|
|
17
|
-
Also you must add mini_magick gem
|
21
|
+
Also you must add mini_magick or rmagick gem
|
18
22
|
|
23
|
+
```Ruby
|
19
24
|
gem 'mini_magick'
|
25
|
+
```
|
26
|
+
|
27
|
+
or
|
28
|
+
|
29
|
+
```Ruby
|
30
|
+
gem 'rmagick'
|
31
|
+
```
|
20
32
|
|
21
33
|
And then execute:
|
22
34
|
|
35
|
+
```Shell
|
23
36
|
$ bundle
|
37
|
+
```
|
24
38
|
|
25
39
|
Finally, restart the server to apply the changes.
|
26
40
|
|
@@ -30,6 +44,7 @@ Add the required files in assets
|
|
30
44
|
|
31
45
|
In .js file
|
32
46
|
|
47
|
+
```javascript
|
33
48
|
//= require jquery
|
34
49
|
//= require jcrop.js
|
35
50
|
//= require_self
|
@@ -37,35 +52,48 @@ In .js file
|
|
37
52
|
$(document).ready(function() {
|
38
53
|
document.jcrop.init({ file_input_id: 'user_avatar' });
|
39
54
|
});
|
55
|
+
```
|
40
56
|
|
41
57
|
In .css file
|
42
58
|
|
59
|
+
```css
|
43
60
|
*= require jquery.jcrop
|
44
61
|
*= require jcrop_fix
|
62
|
+
```
|
45
63
|
|
46
64
|
## Usage
|
47
65
|
|
48
66
|
Open your model file and add the CarrierWave uploader:
|
49
67
|
|
68
|
+
```Ruby
|
50
69
|
class User < ActiveRecord::Base
|
51
70
|
mount_uploader :avatar, AvatarUploader
|
52
71
|
end
|
72
|
+
```
|
53
73
|
|
54
74
|
In the CarrierWave uploader:
|
55
75
|
|
56
|
-
|
76
|
+
```Ruby
|
77
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
78
|
+
# use mini_magick
|
57
79
|
include CarrierWave::MiniMagick
|
80
|
+
# or you may use rmagick:
|
81
|
+
# include CarrierWave::RMagick
|
82
|
+
|
58
83
|
process crop: [100, 100]
|
59
84
|
end
|
85
|
+
```
|
60
86
|
|
61
87
|
In the view:
|
62
88
|
|
89
|
+
```erb
|
63
90
|
<%= form_for @user do |f| %>
|
64
91
|
<%= f.file_field :avatar %>
|
65
92
|
<%= f.cropbox :avatar, width: 300, height: 300 %>
|
66
93
|
<%= f.previewbox :avatar %>
|
67
94
|
<%= f.submit 'Crop' %>
|
68
95
|
<% end %>
|
96
|
+
```
|
69
97
|
|
70
98
|
### Credits and resources
|
71
99
|
* [CarrierWave](https://github.com/carrierwaveuploader/carrierwave)
|
@@ -6,26 +6,33 @@ module CarrierWave
|
|
6
6
|
# Resizes the original image to 600x600 and then performs cropping
|
7
7
|
# process crop: [600, 600]
|
8
8
|
def crop(width = nil, height = nil)
|
9
|
-
|
10
|
-
begin
|
11
|
-
manipulate! do |img|
|
12
|
-
if crop_w.present? && crop_h.present?
|
13
|
-
w = crop_w
|
14
|
-
h = crop_h
|
15
|
-
else
|
16
|
-
orig_w = img['width']
|
17
|
-
orig_h = img['height']
|
9
|
+
raise_missing_processor unless defined?(CarrierWave::MiniMagick) || defined?(CarrierWave::RMagick)
|
18
10
|
|
19
|
-
|
20
|
-
|
11
|
+
proc = ->(original_height, original_width) do
|
12
|
+
if crop_w.present? && crop_h.present?
|
13
|
+
w = crop_w
|
14
|
+
h = crop_h
|
15
|
+
else
|
16
|
+
ratio = width.to_f / height
|
17
|
+
orig_ratio = original_height / original_width
|
21
18
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
19
|
+
w = h = [original_height, original_width].min
|
20
|
+
if ratio < orig_ratio
|
21
|
+
w = original_width * ratio
|
22
|
+
elsif ratio > orig_ratio
|
23
|
+
h = original_height * ratio
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
{ width: w, height: h }
|
28
|
+
end
|
29
|
+
|
30
|
+
if self.is_a?(CarrierWave::MiniMagick)
|
31
|
+
begin
|
32
|
+
manipulate! do |img|
|
33
|
+
sizes = proc.call(img['width'], img['height'])
|
34
|
+
h = sizes[:height]
|
35
|
+
w = sizes[:width]
|
29
36
|
|
30
37
|
if crop_x.blank? && crop_y.blank?
|
31
38
|
img.combine_options do |op|
|
@@ -43,18 +50,34 @@ module CarrierWave
|
|
43
50
|
raise CarrierWave::Crop::ProcessingError, "Failed to crop - #{e.message}"
|
44
51
|
end
|
45
52
|
|
53
|
+
elsif self.is_a?(CarrierWave::RMagick)
|
54
|
+
begin
|
55
|
+
manipulate! do |img|
|
56
|
+
sizes = proc.call(img.columns, img.rows)
|
57
|
+
h = sizes[:height]
|
58
|
+
w = sizes[:width]
|
59
|
+
|
60
|
+
if crop_x.blank? && crop_y.blank?
|
61
|
+
img.crop! 'Center', 0, 0, w.to_i, h.to_i
|
62
|
+
else
|
63
|
+
img.crop! crop_x.to_i, crop_y.to_i, w.to_i, h.to_i
|
64
|
+
end
|
65
|
+
img.resize! width, height
|
66
|
+
end
|
67
|
+
|
68
|
+
rescue Exception => e
|
69
|
+
raise CarrierWave::Crop::ProcessingError, "Failed to crop - #{e.message}"
|
70
|
+
end
|
46
71
|
else
|
47
|
-
|
72
|
+
raise_missing_processor
|
48
73
|
end
|
49
74
|
end
|
50
75
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
# crop_x.present? && crop_y.present? && crop_w.present? && crop_h.present?
|
57
|
-
# end
|
76
|
+
private
|
77
|
+
|
78
|
+
def raise_missing_processor
|
79
|
+
raise CarrierWave::Crop::MissingProcessorError, "Failed to crop #{attachment}. Add mini_magick or rmagick."
|
80
|
+
end
|
58
81
|
|
59
82
|
end ## End of UploaderAdditions
|
60
83
|
end ## End of Crop
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-crop-on-fly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- evg2108
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.4.
|
100
|
+
rubygems_version: 2.4.6
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: CarrierWave extension for on fly specifying crop area with preview and cropping
|