Skip to content

Instantly share code, notes, and snippets.

@cflove
Created May 20, 2013 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cflove/5612969 to your computer and use it in GitHub Desktop.
Save cflove/5612969 to your computer and use it in GitHub Desktop.
Face Cropped Thumbnails with ColdFusion and jviolajones
<!---- ************************************************************* --->
<!--- Detect Faces --->
<!---- ************************************************************* --->
<cfset Detector = CreateObject('java', 'jviolajones.Detector',"#ExpandPath('jviolajones.jar')#,#ExpandPath('jdom.jar')#" ).init( ExpandPath('haarcascade_frontalface_alt2.xml'))>
<cfset image = ImageRead(ExpandPath('images/2.jpg'))>
<cfset results = Detector.getFaces( ImageGetBufferedImage(image) ,
JavaCast('float',2) ,
JavaCast('float',1.25) ,
JavaCast('float',.06) ,
JavaCast('int',3) ,
JavaCast('boolean',true) ) >
<!---- ************************************************************* --->
<!--- Frame all faces together --->
<!---- ************************************************************* --->
<cfif ArrayLen(results)>
<cfset border.x = image.GetWidth()>
<cfset border.y = image.GetHeight()>
<cfset border.width = 0>
<cfset border.height = 0>
<cfloop from="1" to="#ArrayLen(results)#" index="f">
<cfif results[f].x lt border.x>
<cfset border.x = results[f].x>
</cfif>
<cfif results[f].y lt border.y>
<cfset border.y = results[f].y>
</cfif>
<cfif (results[f].x + results[f].width) gt border.width>
<cfset border.width = results[f].x + results[f].width>
</cfif>
<cfif (results[f].y + results[f].height) gt border.height>
<cfset border.height = results[f].y + results[f].height>
</cfif>
</cfloop>
<cfset border.width = border.width-border.x>
<cfset border.height = border.height-border.y>
<!---- ************************************************************* --->
<!---- Make the face frame square and center --->
<!---- ************************************************************* --->
<cfif border.width gt border.height>
<cfset border.y = border.y - ((border.width-border.height)/2)>
<cfset border.height = border.width>
<cfelse>
<cfset border.x = border.x - ((border.height-border.width)/2)>
<cfset border.width = border.height>
</cfif>
<!---- ************************************************************* --->
<!---- Face detect can be tight. Add bit of padding (40%) --->
<!---- ************************************************************* --->
<cfset padding = Ceiling((border.width/100)*40)>
<cfset border.x = border.x - padding>
<cfset border.width = border.width + (padding*2)>
<cfset border.Y = border.Y - padding>
<cfset border.height = border.height + (padding*2)>
<!---- ************************************************************* --->
<!---- If the frame is biger than the picture, shrink it --->
<!---- ************************************************************* --->
<cfif border.x lt 0>
<cfset gap = border.x+1>
<cfset border.x = 1>
<cfset border.width = border.width+gap>
<cfset border.height = border.height+gap>
<cfset border.y = border.y-gap>
</cfif>
<cfif border.y lt 0>
<cfset gap = border.y+1>
<cfset border.y = 1>
<cfset border.width = border.width+(gap*2)>
<cfset border.height = border.height+(gap*2)>
<cfset border.x = border.x-gap>
</cfif>
<cfif (border.y+border.height) gt image.GetHeight()>
<cfset gap = (border.y+border.height)-image.GetHeight()>
<cfset border.x = border.x+gap>
<cfset border.y = border.y+gap>
<cfset border.width = border.width-(gap*2)>
<cfset border.height = border.height-(gap*2)>
</cfif>
<cfif (border.x+border.width) gt image.GetWidth()>
<cfset gap = (border.x+border.width)-image.GetWidth()>
<cfset border.x = border.x+gap>
<cfset border.y = border.y+gap>
<cfset border.width = border.width-(gap*2)>
<cfset border.height = border.height-(gap*2)>
</cfif>
<cfelse>
<!---- ************************************************************* --->
<!---- Images without faces --->
<!---- ************************************************************* --->
<cfif image.GetWidth() gt image.GetHeight()>
<cfset border.x = ( image.GetWidth()-image.GetHeight() ) /2>
<cfset border.y = 0>
<cfset border.width = image.GetHeight()>
<cfset border.height = image.GetHeight()>
<cfelse>
<cfset border.x = 0>
<cfset border.y = ( image.GetHeight()-image.GetWidth() ) /2>
<cfset border.width = image.GetWidth()>
<cfset border.height = image.GetWidth()>
</cfif>
</cfif>
<cfset ImageCrop(image, border.x, border.y, border.width, border.height)>
<cfset ImageResize(image,150,150)>
<cfset ImageWrite(image, ExpandPath('images/test.JPG'),1 )>
<img src="images/test.JPG" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment