Skip to content

Instantly share code, notes, and snippets.

@VvanGemert
Last active December 17, 2015 09:19
Show Gist options
  • Save VvanGemert/5586175 to your computer and use it in GitHub Desktop.
Save VvanGemert/5586175 to your computer and use it in GitHub Desktop.
This script generates two simple daemons to process images with convert and uploads them to S3.
# Image processing daemons
# By VvanGemert
#
# Requirements:
# apt-get install imagemagick
# ruby 1.9.3 or higher
# gem install daemons
# gem install aws-s3
#
# Usage:
# ruby image_processing_daemon.rb start|stop|restart
require 'rubygems'
require 'daemons'
require 'fileutils'
require 'aws/s3'
## SETTINGS
PHOTO_PATH = "/uploaded/photos/path/"
RESULT_PATH = "/temporary/photos/results/path"
AWS::S3::Base.establish_connection!(
:access_key_id => 'XXXXX',
:secret_access_key => 'XXXXX'
)
BUCKET = "bucket.amazon.com"
BUCKET_PATH = "bucket/path"
CONVERSION = {
:thumb => "convert -size 132x132 :file -thumbnail 132x132^ -gravity center -extent 132x132 :fullpath_thumb.jpg",
:default => "convert -resize 800x600^ :file :fullpath.jpg",
:original => "convert :file :fullpath_original.jpg"
}
## SETTINGS
Daemons.run_proc('resizing.rb') do
loop do
images = Dir[PHOTO_PATH + "*/*"]
images.each do |file|
base = File.basename(file,File.extname(file))
Dir.mkdir(RESULT_PATH) unless File.directory?(RESULT_PATH)
full_path = RESULT_PATH + "/" + base
map = {':file' => file, ':fullpath' => full_path}
CONVERSION.each do |key,str|
map.each {|k,v| str.sub!(k,v)}
system(str)
end
FileUtils.rm(file)
end
sleep(2)
end
end
Daemons.run_proc('s3sync.rb') do
loop do
images = Dir[RESULT_PATH + "*/*"]
images.each do |file|
base = File.basename(file)
AWS::S3::S3Object.store(BUCKET_PATH, File.open(file).read, BUCKET, :access => :public_read)
FileUtils.rm(file)
end
sleep(2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment