Skip to content

Instantly share code, notes, and snippets.

@keighl
Created October 7, 2013 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keighl/6866749 to your computer and use it in GitHub Desktop.
Save keighl/6866749 to your computer and use it in GitHub Desktop.
Example image manipulation worker from S3 using rmagick and aws-sdk-ruby
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
@mulaiko
Copy link

mulaiko commented Aug 6, 2014

Hi there,
where is the "Entry" class from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment