Commit 12ef8499

mo khan <mo@mokhan.ca>
2014-06-05 04:27:40
use mini magick to resize original to large image and upload to s3.
1 parent 62379c2
Changed files (3)
app
services
application
app/services/application/handlers/process_photo.rb
@@ -26,5 +26,46 @@ class ProcessPhoto
   def upload_original(file, photo)
     key = "uploads/photo/image/#{photo.id}/#{File.basename(file)}"
     @blob_storage.upload(key, file)
+    upload_large_version(file, photo)
+  end
+
+  def upload_large_version(file, photo)
+    key = "uploads/photo/image/#{photo.id}/large_#{File.basename(file)}"
+    Image.new(file).resize_to_fit(570, 630)
+    @blob_storage.upload(key, file)
+  end
+
+end
+
+class Image
+  attr_reader :current_path
+
+  def initialize(current_path)
+    @current_path = current_path
+  end
+
+  def resize_to_fit(width, height)
+    manipulate! do |img|
+      img.resize "#{width}x#{height}"
+      img = yield(img) if block_given?
+      img
+    end
+  end
+
+  private
+
+  def manipulate!
+    image = ::MiniMagick::Image.open(current_path)
+    begin
+      image = yield(image)
+      image.write(current_path)
+      image.run_command("identify", current_path)
+    ensure
+      image.destroy!
+    end
+  rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
+    default = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :locale => :en)
+    message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :default => default)
+    raise CarrierWave::ProcessingError, message
   end
 end
Gemfile
@@ -15,6 +15,7 @@ gem 'devise', '~> 3.0.0'
 gem 'jquery-ui-rails', '~> 4.0.0'
 gem 'carrierwave', '~> 0.9.0'
 gem 'rmagick', '~> 2.13.0'
+gem 'mini_magick'
 gem "kaminari", "~> 0.15.0"
 gem 'bootstrap-sass', '~> 2.3.0'
 gem 'acts-as-taggable-on', '~> 2.4.1'
Gemfile.lock
@@ -182,6 +182,8 @@ GEM
     memoizable (0.4.2)
       thread_safe (~> 0.3, >= 0.3.1)
     mime-types (1.25.1)
+    mini_magick (3.7.0)
+      subexec (~> 0.2.1)
     mini_portile (0.5.3)
     minitest (4.7.5)
     multi_json (1.10.0)
@@ -275,6 +277,7 @@ GEM
       activesupport (>= 3.0)
       sprockets (~> 2.8)
     sqlite3 (1.3.9)
+    subexec (0.2.3)
     teaspoon (0.8.0)
       railties (>= 3.2.5, < 5)
     thor (0.19.1)
@@ -354,6 +357,7 @@ DEPENDENCIES
   kaminari (~> 0.15.0)
   lol_dba
   mime-types
+  mini_magick
   newrelic_rpm
   pg
   poltergeist