Recipe for resize and combine images in ruby

I had a simple need to combine two images together. I wanted to create many photos in the resized format of instagram (pixel ratio of 1080 by 566). But I didn't want to stretch or destroy the initial image ratio of the image. I wanted to resize the image to the max dimension possible, either at 1080 or 566 pixels and then put a custom background image on it. Of course a similar need could be an avatar common background or a watermark on top of the image.
So the code is the following:
A bit code explanation:
  • The method res resizes and then combines the images (the background and the passed one)
  • Then it loops through the dir
  • It detects the images
  • It saves the resized version inside the tmp folder
  • It saves the resized and combined version inside the resized folder
It is a bit hacky and fast way to do it. You can also have a similar result with the method resize_and_pad!. The only advantage of the above is the custom background image and you can combine more than one.
Max one mail per week.