Ok, so this is probably me having a "Duh" moment here, but I forgot that imageScaleToFit goes both ways. What do I mean by that? If you are using the code to create a thumbnail, like so:
<cfset imageScaleToFit(myImage,255,255,"highestQuality")>
and the image is less than 255 tall or wide than ColdFusion will increase the size of the image. As I said - duh. Luckily this is simple enough to solve:
<cfif myImage.width gt 255 or myImage.height gt 255>
<cfset imageScaleToFit(myImage,255,255,"highestQuality")>
</cfif>
You could also make a simple UDF out of this as well but not sure if it is worth the trouble.
Archived Comments
What about:
<cfset imageScaleToFit(myImage,min(255,myImage.width),min(255,myImage.height),"highestQuality")>
I've seen image resizing on CF sometimes take a long time. Therefore - if there is no need to resize, I wouldn't call it.
I guess I assumed that imageScaleToFit() would be smart enough not to resize the image and just return the original, if the size you specified was the size of the original -- though I can see why it wouldn't.
One more observation: if you resize a GIF with a transparency attribute other than opaque, you get unhappy results. I'm not even sure if this is really a bug, but more to do with the way GIFs work. In any case, I'm debating right now how to best trap that out in a CMS I'm building, and I'm leaning toward some kind of inelegant but practical trap out of this attribute that sends them back to Photoshop, etc. to resize the image there (on the theory that for most users, resizing is more readily accomplished than finding and altering some kind of obscure attribute) before uploading. Any thoughts, anyone?
James, if you cfdump an image like this, and look at the specs (color model and all that crap), is there a particular set of values that match the 'problem' gifs? If so - it would make it easier to trap.
Ray, I think for the .gifs it's not colormodel, but something like "transparency" that seems to be the deciding factor, but I'm only half-confident that I can reliably identify the problem .gifs through that match... resizing gifs even in Photoshop or similar programs can sometimes be a bit dodgy (including requiring an increase in the number of colors to help with the anti-aliasing and sharpening, etc., before bringing the color count back down to 256), so I am thinking that it might be wise to anticipate some cases where the resized image is not so good... especially as compared to the relatively reliable jpgs.
So what I may wind up doing for .gifs is showing the resized image and instructing them to resize it externally if the results look unwanted. Not elegant, but reasonably practical. And, this is a case where the image upload is safely staged prior to the image itself will be shown on the site, so the "bad" image won't be shown; they'll just replace it with their externally resized one before taking it live.
I hope this helps someone. I fought with resizing/scaling to fit gif images all day long. I finally came up with this workaround. Basically I create a jpg copy, resize that, write it back out to a gif then delete the gif. It seems to work alright. I tested it with a couple of gifs I had trouble with and it passed that test. About to run it against 20,000 files.
Here it is:
<cffunction name="createThumbnails" access="public" description="Creates small, medium, large thumbnails given a file" output="false">
<cfargument name="thumbSrcFile" required="true" type="string">
<!--- set base directory for thumbnails --->
<cfset var thumbBaseDir="#GetDirectoryFromPath(Arguments.thumbSrcFile)#">
<cfset var thumbSrcFileName="#GetFileFromPath(Arguments.thumbSrcFile)#">
<!--- <cflog text="#thumbSrcFile#"> --->
<cfif IsImageFile(Arguments.thumbSrcFile)>
<!--- If it's a gif file we have to deal with it so it can be resized' --->
<cfif Right(Arguments.thumbSrcFile,4) eq ".gif">
<cfimage action="convert" destination="#Arguments.thumbSrcFile#.jpg" source="#Arguments.thumbSrcFile#" overwrite="yes">
<cfimage source="#Arguments.thumbSrcFile#.jpg" name="thumbSrc">
<cfelse>
<cfimage source="#Arguments.thumbSrcFile#" name="thumbSrc">
</cfif>
<cfset largeThumb=ImageNew("#thumbSrc#")>
<cfset mediumThumb=ImageNew("#thumbSrc#")>
<cfset smallThumb=ImageNew("#thumbSrc#")>
<cftry>
<cfset ImageScaleToFit(largeThumb,Variables.largeThumbWidth,Variables.largeThumbHeight)>
<cfset ImageScaleToFit(mediumThumb,Variables.mediumThumbWidth,Variables.mediumThumbHeight)>
<cfset ImageScaleToFit(smallThumb,Variables.smallThumbWidth,Variables.smallThumbHeight)>
<cfimage action="write" source="#largeThumb#" destination="#thumbBaseDir#/tnlg/tnlg_#thumbSrcFileName#" overwrite="yes" quality="1">
<cfimage action="write" source="#mediumThumb#" destination="#thumbBaseDir#/tnmed/tnmed_#thumbSrcFileName#" overwrite="yes" quality="1">
<cfimage action="write" source="#smallThumb#" destination="#thumbBaseDir#/tnsm/tnsm_#thumbSrcFileName#" overwrite="yes" quality="1">
<!--- Clean up temp jpg file --->
<cfif Right(Arguments.thumbSrcFile,4) eq ".gif">
<cffile action="delete" file="#Arguments.thumbSrcFile#.jpg" >
</cfif>
<cfcatch type="any">
<cflog text="#Arguments.thumbSrcFile# Scale Error Catch Type: #cfcatch.Type#">
<cflog text="Catch Message: #cfcatch.Message#">
<cflog text="Catch Detail: #cfcatch.Detail#">
<cfinvoke method="createBlankThumbnails" argumentcollection="#Arguments#">
</cfcatch>
</cftry>
</cfif>
</cffunction>