Orientation Issue with iOS Image Upload (Part-2)

Just recently in the past a blog published on image orientation issue with iOS image upload, after that  we again experienced similar type of issue once previously image orientation issue already fixed for lucee CFML.

 

Again, Carried a few test methods to reach up to cause of the strange issue, finally found the cause and solution to fix it.

 

Usually, photos taken on apple device adds the orientation rules to EXIF metadata with captured images, so based on metadata information view photos rotation can be corrected.

 

Generally, we get the value like “Left side, bottom (Rotate 270 CW)” as orientation and using regular expression Getting rotation angel value and based on that rotate image to make it correct.
 

 

Below is the case when iOS user try to upload an image using different iPhone version then received different orientation value like “3”,”6” or “8”.
 

 

We found below page that will explain meaning of this “3”,”6” or “8” orientation value.

http://sylvana.net/jpegcrop/exif_orientation.html

 

Information on above page helped to understand orientation value and its meaning and update the code logic with this new solution to make it work perfectly.
 

Error when loading gists from https://gist.github.com/.<cfset var oImage1 = ImageNew(imgFilePath)>

<cfset var oImage2 = ImageNew(ImageGetBufferedImage(oImage1))>

<cfset var imgEXIFinfo = ImageGetEXIFMetadata(oImage2)>

<cfif isStruct(imgEXIFinfo) AND structKeyExists(imgEXIFinfo,"Orientation")>
    <cfset var angleValue = "">
    <cfset var angleRegEx = "\(Rotate ([\d]+)">
    <cfset var arrMatched = REmatchNoCase(angleRegEx,imgEXIFinfo.Orientation)>
    <cfif arrayLen(arrMatched) eq 1>
        <cfset angleValue = reReplaceNoCase(arrMatched[1],angleRegEx,"\1")>
    </cfif>
    <cfif isNumeric(angleValue)>
        <cfset imageRotate(name = oImage2, x = 0, y = 0, angle = angleValue, interpolation = "bicubic")>
        <cfset imageWrite(oImage2,imgEXIFinfo.source,1)>
    <cfelseif listFind("3,6,8",imgEXIFinfo.Orientation)>
        <cfset var structAngle = {
            "3": "180",
            "6": "90",
            "8": "270"
        }>
        <cfset imageRotate(name = oImage2, x = 0, y = 0, angle = structAngle[imgEXIFinfo.Orientation], interpolation = "bicubic")>
        <cfset imageWrite(oImage2,imgEXIFinfo.source,1)>
    </cfif>
</cfif>