Description of "showupload.cfm" and "showimage.cfm"

The form data is POSTed to "showupload.cfm" which uses Cold Fusion's <cffile> tag to save the file. A temporary name is generated by using the CreateUUID() function. This file is then read into csImageFile, resized and saved again.

Some error control is required and so some Boolean variables are set in various places to indicate success or failure. They are then used further down the page to produce conditional HTML output. For example, if the file is not a JPEG it will be saved but an error will be generated when csImageFile attempts to read it. When an error occurs the temporary file is deleted.

<cfset currentdir=ExpandPath("./")>
<cfset tempname=CreateUUID()>
<cfset tempfile=CurrentDir & tempname & ".jpg">
<cfset success=false>
<cfset nofile=false>
<cfset imageerror=false>
<cfif Len(form["filesent"]) LT 4>
  <cfset nofile=true>
<cfelse>
  <cffile action="upload" filefield="filesent" destination=#tempfile#>
  <cfset ext=form["filesent"]>
  <cfobject action="create" name="image" class="csImageFile.Manage">
  <cftry>
    <cfset image.ReadFile(#tempfile#)>
    <cfset success=true>
    <cfset oldsize=image.FileSize>
    <cfset image.JpegQuality=form["JpegQuality"]>
    <cfif form["NewSize"] EQ "w">
      <cfset image.Resize(150, 0)>
    </cfif>
    <cfif form["NewSize"] EQ "h">
      <cfset image.Resize(0, 150)>
    </cfif>
    <cfif form["NewSize"] EQ "s">
      <cfset image.Scale(20)>
    </cfif>
    <cfset image.WriteFile(#tempfile#)>
    <cfcatch>
      <cfset imageerror=true>
      <cffile action="delete" file=#tempfile#>
    </cfcatch>
  </cftry>
</cfif>
<html>
<head>
<title> . . .

The output used with a successful upload is shown below.

<p>The upload was successful and the image and some details are shown.</p>
<p>New Height: <cfoutput>#image.height#</cfoutput><br>
New Width: <cfoutput>#image.width#</cfoutput><br>
Uploaded Size: <cfoutput>#oldsize#</cfoutput> Bytes<br>
Reduced Size: <cfoutput>#image.NewFileSize("jpg")#</cfoutput> Bytes
<img src="showimage.cfm?Name=<cfoutput>#tempname#</cfoutput>" width="<cfoutput>#image.width#</cfoutput>" height="<cfoutput>#image.height#</cfoutput>">

The image is displayed using "showimage.cfm". This script is shown below.

<cfcache action="flush">
<cfset currentdir=ExpandPath("./")>
<cfset filename=currentdir & url["name"] & ".jpg">
<cfcontent type="image/jpeg" deletefile="yes" file=#filename#>

The purpose of this script is simply to stream the image to the browser and delete the temporary file. The file name is passed in the URL.

Click Here to return to the upload form.