Subsribe to our RSS

latest reactions

Montana Flynn
haha …
some1
Hi, Thank you for help me testing my sit …
metin2 yang
it is very nice …
Tjarko
I would'nt use cfhtmlhead for anything ; …
Sebastiaan
Hi Tjarko,Would you suggest injecting…

Use OpenDNS

mxna feeds

A Special ColdFusion Builder Event in Tampa, FLColdFusion Contract Job in Marbella, SpainColdFusion Job Opportunity in West London, United KingdomColdFusion Job Opportunity in Bethesda, MDColdFusion Job Opportunity in Springfield, MOColdFusion Job Opportunity in Seattle, WAColdFusion Builder - RefactoringColdFusion Job Opportunity in Raleigh, NCColdFusion Job Opportunity in Quincy, MAColdFusion Job Opportunity in Memphis, TNColdFusion Job Opportunity in San Antonio, TXUsing Base64 Canvas Data In jQuery To Create ColdFusion ImagesMy move from using cfhttp and rssatom.cfc to cffeed.Infinite entities, cfgrid and one cfc to handle the dataJob: ColdFusion Developer - Marbella, Spain

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2010 mximize.com.
All right reserved.

MXNA webfeed

Visit Carlos GallupaPowered by ColdFusion MX

Using ColdFusion image functions to resize an image into a square thumbnail

After trying several times to resize and crop the image to fit nicely into a square I stumbled upon the function ImageScaleToFit() and that was exactly what I needed for my problem. I want to fit an image into a square thumbnail, but with the imageCrop or Resize you can't get the image to fit. :-(

What I did is the following... after I found the ImageScaleToFit function I also found the ImagePaste function which will paste 1 image into the other. After thinking about this I came with the solution to first scale the image to the correct size, then make a new blank image with the imageNew() function and paste the 2 together.

With the ImagePaste() function you also need to calculate the x and y value where the image should be pasted upon the other image. See the code below for an example

The original image:


<!---Read the image into an object --->
<cfimage action="read" name="myImage" source="65.jpg">

<!--- Set the square size of the thumb --->
<cfset sq_size = 200>

<!--- Write the result to a file. --->
<cfset ImageSetAntialiasing(myImage,"on")>
<cfset ImageScaleToFit(myImage,sq_size,sq_size)>

<!--- Calculate the x and y position to paste the image --->
<cfif myImage.width GTE myImage.height>
    <cfset x = 0>
    <cfset y = ceiling((myImage.width - myImage.height)/2)>
<cfelse>
    <cfset x = ceiling((myImage.height - myImage.width)/2)>
    <cfset y = 0>
</cfif>

<cfset newimg = ImageNew("",sq_size, sq_size, "rgb", "FF0000")>

<cfset ImagePaste(newimg, myImage, x, y)>

<cfimage action="WRITETOBROWSER" source="#newimg#" format="JPG">


The result image looks like this. Picture taken from www.wickedweasel.com

7982 viewed | 3 opinion(s)  | del.icio.us | Digg it | Tjarko @ 13/12/07 12:29 cet


Reactions:

Sebastiaan wrote.... (site)

Hi Tjarko, we incorporate the same thing in our webshop. A user can upload an image and we automagically scale it down to a thumbnail using CFimage. Only works in CF8 of course ;-) The beauty is that the user only needs to upload the one image he/she needs and we than scale it to the two set sizes (full and thumb). No more hassle in Photoshop ;-)

17 December 2007 9:41 cet  

wrote....

05 May 2008 8:54 cet  

toujours wrote....

this has really helped me a lot. thank you very much for posting this, it is very kind of you.

30 April 2009 20:06 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



The answer to the ultimate question is?? (42 ;-))




URL en mail addresses are translated for you... life sometimes is that simple!!