ColdFusion: Overlay an Image
| Posted in ColdFusion | Posted on 10-02-2009
0
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.
| ColdFusion | | copy code | | ? |
| 01 | <!--- read in the base image ---> |
| 02 | <cfimage source="images/base.jpg" name="myImage"> |
| 03 | |
| 04 | <!--- turn on antialiasing to improve image quality. ---> |
| 05 | <cfset ImageSetAntialiasing(myImage,"on")> |
| 06 | |
| 07 | <!--- load in the client supplied image ---> |
| 08 | <cfimage source="images/harvick.jpg" name="topImage" action="resize" width="200" height=""> |
| 09 | |
| 10 | <!--- Overlay the top image on the background image. ---> |
| 11 | <cfset ImageOverlay(myImage,topImage)> |
| 12 | |
| 13 | <!--- Display the combined image in a browser. ---> |
| 14 | <cfimage source="#myImage#" action="writeToBrowser"> |
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.
| ColdFusion | | copy code | | ? |
| 01 | <!--- read in the base image ---> |
| 02 | <cfimage source="images/base.jpg" name="myImage"> |
| 03 | |
| 04 | <!--- load in the client supplied image and resize ---> |
| 05 | <cfimage source="images/harvick.jpg" name="topImage" action="resize" width="200" height=""> |
| 06 | |
| 07 | <!--- get dimensions ---> |
| 08 | <cfset info=ImageInfo(topImage)> |
| 09 | |
| 10 | <!--- Copy a rectangular region of myImage1. ---> |
| 11 | <cfset resImage = ImageCopy(topImage,1,1,info.width-1,info.height-1)> |
| 12 | |
| 13 | <!--- Paste the rectangular area over myImage2. ---> |
| 14 | <cfset yCoord = Ceiling((200-info.height)/2)> |
| 15 | <cfset ImagePaste(myImage,resImage,1,yCoord)> |
| 16 | |
| 17 | <cfimage source="#myImage#" action="writeToBrowser"> |
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.



















