CFMX 8 ImageResize
New to CFMX 8 is a slew of 50 or so new functions for manipulating, creating, and drawing images natively including imageResize(). Here is a brief overview of the Read, Resize, Save process.
ImageRead()
This function will read a file from the server hard drive OR URL! It returns a ColdFusion Image - this is a new ColdFusion data type.
Here is how it works:
From a URL:
<cfset thisImage = ImageRead("http://www.myDomain.com/images/logo.jpg")>
From a Path:
<cfset thisImage = ImageRead("c:\images\logo.jpg")>
The variable 'thisImage' contains the image file that you requested. You can now use other Image Functions with thisImage to manipulate the image.
ImageResize()
This function will resize the file object returned by ImageRead() discussed above. It returns nothing.
Required Arguments:
Name - The ColdFusion Image to manipulate ('thisImage' from our example above).
Width - The new width of the image (Pixel or Percentage) (leave blank to keep aspect ratio with height)
Height - The new height of the image (Pixel or Percentage) (leave blank to keep aspect ratio with width)
Optional Arguments:
Interpolation - There are about 17 methods of re-sampling the image. The default is highestQuality
blurFactor - Adds blur to the image during resize (1-10)
How it works:
To resize the width and keep the aspect ratio the same (let the height automatically adjust) default quality, and no blur.
<cfset imageResize(thisImage,200,"")>
To resize height and width, at a lower quality (faster operation) and a slight blur.
<cfset imageResize(thisImage,200,300,"highPerformance",2)>
Also see ImageScaleToFit() for specifying a height and width for the Image to be scaled to fit within.
ImageWrite()
Let's save an image now. ImageWrite() takes three arguments:
Name (required) - the ColdFusion Image variable ('thisImage' from our example)
Destination (optional) - Absolute or Relative path to save file. Required if the original source image was created rather than read from the drive (ex: imageNew() created a new image) If the image came from a function like ImageRead(), then the default value for this argument is the original filename with no path information.
Quality (optional) - for file extensions of JPG and JPEG, this sets the image quality. Use a value between 0 and 1. Default is .75
How to use it:
Save best quality to a new path and filename.
<cfset imageWrite(thisImage,"c:\images\myNewImage.jpg",1)>
Save default quality to new path keeping the original filename.
<cfset imageWrite(thisImage,"c:\images\")>
Save to current path of executing script with default quality.
<cfset imageWrite(thisImage)>
So, there it is. Native image resizing in CFMX 8. A truly great thing in deed.
____________