Since Ben Nadel was kind enough to help me out a few days ago on an unusual coding requirement and wrote a post about it as well, I thought I would write one myself and showcase an actual use-case scenario for it.

The real coding request that I had was to allow users to upload images through a CMS, assign some properties to them, and the CMS would create a gallery slider for the homepage. Each slide would show a small section of the previous one at the left hand side corner:

Even though ColdFusion comes with a plethora of native image functions, there isn’t anything that could achieve this (and please correct me if I am wrong). That’s where Ben’s example came really handy. Please read his post first to get an idea of how to “punch out” a section of a photo, and I will continue with the code to get the effect shown above for the slider.

There are two things we need to be very careful about in creating such effects:

  1. All our images need to be in PNG format.
    We could definitely start off with anything other than that (like JPG), but we need to convert them first before we can work on them. This is so that we can use PNG transparencies, which are needed to create the diagonal slicing effects.
  2. All images we work with need to have the same dimensions.
    Since we are going to overlay images on top of each other, it really helps if everything just fits within the same size. We can verify this beforehand by having a crop tool, or just use ColdFusion’s ImageResize() and ImageCrop() functions.

These are the steps to get this effect rocking:

  1. Read the 2 images that will be joined. Let’s refer to them as the “left-hand” and “right-hand” images.
  2. Create the image that will be used to punch through the left-hand slider image.
  3. Punch through the left-hand slider image to create a transparency for the image part we don’t want.
  4. Overlay the punched out image on top of the right-hand image.

And here’s the complete code below. Inline comments describe it more detail and point to the 4 steps outlined above.

Here’s another example of how you can use this method to create a nice joined image of two rugs:

One Response to Joining Images Diagonally using ColdFusion and Java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.