Thursday, 28 April 2016

Auto Image rotation functionality using ColdFusion.

Generally we use our mobile phones to take picture and then send it to computer for various uses. We can take a picture from mobile with any angle we want then the phone has the ability to preview the image with proper orientation. Now the problem is when we have taken a picture with up side down and send the same picture to computer, it will preview the images with upside down as the system doesn't change the orientation automatically. Now the real issue is, we have couple of images and we want to create a profile picture for each users by manipulating the same images that we have. We can't use a up side down image as a profile picture. In order to resolve this issue we have the mechanism in ColdFusion to read the image and we can find if the image is proper oriented or not.

Let's take a look to the below code:

Set an image path to a varibale.

<cfset VARIABLES.imageName = "IMG_111.JPG">

Read the image property.
   
<cfset VARIABLES.theImage = ImageRead(VARIABLES.imageName)>
Then get the orientation of that image.
<cfset VARIABLES.orientation  = ImageGetEXIFTag(VARIABLES.theImage, 'orientation')>

Use ImageGetEXIFMetadata and then check if the structKeyExists.

<cfif (NOT isNull(orientation))>   
    <!---Find the rotation degree from below string if needed--->
    <cfset VAR hasRotate = findNoCase('rotate',orientation)>       
    <cfif hasRotate>       
        <!---strip out all text in the orientation value to get the degree of orientation--->
        <cfset rotateValue =reReplace(orientation,'[^0-9]','','all')>       
        <!---Rotate and write the image if needed for the particular image if needed.--->
        <cfimage   
            action = "rotate"
            angle = "#rotateValue#"
            source = "#
VARIABLES.imgPath#"  
            destination = "#
VARIABLES.imgPath#"
            isBase64= "no"
            name = "cfimage"
            overwrite = "yes">   
    </cfif>
</cfif>  
 

Now the above snippet will query for the given image, check if the orientation is right or not, if not, then it will proper oriented.
Now try to show the same image on the UI and you can find the proper oriented image.

We can also get below information form an image by reading the EXIF data of an image:

- The date/time the pic was shoot
- Image height
- Image weight
- ISO speed rating
- Make and model of the camera
- X resolution
- Y resolution
- GPS Latitude
- GPS Latitude Ref
- GPS Longitude
- GPS Longitude Ref etc.

 

No comments:

Post a Comment