Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Autorotate uploaded images based on EXIF orientation

This uses paperclip's before_post_process filter
to rotate an image before paperclip deals with it.
  • Loading branch information...
commit a2cabf4ba0fcac9b0a6c42aa052624b1eabaf16b 1 parent da5c518
@mhl mhl authored
View
15 app/models/campaign_photo.rb
@@ -4,10 +4,21 @@ class CampaignPhoto < ActiveRecord::Base
validates_attachment_content_type :image,
:content_type => %w( image/jpeg image/png image/gif image/pjpeg image/x-png ),
:message => I18n.translate('campaigns.show.please_upload_valid_image')
-
+
has_attached_file :image,
:path => "#{MySociety::Config.get('FILE_DIRECTORY', ':rails_root/public/system')}/paperclip/:class/:attachment/:id/:style/:filename",
:url => "#{MySociety::Config.get('PAPERCLIP_URL_BASE', '/system/paperclip')}/:class/:attachment/:id/:style/:filename",
:default_url => "/images/paperclip_defaults/:class/:attachment/missing_:style.png",
:styles => { :default => "130x130#", :max => "600x600>", :list => "90x90#" }
-end
+
+ before_post_process :autorotate_paperclip_image
+
+ def autorotate_paperclip_image
+ uploaded_filename = image.queued_for_write[:original].path
+ # Ignore return values form autorotate_image - if it's not a JPEG
+ # or reorientation fails, the original file is left untouched:
+ autorotate_image(uploaded_filename)
+ true
+ end
+
+end
View
11 app/models/user.rb
@@ -48,6 +48,17 @@ class User < ActiveRecord::Base
:small_thumb => "40x40#",
:medium_thumb => "46x46#" }
+
+ before_post_process :autorotate_paperclip_image
+
+ def autorotate_paperclip_image
+ uploaded_filename = profile_photo.queued_for_write[:original].path
+ # Ignore return values form autorotate_image - if it's not a JPEG
+ # or reorientation fails, the original file is left untouched:
+ autorotate_image(uploaded_filename)
+ true
+ end
+
attr_accessor :ignore_blank_passwords,
:skip_email_uniqueness_validation,
:profile_photo_url,
2  commonlib
@@ -1 +1 @@
-Subproject commit 8e3c6fd31af53b3da8d31d9ac40b7310fac3af83
+Subproject commit b94e1b6af188b96fed88889fa557495f1d650a80
View
1  config/environment.rb
@@ -19,6 +19,7 @@
load "util.rb"
load "validate.rb"
load "voting_area.rb"
+load "autorotate_image.rb"
# Patch Rails::GemDependency to cope with older versions of rubygems
# Restores override removed in https://github.com/rails/rails/commit/c20a4d18e36a13b5eea3155beba36bb582c0cc87
View
1  config/packages
@@ -1,6 +1,7 @@
bsd-mailx
elinks
irb
+jhead
libapache2-mod-passenger
libopenssl-ruby1.8
libpgsql-ruby1.8
Please sign in to comment.
Something went wrong with that request. Please try again.