carrierwave_retina 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/carrierwave_retina.gemspec +25 -0
- data/lib/carrierwave_retina.rb +11 -0
- data/lib/carrierwave_retina/optimizer.rb +50 -0
- data/lib/carrierwave_retina/version.rb +3 -0
- data/spec/carrierwave_retina_spec.rb +70 -0
- data/spec/fixtures/images/avatar.jpeg +0 -0
- data/spec/spec_helper.rb +3 -0
- data/vendor/assets/javascripts/retina.js +137 -0
- metadata +141 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Johan van Zonneveld
|
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,39 @@
|
|
1
|
+
## CarrierWave Retina Optimzer [![Build Status](https://secure.travis-ci.org/jhnvz/carrierwave_retina.png?branch=master)](http://travis-ci.org/jhnvz/carrierwave_retina)
|
2
|
+
|
3
|
+
Automatically generates retina versions of your uploaded images
|
4
|
+
|
5
|
+
Installation
|
6
|
+
------------
|
7
|
+
|
8
|
+
1. Add `gem 'carrierwave_retina'` to your Gemfile.
|
9
|
+
1. Run `bundle install`.
|
10
|
+
1. Add `//= require retina` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
|
11
|
+
1. Add `include CarrierWaveRetina::Optimizer` to the bottom of your uploader
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class ExampleUploader < CarrierWave::Uploader::Base
|
15
|
+
|
16
|
+
version :small do
|
17
|
+
process :resize_to_fill => [30, 30]
|
18
|
+
end
|
19
|
+
|
20
|
+
include CarrierWaveRetina::Optimizer
|
21
|
+
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
1. Restart your server and you're now using retina optimizer!
|
26
|
+
|
27
|
+
## Note on Patches/Pull Requests
|
28
|
+
|
29
|
+
* Fork the project.
|
30
|
+
* Make your feature addition or bug fix.
|
31
|
+
* Add tests for it. This is important so I don't break it in a
|
32
|
+
future version unintentionally.
|
33
|
+
* Commit, do not mess with rakefile, version, or history.
|
34
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
35
|
+
* Send me a pull request. Bonus points for topic branches.
|
36
|
+
|
37
|
+
## Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2012 Johan van Zonneveld. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'carrierwave_retina/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "carrierwave_retina"
|
8
|
+
gem.version = CarrierWaveRetina::VERSION
|
9
|
+
gem.authors = ["Johan van Zonneveld"]
|
10
|
+
gem.email = ["johan@vzonneveld.nl"]
|
11
|
+
gem.homepage = 'https://github.com/jhnvz/carrierwave_retina.git'
|
12
|
+
gem.summary = %q{Automatically generates retina versions of your uploaded images}
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency "bundler", ">= 1.0.0"
|
20
|
+
gem.add_development_dependency "rspec", ">= 2.3"
|
21
|
+
|
22
|
+
gem.add_dependency 'carrierwave'
|
23
|
+
gem.add_dependency 'rails', '>= 3.0'
|
24
|
+
gem.add_dependency 'rmagick'
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module CarrierWaveRetina
|
2
|
+
|
3
|
+
module Optimizer
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
|
9
|
+
## Define retina version based on defined versions in the uploader class
|
10
|
+
versions.each do |v|
|
11
|
+
|
12
|
+
processor = nil
|
13
|
+
|
14
|
+
## Check if there's a resize processor to get the dimensions
|
15
|
+
v[1][:uploader].processors.each do |p|
|
16
|
+
processor = p if p[0].to_s.scan(/resize_to_fill|resize_to_limit|resize_to_fit|resize_and_pad/).any?
|
17
|
+
end
|
18
|
+
|
19
|
+
## Define a retina version if processor is present
|
20
|
+
if processor
|
21
|
+
|
22
|
+
options = processor[1].dup
|
23
|
+
|
24
|
+
width = options[0] * 2
|
25
|
+
height = options[1] * 2
|
26
|
+
|
27
|
+
2.times { options.delete_at(0) }
|
28
|
+
|
29
|
+
options.insert(0, height)
|
30
|
+
options.insert(0, width)
|
31
|
+
|
32
|
+
version "#{v[0]}_retina" do
|
33
|
+
process processor[0] => options
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
## Set the correct filename for storage according to the convention (append @2x to filename)
|
42
|
+
def full_filename(for_file)
|
43
|
+
super.tap do |file_name|
|
44
|
+
file_name.gsub!('.', '@2x.').gsub!('retina_', '') if version_name.to_s.include?('retina')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class AnonymousUploader < CarrierWave::Uploader::Base
|
4
|
+
|
5
|
+
include CarrierWave::RMagick
|
6
|
+
|
7
|
+
version :small do
|
8
|
+
process :resize_to_fill => [30, 30]
|
9
|
+
end
|
10
|
+
|
11
|
+
version :small_without_processor
|
12
|
+
|
13
|
+
include CarrierWaveRetina::Optimizer
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class UploaderModel
|
18
|
+
|
19
|
+
extend CarrierWave::Mount
|
20
|
+
|
21
|
+
attr_accessor :avatar
|
22
|
+
attr_accessor :id
|
23
|
+
|
24
|
+
mount_uploader :avatar, AnonymousUploader
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
self.id = 999
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe CarrierWaveRetina::Optimizer do
|
33
|
+
|
34
|
+
include CarrierWave::Test::Matchers
|
35
|
+
|
36
|
+
subject { AnonymousUploader }
|
37
|
+
|
38
|
+
before do
|
39
|
+
AnonymousUploader.enable_processing = true
|
40
|
+
@uploader = AnonymousUploader.new(UploaderModel.new, :avatar)
|
41
|
+
@uploader.store!(File.open("#{File.dirname(__FILE__)}/fixtures/images/avatar.jpeg"))
|
42
|
+
end
|
43
|
+
|
44
|
+
after do
|
45
|
+
AnonymousUploader.enable_processing = false
|
46
|
+
@uploader.remove!
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with processor' do
|
50
|
+
|
51
|
+
its(:versions) { should include :small_retina }
|
52
|
+
|
53
|
+
it { @uploader.small.should have_dimensions(30, 30) }
|
54
|
+
it { @uploader.small_retina.should have_dimensions(60, 60) }
|
55
|
+
|
56
|
+
it { File.basename(@uploader.small.current_path, 'jpeg').should include 'small_'}
|
57
|
+
|
58
|
+
it { File.basename(@uploader.small_retina.current_path, 'jpeg').should include '@2x'}
|
59
|
+
it { File.basename(@uploader.small_retina.current_path, 'jpeg').should_not include 'retina_'}
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'without processor' do
|
64
|
+
|
65
|
+
its(:versions) { should include :small_without_processor }
|
66
|
+
its(:versions) { should_not include :small_without_processor_retina }
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
(function() {
|
2
|
+
|
3
|
+
var root = (typeof exports == 'undefined' ? window : exports);
|
4
|
+
|
5
|
+
var config = {
|
6
|
+
// Ensure Content-Type is an image before trying to load @2x image
|
7
|
+
// https://github.com/imulus/retinajs/pull/45)
|
8
|
+
check_mime_type: true
|
9
|
+
};
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
root.Retina = Retina;
|
14
|
+
|
15
|
+
function Retina() {}
|
16
|
+
|
17
|
+
Retina.configure = function(options) {
|
18
|
+
if (options == null) options = {};
|
19
|
+
for (var prop in options) config[prop] = options[prop];
|
20
|
+
};
|
21
|
+
|
22
|
+
Retina.init = function(context) {
|
23
|
+
if (context == null) context = root;
|
24
|
+
|
25
|
+
var existing_onload = context.onload || new Function;
|
26
|
+
|
27
|
+
context.onload = function() {
|
28
|
+
var images = document.getElementsByTagName("img"), retinaImages = [], i, image;
|
29
|
+
for (i = 0; i < images.length; i++) {
|
30
|
+
image = images[i];
|
31
|
+
if ($(image).attr('data-retina') != 'true') {
|
32
|
+
retinaImages.push(new RetinaImage(image));
|
33
|
+
}
|
34
|
+
}
|
35
|
+
existing_onload();
|
36
|
+
}
|
37
|
+
};
|
38
|
+
|
39
|
+
Retina.isRetina = function(){
|
40
|
+
var mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\
|
41
|
+
(min--moz-device-pixel-ratio: 1.5),\
|
42
|
+
(-o-min-device-pixel-ratio: 3/2),\
|
43
|
+
(min-resolution: 1.5dppx)";
|
44
|
+
|
45
|
+
if (root.devicePixelRatio > 1)
|
46
|
+
return true;
|
47
|
+
|
48
|
+
if (root.matchMedia && root.matchMedia(mediaQuery).matches)
|
49
|
+
return true;
|
50
|
+
|
51
|
+
return false;
|
52
|
+
};
|
53
|
+
|
54
|
+
|
55
|
+
root.RetinaImagePath = RetinaImagePath;
|
56
|
+
|
57
|
+
function RetinaImagePath(path) {
|
58
|
+
this.path = path;
|
59
|
+
this.at_2x_path = path.replace(/\.\w+$/, function(match) { return "@2x" + match; });
|
60
|
+
}
|
61
|
+
|
62
|
+
RetinaImagePath.confirmed_paths = [];
|
63
|
+
|
64
|
+
RetinaImagePath.prototype.is_external = function() {
|
65
|
+
return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) )
|
66
|
+
}
|
67
|
+
|
68
|
+
RetinaImagePath.prototype.check_2x_variant = function(callback) {
|
69
|
+
var http, that = this;
|
70
|
+
if (this.is_external()) {
|
71
|
+
return callback(false);
|
72
|
+
} else if (this.at_2x_path in RetinaImagePath.confirmed_paths) {
|
73
|
+
return callback(true);
|
74
|
+
} else {
|
75
|
+
http = new XMLHttpRequest;
|
76
|
+
http.open('HEAD', this.at_2x_path);
|
77
|
+
http.onreadystatechange = function() {
|
78
|
+
if (http.readyState != 4) {
|
79
|
+
return callback(false);
|
80
|
+
}
|
81
|
+
|
82
|
+
if (http.status >= 200 && http.status <= 399) {
|
83
|
+
if (config.check_mime_type) {
|
84
|
+
var type = http.getResponseHeader('Content-Type');
|
85
|
+
if (type == null || !type.match(/^image/i)) {
|
86
|
+
return callback(false);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
RetinaImagePath.confirmed_paths.push(that.at_2x_path);
|
91
|
+
return callback(true);
|
92
|
+
} else {
|
93
|
+
return callback(false);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
http.send();
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
function RetinaImage(el) {
|
103
|
+
this.el = el;
|
104
|
+
this.path = new RetinaImagePath(this.el.getAttribute('src'));
|
105
|
+
var that = this;
|
106
|
+
this.path.check_2x_variant(function(hasVariant) {
|
107
|
+
if (hasVariant) that.swap();
|
108
|
+
});
|
109
|
+
}
|
110
|
+
|
111
|
+
root.RetinaImage = RetinaImage;
|
112
|
+
|
113
|
+
RetinaImage.prototype.swap = function(path) {
|
114
|
+
if (typeof path == 'undefined') path = this.path.at_2x_path;
|
115
|
+
|
116
|
+
var that = this;
|
117
|
+
function load() {
|
118
|
+
if (! that.el.complete) {
|
119
|
+
setTimeout(load, 5);
|
120
|
+
} else {
|
121
|
+
that.el.setAttribute('width', that.el.offsetWidth);
|
122
|
+
that.el.setAttribute('height', that.el.offsetHeight);
|
123
|
+
that.el.setAttribute('src', path);
|
124
|
+
that.el.setAttribute('data-retina', true);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
load();
|
128
|
+
}
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
if (Retina.isRetina()) {
|
134
|
+
Retina.init(root);
|
135
|
+
}
|
136
|
+
|
137
|
+
})();
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave_retina
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Johan van Zonneveld
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-13 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.0.0
|
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.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.3'
|
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: '2.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: carrierwave
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
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: rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rmagick
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
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
|
+
description:
|
95
|
+
email:
|
96
|
+
- johan@vzonneveld.nl
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- carrierwave_retina.gemspec
|
107
|
+
- lib/carrierwave_retina.rb
|
108
|
+
- lib/carrierwave_retina/optimizer.rb
|
109
|
+
- lib/carrierwave_retina/version.rb
|
110
|
+
- spec/carrierwave_retina_spec.rb
|
111
|
+
- spec/fixtures/images/avatar.jpeg
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
- vendor/assets/javascripts/retina.js
|
114
|
+
homepage: https://github.com/jhnvz/carrierwave_retina.git
|
115
|
+
licenses: []
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.24
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: Automatically generates retina versions of your uploaded images
|
138
|
+
test_files:
|
139
|
+
- spec/carrierwave_retina_spec.rb
|
140
|
+
- spec/fixtures/images/avatar.jpeg
|
141
|
+
- spec/spec_helper.rb
|