ColdFusion: Overlay an Image

I have not had a chance to play around with every piece of functionality with the image functions that were released in ColdFusion 8.  Due to a problem yesterday, I had the chance to play around with a couple of them last evening.  The problem was I needed all the images on the site to be the same size.  These images are uploaded by the user, so not all of them had the same beginning dimensions.  Just resizing them with a height and width would have caused a great number of them to become distorted.  So we then decided to start with a base image of a fixed size, resize the uploaded image to a standard width, then overlay that image with the base image.  So in the end we have images that are all the exact same size, with our uploaded image looking nice and centered within the base image.

So the first attempt I looked into using the overlay function.

<code lang="cfm[lines]"><br></br><cfimage name="myImage" source="images/base.jpg"></cfimage>

This doesn’t work too bad, but notice that the overlay function will always place the image over your second image aligned to the top.  The function has no options for centering or telling it where you want to place the image.  A coworker then came across the ImagePaste function, which you can tell it where to place the image using x and y coordinates.  With a little math you can figure out the y coordinate to center the image.

<code lang="cfm[lines]"><br></br><cfimage name="myImage" source="images/base.jpg"></cfimage>


You can view a demo of the code here.  It shows you the output of each image.  Do note that for this example I used a gray background for the base image, just for a visual reference of the image size.  The base image I really used was white and was 200×200 in size, so the image appears fixed in that space.  Also, a lot of the code here came straight from the example code in the documentation, with a few changes and additions.

Comments

comments powered by Disqus