rails_responsive_images 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 533708a79d576f39b8d794bc49edd323b38f400d
4
- data.tar.gz: f197d41ae752d62b983e1f92dbb1866d02a1aa69
3
+ metadata.gz: 843b8ec2d4b097ea84360d505d967f80bc26eed4
4
+ data.tar.gz: 00becd925cc066477f201671c119c0566e29fdb6
5
5
  SHA512:
6
- metadata.gz: 390c812e13f4ef90f387a499d72e4d30ac9eafce3f9f8cc7d6ce225e1ed9f4c1aa55f7469eac279d75b881b7d953064ba9799fac3ebdd0902f5c8f19b416ee6f
7
- data.tar.gz: ec61eb527efd3b44f9121bb185472c43a7471d61f45ce9682a058e4694df59fe8b9057b522216ab1cd78cfe8c6d19e79b51e051d36ab374c0b8f9452d5f757a1
6
+ metadata.gz: eb312fc7220ea6e51af9d00f1e56dc3e54e1ad5b8b934ac833089cb6a26e20eff66d2506fa50388de44c0c1cc6f79ab0d5259e2e8b613c80688e4e559508bca1
7
+ data.tar.gz: 07507cd44debe7ded4894ccffd313cc75758f26f788007d19de0700c790a4cf8f81e521f3cf064fbe6373df9bb5a5e2f64b1e456cbca49e8bbff3a6cb5a95b9f
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
- # RailsResponsiveImages
1
+ # Rails responsive images
2
2
 
3
- A Rails image_tag() extension to generate HTML5 <picture> tag markup from the W3C HTML Responsive Images Extension Proposal.
3
+ A Rails image_tag() extension to generate HTML5 <picture> tag markup from the W3C HTML Responsive Images Extension Proposal. Dont't care about image resizing, the gem will do the work for you - on the fly ;-) or just use the rake task "rake rails_responsive_images".
4
4
 
5
+ The following image_tag
5
6
  ```ruby
7
+ = image_tag 'awesome/picture.jpeg', alt: 'awesome', responsive: true
8
+ ```
9
+ will generate this output:
10
+ ```html
6
11
  <picture>
7
- <source media="(max-width: 766px)" srcset="/assets/responsive_images_766/picture.jpg">
8
- <source media="(max-width: 990px)" srcset="/assets/responsive_images_990/picture.jpg">
9
- <source media="(max-width: 200px)" srcset="/assets/responsive_images_200/picture.jpg">
12
+ <source media="(max-width: 767px)" srcset="/assets/responsive_images_767/picture.jpg">
13
+ <source media="(max-width: 991px)" srcset="/assets/responsive_images_991/picture.jpg">
14
+ <source media="(max-width: 1999px)" srcset="/assets/responsive_images_1999/picture.jpg">
10
15
  <img width="2568" height="878" alt="awesome" src="/assets/picture.jpg">
11
16
  </picture>
12
17
  ```
@@ -44,6 +49,10 @@ Require jquery-picture for cross browser support
44
49
  ```ruby
45
50
  = image_tag 'awesome/picture.jpeg', alt: 'awesome', responsive: true
46
51
  ```
52
+ ## TODO
53
+ - write tests
54
+ - write docs
55
+ - review & cleanup the code
47
56
 
48
57
  ## Contributing
49
58
 
@@ -1,6 +1,4 @@
1
1
  module RailsResponsiveImages
2
- require 'RMagick'
3
- require 'fileutils'
4
2
  class AssetsController < ActionController::Base
5
3
  def responsive_image
6
4
  filepath = params[:filepath]
@@ -18,29 +16,14 @@ module RailsResponsiveImages
18
16
  full_original_filepath = Rails.root.join('app', 'assets', 'images', original_filepath, image_filename)
19
17
  full_responsive_filepath = Rails.root.join('app', 'assets', 'images', filepath, image_filename)
20
18
 
21
- create_responsive_folder!(full_responsive_filepath)
19
+ Image.instance.create_responsive_folder!(full_responsive_filepath)
22
20
 
23
21
  fail ActionController::RoutingError.new('Not found') unless File.exist?(full_original_filepath)
24
22
 
25
- generate_responsive_image!(full_original_filepath, requested_image_size, full_responsive_filepath)
23
+ Image.instance.generate_responsive_image!(full_original_filepath, requested_image_size, full_responsive_filepath)
26
24
 
27
25
  mime_type = MIME::Types.type_for("#{full_responsive_filepath}").first.content_type
28
26
  send_file full_responsive_filepath, type: mime_type, disposition: :inline
29
27
  end
30
-
31
- private
32
-
33
- def create_responsive_folder!(path)
34
- dirname = File.dirname(path)
35
- unless File.directory?(dirname)
36
- FileUtils.mkdir_p(dirname)
37
- end
38
- end
39
-
40
- def generate_responsive_image!(original_image, size, output_path)
41
- img = ::Magick::Image.read(original_image).first
42
- img = img.resize_to_fit(size, size)
43
- img.write(output_path)
44
- end
45
28
  end
46
29
  end
@@ -0,0 +1,20 @@
1
+ module RailsResponsiveImages
2
+ require 'RMagick'
3
+ require 'fileutils'
4
+ class Image
5
+ include Singleton
6
+
7
+ def create_responsive_folder!(path)
8
+ dirname = File.dirname(path)
9
+ unless File.directory?(dirname)
10
+ FileUtils.mkdir_p(dirname)
11
+ end
12
+ end
13
+
14
+ def generate_responsive_image!(original_image, size, output_path)
15
+ img = ::Magick::Image.read(original_image).first
16
+ img = img.resize_to_fit(size, size)
17
+ img.write(output_path)
18
+ end
19
+ end
20
+ end
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
- Rails.application.routes.draw do
1
+ ::Rails.application.routes.draw do
2
2
  get '/assets/*filepath/:filename', to: 'rails_responsive_images/assets#responsive_image'
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module RailsResponsiveImages
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,38 @@
1
+ require "rails_responsive_images"
2
+
3
+ desc "Rails responsive images builds different sized versions from your images inside of the asset folder"
4
+ task rails_responsive_images: [ 'rails_responsive_images:check_requirements', 'rails_responsive_images:resize' ]
5
+
6
+ namespace :rails_responsive_images do
7
+ desc "Check for required programms"
8
+ task :check_requirements do
9
+ RakeFileUtils.verbose(false)
10
+ tools = %w[convert] # imagemagick
11
+ puts "\nResize images with the following tools:"
12
+ tools.delete_if { |tool| sh('which', tool) rescue false }
13
+ raise "The following tools must be installed and accessible from the execution path: #{ tools.join(', ') }\n\n" if tools.size > 0
14
+ end
15
+
16
+ task :resize do
17
+ RakeFileUtils.verbose(false)
18
+ start_time = Time.now
19
+
20
+ file_list = FileList.new(Rails.root.join('app', 'assets', 'images/**/*.{gif,jpeg,jpg,png}').to_s) do |f|
21
+ f.exclude(/(responsive_images_)\d+/)
22
+ end
23
+
24
+ puts "\nResize #{ file_list.size } image files."
25
+
26
+ ::RailsResponsiveImages.configuration.image_sizes.each do |size|
27
+ file_list.split(' ').first.each do |original_filepath|
28
+ filepath = original_filepath.gsub(Rails.root.join('app', 'assets', 'images').to_s, '')
29
+ responsive_filepath = Rails.root.join('app', 'assets', 'images', "responsive_images_#{size}", filepath.sub(/\A\//, ''))
30
+ ::RailsResponsiveImages::Image.instance.create_responsive_folder!(responsive_filepath)
31
+ ::RailsResponsiveImages::Image.instance.generate_responsive_image!(original_filepath, size, responsive_filepath)
32
+ end
33
+ end
34
+
35
+ minutes, seconds = (Time.now - start_time).divmod 60
36
+ puts "\nTotal run time: #{minutes}m #{seconds.round}s\n"
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_responsive_images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Chilian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-15 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,12 +95,14 @@ files:
95
95
  - README.md
96
96
  - Rakefile
97
97
  - app/controllers/rails_responsive_images/assets_controller.rb
98
+ - app/models/rails_responsive_images/image.rb
98
99
  - config/routes.rb
99
100
  - lib/assets/javascripts/jquery-picture.js
100
101
  - lib/rails_responsive_images.rb
101
102
  - lib/rails_responsive_images/configuration.rb
102
103
  - lib/rails_responsive_images/engine.rb
103
104
  - lib/rails_responsive_images/version.rb
105
+ - lib/tasks/rails_responsive_images.rake
104
106
  - rails_responsive_images.gemspec
105
107
  - spec/spec_helper.rb
106
108
  homepage: https://chilian.de