Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created July 31, 2015 17:30
Show Gist options
  • Save hayduke19us/ac735454f00aae8ee58a to your computer and use it in GitHub Desktop.
Save hayduke19us/ac735454f00aae8ee58a to your computer and use it in GitHub Desktop.
# aws-sdk
# aws-sdk-resources
# rmagick
class ResizeImages
attr_reader :all_image_names, :s3
attr_accessor :width, :height, :image_file_name
def initialize args
@image_file_name = args.fetch(:image_file_name, nil)
@height = args.fetch(:height, 'normal')
@width = args.fetch(:width, 'normal')
@all_image_names = ::Image.select 'asset_file_name'
@s3 = S3Client.new
set_constraints
end
# The height and width can be given in string format
# or numeric. The string format takes just the arg height: 'constraint'
# or width: 'constraint' with the same result
def set_constraints
case self.height || self.width
when 'normal'
@height, @width = 930, 350
when 'medium'
@height, @width = 500, 250
when 'thumb'
@height, @width = 100, 100
end
end
def all prefix='thumb'
self.all_image_names.each do |name|
unless self.s3.has_thumbnail?(name.asset_file_name)
image = self.to_magick_object name.asset_file_name
resized_image = self.resize image
self.s3.put_image name.asset_file_name, prefix, resized_image
else
s3.found_image_message
end
end
end
def single prefix='thumb'
unless self.s3.has_thumbnail?(self.image_file_name)
image = self.to_magick_object self.image_file_name
resized_image = self.resize image
self.s3.put_image self.image_file_name, prefix, resized_image
else
s3.found_image_message
end
end
def to_magick_object image_name
# TODO figure out the IO errors for reading the file
image = self.s3.get_image(image_name, 'normal')
Magick::Image.read(image.read).first
end
def resize! magick_image
magick_image.resize self.width, self.height
end
end
class S3Client
attr_reader :id, :secret, :creds, :client, :bucket_name, :resource, :bucket
def initialize bucket_name='field-journal'
@bucket_name = bucket_name
@id = ENV['aws_access_key_id']
@secret = ENV['aws_secret_access_key']
@creds = Aws::Credentials.new(@id, @secret)
@client = Aws::S3::Client.new(region: 'us-west-2', credentials: @creds)
@resource = Aws::S3::Resource.new(region: 'us-west-2', credentials: @creds)
@bucket = @resource.bucket(@bucket_name)
end
def object_exists? key, prefix
self.bucket.object(add_prefix(key, prefix)).exists?
end
def has_thumbnail? key
object_exists? key, 'thumb'
end
def get_image key, prefix
if object_exists? key, prefix
self.client.get_object(bucket: self.bucket_name, key: add_prefix(key, prefix)).body
else
raise "Unable to find #{add_prefix(key, prefix)}".colorize(:red)
end
end
def put_image key, prefix, body
self.client.put_object(bucket: self.bucket_name,
key: add_prefix(key, prefix),
body: body)
created_image_message
rescue
puts "Something went horrible wrong contact Jonan, or we could not create an image"
end
def found_image_message
puts "A thumbnail has been found for this image at:".colorize(:green)
puts "http://s3-us-west-2.amazonaws.com/field-journal/uploads/thumb/#{name}"
end
def created_image_message height, width, image_name
puts "you have successfully created an image".colorize(:green)
puts "height: #{height}"
puts "width: #{width}"
puts "image_name: #{image_name}"
end
def add_prefix name, prefix
"uploads/#{prefix}/#{name}"
end
end
desc 'resize all images into thumbnails if the thumbnail key does not exist'
namespace :images do
task 'resize:all' => :environment do
ResizeImages.new(thumb: 'thumb').all
end
end
# Takes an argument as rake images:resize:single image_name='name'
desc "resize a single image with the arg imge_name='file_name'"
namespace :images do
task 'resize:single' => :environment do
resize = ResizeImages.new(image_file_name: ENV["image_name"], height: 'thumb')
resize.single
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment