Skip to content

Commit

Permalink
Process images on display, not on upload. Should close #103
Browse files Browse the repository at this point in the history
  • Loading branch information
jcn committed Oct 29, 2013
1 parent 6441266 commit e60b9f9
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,18 @@ Much of the AwesomeBits interface has been localized. Localization files are
in the config/locales/ directory with a separate file for each language.


Images
------

Images are resized dynamically via the [Magickly gem](http://magickly.afeld.me).
In order to display images properly, you must have a Magickly installation running
and you must set the MAGICKLY_HOST environment variable to point to that host.

e.g. If you are running Magickly localy on port 8888, run AwesomeBits using the command:

MAGICKLY_HOST=http://localhost:8888 thin start


License
-------

Expand Down
Binary file added app/assets/images/no-image-original.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 45 additions & 5 deletions app/models/photo.rb
Expand Up @@ -3,12 +3,52 @@ class Photo < ActiveRecord::Base

belongs_to :project
has_attached_file :image,
:default_style => :main,
:styles => {
:main => "#{MAIN_DIMENSIONS}#",
:index => "500x300#"
},
:default_url => "/assets/no-image-:style.png"

attr_accessible :image

# Build a URL to dynamically resize application images via an external service
# Currently using http://magickly.afeld.me/
def url(size = nil)
case size

when :main
image.present? ? cropped_image_url("#{MAIN_DIMENSIONS}#") : image.url(:main)

when :index
image.present? ? cropped_image_url("500x300#") : image.url(:index)

else
image_url
end
end

protected

def image_url
image.url(:original, :timestamp => false)
end

def cropped_image_url(crop)
[image_host, "q", image_path(crop)].join("/")
end

def image_host
ENV['MAGICKLY_HOST']
end

def image_path(crop)
[ "src", image_with_host(image_url), "thumb", crop ].collect { |part| CGI.escape(part) }.join("/")
end

def image_with_host(image_url)
base_uri = URI(ENV['DEFAULT_URL'] || "http://localhost:3000")

uri = URI(image_url)
uri.scheme ||= base_uri.scheme
uri.host ||= base_uri.host
uri.port ||= base_uri.port

uri.to_s
end
end
4 changes: 2 additions & 2 deletions app/models/project.rb
Expand Up @@ -136,9 +136,9 @@ def new_photos=(photos)

def display_images
if photos.empty?
[Photo.new.image]
[Photo.new]
else
photos.map(&:image)
photos
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_project.html.erb
Expand Up @@ -55,7 +55,7 @@
<h3><%= t ".images" %></h3>
<p>
<% project.photos.each_with_index do |image, index| %>
<%= link_to t(".image", :index => index+1), image.image.url, :rel => "project-#{project.id}-images", :class => "lightbox", :target => "blank" %>
<%= link_to t(".image", :index => index+1), image.url(:main), :rel => "project-#{project.id}-images", :class => "lightbox", :target => "blank" %>
<% end %>
</p>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/index.html.erb
Expand Up @@ -61,7 +61,7 @@
$('#filter_start').datepicker({dateFormat: 'yy-mm-dd', onClose: function(){ $('#filter_start').blur() }});
$('#filter_end').datepicker({dateFormat: 'yy-mm-dd', onClose: function(){ $('#filter_end').blur() }});

$('a.lightbox').colorbox();
$('a.lightbox').colorbox({iframe: true, innerWidth: 940, innerHeight: 470});
});
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/projects/public_show.html.erb
Expand Up @@ -12,7 +12,7 @@
<section class="project-gallery" id="project-gallery">
<ul class="viewport">
<% @project.display_images.reverse_each do |image| %>
<li class="piece image-wrapper"> <%= image_tag(image.url) %> </li>
<li class="piece image-wrapper"> <%= image_tag(image.url(:main)) %> </li>
<% end %>
</ul>
<% if @project.photos.count > 1 %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/show.html.erb
Expand Up @@ -20,7 +20,7 @@
<% content_for :javascript do %>
<% javascript_tag do %>
$(window).load(function(){
$('a.lightbox').colorbox();
$('a.lightbox').colorbox({iframe: true, innerWidth: 940, innerHeight: 470});
});
<% end %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/initializers/magickly.rb
@@ -0,0 +1 @@
ENV['MAGICKLY_HOST'] ||= "http://img.awesomefoundation.org"
14 changes: 14 additions & 0 deletions spec/models/photo_spec.rb
Expand Up @@ -4,4 +4,18 @@
describe Photo do
it { should belong_to :project }
it { should have_attached_file :image }

context 'with an image' do
let(:photo) { create(:photo) }

it "returns the base photo url as the original photo" do
photo.url.should == photo.image.url(:original, :timestamp => false)
end

it "returns a dynamic photo url as a cropped photo" do
photo.url(:main).should match(/#{ENV['MAGICKLY_HOST']}/)
photo.url(:main).should match(/thumb\/#{CGI.escape(Photo::MAIN_DIMENSIONS)}/)
photo.url(:main).should match(/#{CGI.escape(photo.url)}/)
end
end
end
2 changes: 1 addition & 1 deletion spec/models/project_spec.rb
Expand Up @@ -270,7 +270,7 @@
it "returns the photos if there are any" do
photo = create(:photo)
project.photos = [photo]
project.display_images.map(&:url).should == [photo.image.url]
project.display_images.map(&:url).should == [photo.url]
end

it "returns a new photo if there aren't any" do
Expand Down

0 comments on commit e60b9f9

Please sign in to comment.