Creating nice Square Thumbnails with CFIMAGE
I recently worked on a project that allowed users to upload images and I then needed to create different versions of the image... a 1200 x N, 430 x N, and 75x75... wait it has to be square!? Yeah that was my first thought, nah that won't work
What about stretching it?

Nope that looks like junk
What about white bars ALA Your DVD player?

Image is too small and they want the image to fill up the whole square.
hmm how about a nice sized-down-then-chopped image?

yeah that would look cool.
So how do we do it?
First we need to read the image from the upload and put it into a variable
-
<cffile action="upload" filefield="form.uploaded_image" destination="#expandpath(" nameconflict="makeunique">
-
-
<cfimage action="read" name="img1" source="#cffile.serverDirectory#/#cffile.serverfile#">
Next we want to create an image that is 1200xN and an image that is 430xN and write it to our image directory.
-
<!--- Create the full size image 1280 x N --->
-
-
<cfif img1.height GT img1.width>
-
<cfif img1.width GTE 1024>
-
<cfset ImageResize(img1,'1024','')>
-
</cfif>
-
<cfelseif img1.width GT img1.height>
-
<cfif img1.width GTE 1280>
-
<cfset ImageResize(img1, '1280','')>
-
</cfif>
-
</cfif>
-
-
<cfset ImageWrite(img1, "#expandpath("/images")#\img-#insertImage.newImageID#-fullxfull.jpg")>
-
-
-
-
-
<cfif img1.height GT img1.width>
-
<cfset ImageResize(img1,'430','')>
-
<cfelseif img1.width GT img1.height>
-
<cfset ImageResize(img1, '430','')>
-
</cfif>
-
-
<cfset ImageWrite(img1, "#expandpath("/images")#\img-#insertImage.newImageID#-430xN.jpg")>
The fun part comes next. We want to create the thumbnail and do all kinds of math to figure out how to crop. That is where the "FromX" and "FromY" variables comes in. It figures out where the upper left hand corner is then it walks its way in to 38 pixels and does a crop. This is how we now if it is portrait or landscape and where to crop it. After that we save the image and we are done!
-
<!--- Create the thumbnail 75 x 75 --->
-
-
<!--- resize image to 75 pixels tall if width is greater then height --->
-
<cfif img1.height GT img1.width>
-
-
<cfset ImageResize(img1,'75','')>
-
<cfset fromX = img1.Height / 2 - 38>
-
<cfset ImageCrop(img1,0,fromX,75,75)>
-
-
<!--- resize image to 75 pixels wide if height is greater then width --->
-
<cfelseif img1.width GT img1.height>
-
-
<cfset ImageResize(img1,'','75')>
-
<cfset fromY = img1.Width / 2 - 38>
-
<cfset ImageCrop(img1,fromY,0,75,75)>
-
-
<cfelse>
-
<cfset ImageResize(img1,'','75')>
-
<cfset ImageCrop(img1,0,0,75,75)>
-
</cfif>
-
-
<cfset ImageWrite(img1, "#expandpath("/images")#\img-#insertImage.newImageID#-75x75.jpg")>
Nice easy way to create cool looking square thumbnails.
Glyn on February 8th, 2008
Thank you for this example, i was having the same issue.