-
Notifications
You must be signed in to change notification settings - Fork 7
Performing Multiple Operations on an Image
Many times, you want to do multiple operations on an image, such as cropping it to multiple different sizes, or perhaps even run a filter or watermark and then crop that image. For example, lets watermark an image and the crop it to multiple sub-sizes.
job = Blitline::Job.new("http://www.google.com/logos/2011/yokoyama11-hp.jpg") # My source file to manipulate
job.application_id = "ACCOUNT_ID" # This ACCOUNT_ID needs to be your account ID from Blitline which you got from Blitline.com above
watermark_function = job.add_function("watermark", { :text=>"Sample Text" }) # Add a watermark
crop_large_function = watermark_function.add_function("resize_to_fit", { :width => 300, :height => 150 })
crop_large_function.add_save("my_large_thumbnail")
crop_small_function = watermark_function.add_function("resize_to_fit", { :width => 100, :height => 50})
crop_small_function.add_save("my_small_thumbnail")
blitline_service = Blitline.new
blitline_service.jobs << job # Push job into service
blitline_service.post_jobs
Viola. The output of the post_jobs method will return json identifying where the images will be outputted once they have completed.
A Blitline job always has a source image, so when we create the job we have to identify what the source is:
job = Blitline::Job.new("http://www.google.com/logos/2011/yokoyama11-hp.jpg")
An Application ID identifies who is submitting this job (you!):
job.application_id = "ACCOUNT_ID"
To modify the image, we add functions to a job. These functions perform some action (eg. watermark, blur, crop, etc...):
watermark_function = job.add_function("watermark", { :text=>"Sample Text" })
Each function can have other subfunctions (which identify further operations to perform) or also save actions which identify that we want to output an image from this function. In the example above, we don't need to output the original image with the watermark, we only want to output the thumbnails, so we won't add a save to the watermark_function. We will go ahead and add the subfunctions that do the thumbnailing for us:
crop_large_function = watermark_function.add_function("resize_to_fit", { :width => 300, :height => 150 })
The crop_large_function takes the output of the watermark_function and performs a "resize_to_fit" function on it. We then want to output the results of the crop_large_function so we'll add a save to it:
crop_large_function.add_save("my_large_thumbnail")
The crop_small_function takes the output of the watermark_function and performs another "resize_to_fit" to make an even smaller thumbnail:
crop_small_function = watermark_function.add_function("resize_to_fit", { :width => 100, :height => 50})
We then want to output the results of the crop_small_function too, so we'll add a save to it:
crop_small_function.add_save("my_small_thumbnail")
Now that we have the job defined, we simply create an instance of the blitline_service, add the job to it's collection, and submit the jobs via the post_jobs method:
blitline_service = Blitline.new
blitline_service.jobs << job # Push job into service
blitline_service.post_jobs