#assets_controller.rb
def update
## some stuff
Delayed::Job.enqueue(CropImageJob.new(@
asset.id))
end
#lib/crop_image_job.rb
class CropImageJob < Struct.new(:asset_id)
def perform
puts "Cropping image with id: #{asset_id}"
asset = Asset.find(asset_id)
tempfile = asset.image.process(:resize_and_crop, :width =>
40, :height=> 50, :gravity => 'ne').url
system "wget
http://localhost:3000#{tempfile} --delete-after"
asset.state = "ready"
asset.save!
puts "Image #{asset_id} successful resized and published"
end
def success(job)
puts ">>> Successful finished job: #{
job.id}"
end
def failure(job)
puts ">>> Job #{
job.id} failed to resize image"
end
end
This will generate the imagethumb the cache uses.
I would recommend to fire up the console and the worker with "rake
jobs:work" and test
your job with the console first. The asset.state is later used to
determine if the image
is ready for display. Use a default value of "not_ready" or something,
boolean would be suffice
here.