I put this together a couple years ago for your basic upload, resize, create
thumb, (and crop) process using imageCR.
It takes care of it all, you just tell it the name of the file field, the
paths and anything else it needs to know. It looks all jumbled up pasted
here but hopefully it will come through formatted fine.
Wow... I can't believe I'm showing anybody this code... I'll update this
thing soon... to at LEAST return some kind of structured info instead of
some random variables HAH!
<cfsetting enablecfoutputonly="Yes" showdebugoutput="No">
<!---
Tag Name : ImageCRTag.cfm
Author : Bobby Hartsfield | [EMAIL PROTECTED]
Date : 8-15-2004
Descritption : This custom tag was written to interface with the ImageCR3
tag from Efflare (http://www.efflare.com)
and is useless without it. Basically it was
written to make my life and the lives of the guys I work
with much easier. It is the upload, resize,
rename, thumb
creation, and move code for an image
management tool all wrapped up in a nice neat package.
Example : <cf_ImageCRTag FileField = "ImageFile"
\\ REQUIRED: **NO SCOPE OF "Form."** The name of the "FILE" field used to
upload the image file
TempPath
= "C:\INETPUB\WWWROOT\SITE\IMAGES\TEMP" \\ REQUIRED: The temporary path to
upload the image to for further processing
FinalPath
= "C:\INETPUB\WWWROOT\SITE\IMAGES" \\ REQUIRED: The final
destination of the processed image(s)
CreateThumb =
"Yes" \\
OPTIONAL: DEFAULT="No" : Rather or not to create a thumbnail of the image
ThumbPrefix =
"thumb_" \\
OPTIONAL: DEFAULT="th_" : Prefix to add to the beginning of the image name
for the name of a thumbnail
ThumbSize
= "100" \\
OPTIONAL: REQUIRED IF CREATETHUMB is "yes" Maximum width of processed
thumbnail image. Proprtions will be kept
ImageTypes
= "image/gif,image/jpg" \\ OPTIONAL:
DEFAULT="image/gif,image/pjpeg,image/jpg,image/jpeg"
NewImageName =
"MyNewImageName" \\ OPTIONAL:
**NO EXTENSION** If left out, the original name will be stripped of spaces
and used as the new file name
MaxSize
= "300" \\
REQUIRED: The maximum width of the processed image. Proportions will be kept
CropIt
= "Thumb" \\
OPTIONAL: If specified, the image will be cropped, Valid values are "thumb"
and "Large" (which image to crop)
Anchor
= "center" \\
OPTIONAL: REQUIRED IF CROPPING: Valid values are
none|center|northwest|north|northeast|west|east|southwest|south|southeast
ReCrop
= "100x100" \\
OPTIONAL: REQUIRED IF CROPPING: Check your ImageCR3 documentation to see
valid values
/>
--->
<cfparam name="ATTRIBUTES.FileField" default="">
<cfparam name="ATTRIBUTES.TempPath" default="">
<cfparam name="ATTRIBUTES.FinalPath" default="">
<cfparam name="ATTRIBUTES.CreateThumb" default="no">
<cfparam name="ATTRIBUTES.ThumbPrefix" default="th_">
<cfparam name="ATTRIBUTES.ThumbSize" default="no">
<cfparam name="ATTRIBUTES.ImageTypes"
default="image/gif,image/pjpeg,image/jpg,image/jpeg">
<cfparam name="ATTRIBUTES.NewImageName" default="">
<cfparam name="ATTRIBUTES.MaxSize" default="">
<cfparam name="ATTRIBUTES.CropIt" default="">
<cfparam name="ATTRIBUTES.Anchor" default="center">
<cfparam name="ATTRIBUTES.ReCrop" default="0x0">
<cfif VAL(ATTRIBUTES.MaxSize) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>MAXSIZE</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>ImageCRTag</b>''.">
<cfabort>
</cfif>
<cfif YESNOFORMAT(ATTRIBUTES.CreateThumb) is "yes" and
VAL(ATTRIBUTES.ThumbSize) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>THUMBSIZE</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>ImageCRTag</b>'' WHEN <b>CREATETHUMB</b> IS ''Yes''.">
<cfabort>
</cfif>
<cfif Len(Trim(ATTRIBUTES.TempPath)) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>TEMPPATH</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>IMCRTAG</b>''.">
<cfabort>
</cfif>
<cfif Len(Trim(ATTRIBUTES.FinalPath)) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>FINALPATH</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>IMCRTAG</b>''.">
<cfabort>
</cfif>
<cfif Len(Trim(ATTRIBUTES.FileField)) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>FILEFIELD</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>IMCRTAG</b>''.">
<cfabort>
</cfif>
<cfif Len(Trim(ATTRIBUTES.ImageTypes)) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>IMAGETYPS</b>'' IS REQUIRED IN
THE CUSTOM TAG ''<b>IMCRTAG</b>''. YOU MAY LEAVE THIS ATTRIBUTE BLANK AND
ACCEPT THE DEFAULT FILE TYPES OF:
''image/gif,image/pjpeg,image/jpg,image/jpeg''.">
<cfabort>
</cfif>
<cfif YESNOFORMAT(ATTRIBUTES.CreateThumb) is "yes" and
LEN(TRIM(ATTRIBUTES.ThumbPrefix)) is 0>
<cfthrow message="THE ATTRIBUTE ''<b>THUMBPREFIX</b>'' IS OPTIONAL
IN THE CUSTOM TAG ''<b>ImageCRTag</b>''. IF SPECIFIED, IT MUST NOT BE EMPTY.
THE DEFAULT IS ''th_''.">
<cfabort>
</cfif>
<cfif ATTRIBUTES.CropIt NEQ "" and (ATTRIBUTES.CropIt NEQ "Thumb" AND
ATTRIBUTES.CropIt NEQ "Large")>
<cfif YESNOFORMAT(ATTRIBUTES.CreateThumb) is "no" and
ATTRIBUTES.CropIT is "thumb">
<cfthrow message="YOU MUST SPECIFY THE CORRECT THUMBNAIL ATTRIBUTES
IN ORDER TO CROP THE THUMBNAIL.
''CREATETHUMNAIL=""#ATTRIBUTES.CREATETHUMB#""">
<cfelse>
<cfthrow message="VALID VALUES OF THE ATTRIBUTE ''<b>CROPIT</b>''
ARE ''THUMB'' AND ''LARGE''. MEAINGING EITHER CROP THE THUMBNAIL OR THE
LARGE IMAGE.">
<cfabort>
</cfif>
</cfif>
<cfset ValidAnchors =
"none|center|northwest|north|northeast|west|east|southwest|south|southeast">
<cfif ATTRIBUTES.Cropit NEQ "" and NOT ListFindNoCase(ValidAnchors,
ATTRIBUTES.ANCHOR, "|")>
<cfthrow message = "VALID VALUES FOR THE ATTRIBUTE ""ANCHOR""
ARE:<br>none<br>center<br>northwest<br>north<br>northeast<br>west<br>east<br
>southwest<br>south<br>southeast">
<cfabort>
</cfif>
<cfif ATTRIBUTES.Cropit NEQ "" and NOT ListFindNoCase(ValidAnchors,
ATTRIBUTES.ANCHOR, "|")>
<cfif NOT REFindNoCase("^[0-9]*x[0-9-]+[+0-9]", ATTRIBUTES.Anchor)>
<cfthrow message="YOU MUST USE A VALID VALUE FOR THE
""RECROP"" ATTRIBUTE. CHECK YOUR IMAGECR3 DOCUMENTATION FOR VALID VALUES OF
THIS ATTRIBUTE.">
<cfabort>
</cfif>
</cfif>
<cfset ImageCRTagReturn = False>
<cfparam name="ImageCRTagErrorMsg" default="">
<cfparam name="ImageCRTagNewFile" default="">
<cfparam name="ImageCRTagOriginalFile" default="">
<cfparam name="ImageCRTagIWidth" default="">
<cfparam name="ImageCRTagIHeight" default="">
<cfparam name="ImageCRTagTWidth" default="">
<cfparam name="ImageCRTagTHeight" default="">
<cfparam name="ImageCRTagThumbFile" default="">
<cfparam name="CALLER.ImageCRTagErrorMsg" default="">
<cfparam name="CALLER.ImageCRTagNewFile" default="">
<cfparam name="CALLER.ImageCRTagOriginalFile" default="">
<cfparam name="CALLER.ImageCRTagIWidth" default="">
<cfparam name="CALLER.ImageCRTagIHeight" default="">
<cfparam name="CALLER.ImageCRTagTWidth" default="">
<cfparam name="CALLER.ImageCRTagTHeight" default="">
<cfparam name="CALLER.ImageCRTagThumbFile" default="">
<cfset CALLER.ImageCRTagSuccess = False>
<!--- custom tag which will guard against malicious form inputs --->
<!--- wrap in <cftry> incase the tag doesnt exist --->
<cftry>
<cf_inputfilter scopes = "form" chars = "%&<>" tags = "script,object">
<cfcatch></cfcatch>
</cftry>
<!--- if the image field was empty or the file was empty, alert user and
redirect before running upload script --->
<cfif NOT len(evaluate("form.#ATTRIBUTES.FileField#"))>
<cfset ImageCRTagErrorMsg="The image file had 0 bytes and was not uploaded.
Please check your file and try again.">
<cfset ImageCRTagReturn = True>
<!--- else if there was a valid file specified, upload it to the temp
directory for processing --->
<cfelseif len(evaluate("form.#ATTRIBUTES.FileField#"))>
<!--- try uploading the file, if there is an error, return to the form and
alert the user --->
<cftry>
<cffile action="UPLOAD" filefield="form.#ATTRIBUTES.FileField#"
destination="#ATTRIBUTES.TempPath#" nameconflict="overwrite"
accept="#ATTRIBUTES.ImageTypes#">
<cfcatch type="Any">
<cfset ImageCRTagErrorMsg="There was a problem uploading the file,
please check the image type and try again.">
<cfset ImageCRTagReturn = True>
</cfcatch>
</cftry>
<cfif NOT ImageCRTagReturn>
<!--- try renaming the file to replace any uppercase chars or
illigal chars or spaces
if the process fails, delete the temp files and return to
the form to indform the user--->
<cfif len(trim(ATTRIBUTES.NewImageName)) is 0>
<cfset ImageCRTagNewFile = File.ServerFile>
<cfelse>
<cfset ImageCRTagNewFile =
replace(ATTRIBUTES.NewImageName, " ", "", "ALL") & "." & File.ServerFileExt>
</cfif>
<cfif Len(Trim(ImageCRTagNewFile)) is 0>
<cfset ImageCRTagReturn = True>
<cfset ImageCRTagErrorMsg = "There was a problem
renaming the file. If the problem persists, contact your website
administrator.">
</cfif>
<cfif NOT ImageCRTagReturn>
<cftry>
<!--- use this to rename file name w/lcase and will
strip out "_" and " "chars found --->
<cfset ImageCRTagNewFile =
lcase(replacenocase(trim(ImageCRTagNewFile),"_", "","ALL"))>
<cfset ImageCRTagNewFile =
lcase(replacenocase(trim(ImageCRTagNewFile)," ", "","ALL"))>
<cffile action="rename"
source="#ATTRIBUTES.TempPath#\#File.ServerFile#"
destination="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
<cfcatch type="Any">
<cfset ImageCRTagErrorMsg="The file could
not be renamed, there may be unacceptable characters in the files name.
Reanme the file and try again.">
<cfset ImageCRTagReturn = True>
<cffile action="delete"
file="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
</cfcatch>
</cftry>
</cfif>
</cfif>
<cfif NOT ImageCRTagReturn>
<!--- if we made it this far, get image width --->
<cfx_imageCR3 GETIMAGEINFO =
"#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
<!--- set the maxsize for the normal image --->
<!--- if the images original width is greater than the size set for
the section being uploaded to, resize the image --->
<cfif imagecr.width GTE ATTRIBUTES.MaxSize>
<cfset MaxSize = ATTRIBUTES.MaxSize>
<!--- else if the images width is less than the size set for
the section being uploaded to, do not resize the image --->
<cfelse>
<cfset MaxSize = imagecr.width>
</cfif>
<!--- if CREATETHUMb is "Yes", Set the maximum size for the
thumbnail --->
<cfif YesNoFormat(ATTRIBUTES.CreateThumb) is "Yes">
<!--- same size setting process is used for the thumb as is
for the full sized image above --->
<!--- set the thumbsize for the thumb image --->
<cfif imagecr.width GTE ATTRIBUTES.ThumbSize>
<cfset ThumbSize = ATTRIBUTES.ThumbSize>
<cfelse>
<cfset ThumbSize = imagecr.width>
</cfif>
</cfif>
</cfif>
<cfif NOT ImageCRTagReturn>
<!--- try to resize the image to the needed size. if the process
fails, delete the temp files, set the error message and set ImageCRTagReturn
to True--->
<cftry>
<cfif ATTRIBUTES.CropIt is "Large">
<cfx_imagecr3 format="#file.ServerFileExt#"
load="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
save="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
resize="#MaxSize#x"
anchor="#ATTRIBUTES.Anchor#"
recrop="!#ATTRIBUTES.Recrop#"
nocache="1">
<cfelse>
<cfx_imagecr3 format="#file.ServerFileExt#"
load="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
save="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
resize="#MaxSize#x"
nocache="1">
</cfif>
<cfset ImageCRTagIWidth = imagecr.width>
<cfset ImageCRTagIHeight = imagecr.height>
<cfcatch type="Any">
<cfset ImageCRTagErrorMsg="The file could not be resized,
please check your file and try again">
<cffile action="delete"
file="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
<cfset ImageCRTagReturn = True>
</cfcatch>
</cftry>
</cfif>
<cfif NOT ImageCRTagReturn and YesNoFormat(ATTRIBUTES.CreateThumb) is "yes">
<!--- if we made it this far then we have our normal size image, now
try to create a thumb form the NEW image
if the process fails, delete the temp files and return to
the form to indform the user --->
<cftry>
<cfif ATTRIBUTES.CropIt is "thumb">
<cfx_imagecr3
format="#file.ServerFileExt#"
load="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
save="#ATTRIBUTES.TempPath#\#ATTRIBUTES.ThumbPrefix##ImageCRTagNewFile#"
resize="#thumbsize#x"
recrop="!#ATTRIBUTES.ReCrop#"
anchor="#ATTRIBUTES.ANCHOR#"
nocache="1">
<cfelse>
<cfx_imagecr3
format="#file.ServerFileExt#"
load="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
save="#ATTRIBUTES.TempPath#\#ATTRIBUTES.ThumbPrefix##ImageCRTagNewFile#"
resize="#thumbsize#x"
nocache="1">
</cfif>
<cfset ImageCRTagTWidth=imagecr.width>
<cfset ImageCRTagTHeight=imagecr.height>
<cfcatch type="Any">
<cfset ImageCRTagErrorMsg="The file could not be
resized, please check your file and try again">
<cffile action="delete"
file="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
<cfset ImageCRTagReturn = True>
</cfcatch>
</cftry>
</cfif>
<cfif NOT ImageCRTagReturn>
<!--- try to move the file to its proper directory, if the process
fails, delete the temp files, set the error message and set ImageCRTagReturn
to True --->
<cftry>
<cffile action="MOVE"
source="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#"
destination="#ATTRIBUTES.FinalPath#\#ImageCRTagNewFile#"
nameconflict="error">
<cfif YesNoFormat(ATTRIBUTES.CreateThumb) is "yes">
<cffile action="MOVE"
source="#ATTRIBUTES.TempPath#\#ATTRIBUTES.ThumbPrefix##ImageCRTagNewFile#"
destination="#ATTRIBUTES.FinalPath#\#ATTRIBUTES.ThumbPrefix##ImageCRTagNewFi
le#" nameconflict="error">
</cfif>
<cfcatch type="Any">
<cfset ImageCRTagErrorMsg = "There was an error
moving the file to its correct directory. The file name may already exist.">
<cffile action="delete"
file="#ATTRIBUTES.TempPath#\#ImageCRTagNewFile#">
<cfif YesNoFormat(ATTRIBUTES.CreateThumb) is "yes">
<cffile action="delete"
file="#ATTRIBUTES.TempPath#\#ATTRIBUTES.ThumbPrefix##ImageCRTagNewFile#">
</cfif>
<cfset ImageCRTagReturn = True>
</cfcatch>
</cftry>
</cfif>
<cfif NOT ImageCRTagReturn>
<!--- if the tag made it this far and is inside this CFIF,
everything is done and all that is left to do is set some caller variables
for use in output --->
<cfscript>
CALLER.ImageCRTagErrorMsg = "The image was uploaded successfully.";
CALLER.ImageCRTagOriginalName = File.ServerFile;
CALLER.ImageCRTagNewFile = ImageCRTagNewFile;
CALLER.ImageCRTagIWidth = ImageCRTagIWidth;
CALLER.ImageCRTagIHeight = ImageCRTagIHeight;
CALLER.ImageCRTagSuccess = True;
if (YesNoFormat(ATTRIBUTES.CreateThumb) is "yes")
{
CALLER.ImageCRTagThumbFile = ATTRIBUTES.ThumbPrefix &
ImageCRTagNewFile;
CALLER.ImageCRTagTWidth = ImageCRTagTWidth;
CALLER.ImageCRTagTHeight = ImageCRTagTHeight;
}
else
{
CALLER.ImageCRTagThumbFile = "N/A";
CALLER.ImageCRTagTWidth = "N/A";
CALLER.ImageCRTagTHeight = "N/A";
}
</cfscript>
<cfelse>
<!--- else there was an error somewhere so set the caller variable
for the error message --->
<cfset CALLER.ImageCRTagErrorMsg = ImageCRTagErrorMsg>
<cfset CALLER.ImageCRTagSuccess = False>
</cfif>
</cfif>
<cfsetting enablecfoutputonly="No">
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.16.2/613 - Release Date: 1/1/2007
2:50 PM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create robust enterprise, web RIAs.
Upgrade & integrate Adobe Coldfusion MX7 with Flex 2
http://ad.doubleclick.net/clk;56760587;14748456;a?http://www.adobe.com/products/coldfusion/flex2/?sdid=LVNU
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:265491
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4