Created
October 7, 2013 12:06
-
-
Save keighl/6866749 to your computer and use it in GitHub Desktop.
Example image manipulation worker from S3 using rmagick and aws-sdk-ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'RMagick' | |
class PhotoProcessor | |
def self.process(entry_id) | |
entry = Entry.find entry_id | |
raise "The photo for this entry has already been processed" if entry.image_processed? | |
AWS.config(access_key_id: AWS_ACCESS_KEY_ID, secret_access_key: AWS_SECRET_ACCESS_KEY) | |
s3 = AWS::S3.new | |
bucket = s3.buckets[S3_BUCKET] | |
filename = entry.image.gsub("photos/", "").gsub("/", "-") | |
local_orig = "tmp/#{filename}" | |
local_large = "tmp/large_#{filename}" | |
local_thumb = "tmp/thumb_#{filename}" | |
photo = bucket.objects[entry.image] | |
photo_large = bucket.objects[entry.image_large] | |
photo_thumb = bucket.objects[entry.image_thumb] | |
File.open(local_orig, 'wb') do |file| | |
photo.read do |chunk| | |
file.write(chunk) | |
end | |
end | |
photo.write(Pathname.new(local_orig)) | |
img = Magick::Image.read(local_orig).first | |
img = img.auto_orient | |
img.resize_to_fit!(360, 360).write(local_large) | |
photo_large.write(Pathname.new(local_large)) | |
img.resize_to_fill!(200, 200).write(local_thumb) | |
photo_thumb.write(Pathname.new(local_thumb)) | |
File.delete(local_orig, local_large, local_thumb) | |
entry.update_attribute :image_processed, true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
where is the "Entry" class from?