Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 981

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Working with Documents, Charts, and Reports
Generating thumbnail images
The following example shows how to create a form for uploading images. A visitor to the site can use the form to
upload an image file and generate a thumbnail image from it. You use ColdFusion image operations to perform the
following tasks:
• Verify that the uploaded file is a valid image.
• Ensure that the height or the width of the image does not exceed 800 pixels.
• If the image is valid and within the size limits, generate a thumbnail version of the source image and save it to a file.
Enter the following code on the form page:
<!--- This code creates a form with one field where the user enters the image file to upload.
--->
<cfform action="makeThumbnail.cfm" method="post" enctype="multipart/form-data">
Please upload an image: <cfinput type="file" name="image">
<cfinput type="submit" value="Send Image" name="Submit">
</cfform>
Enter the following code on the action page:
<cfset thisDir = expandPath(".")>
<!--- Determine whether the form is uploaded with the image. --->
<cfif structKeyExists(form,"image") and len(trim(form.image))>
<!--- Use the cffile tag to upload the image file. --->
<cffile action="upload" fileField="image" destination="#thisDir#" result="fileUpload"
nameconflict="overwrite">
<!--- Determine whether the image file is saved. --->
<cfif fileUpload.fileWasSaved>
<!--- Determine whether the saved file is a valid image file. --->
<cfif IsImageFile("#fileUpload.serverfile#")>
<!--- Read the image file into a variable called myImage. --->
<cfimage action="read" source="#fileUpload.serverfile#" name="myImage">
<!--- Determine whether the image file exceeds the size limits. --->
<cfif ImageGetHeight(myImage) gt 800 or ImageGetWidth(myImage) gt 800>
<!--- If the file is too large, delete it from the server. --->
<cffile action="delete"
file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">
<cfoutput>
<p>
The image you uploaded was too large. It must be less than 800 pixels wide
and 800 pixels high. Your image was #imageGetWidth(myImage)# pixels wide
and #imageGetHeight(myImage)# pixels high.
</p>
</cfoutput>
<!--- If the image is valid and does not exceed the size limits,
create a thumbnail image from the source file that is 75-pixels
square, while maintaining the aspect ratio of the source image.
Use the bilinear interpolation method to improve performance.
--->
<cfelse>
<cfset ImageScaleToFit(myImage,75,75,"bilinear")>
<!--- Specify the new filename as the source filename with
"_thumbnail" appended to it. --->
<cfset newImageName = fileUpload.serverDirectory & "/" &
fileUpload.serverFilename & "_thumbnail." &
fileUpload.serverFileExt>
Last updated 1/20/2012
976

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents