<cfprocessingdirective pageEncoding="utf-8">
<!---This variable specifies relative path to the folder, where the gallery with uploaded files is located.
Do not forget about the slash in the end of the folder name.--->
<cfset galleryPath="../Gallery/" />
		
<cfset absGalleryPath="#ExpandPath(galleryPath)#" />
<cfset absThumbnailsPath="#absGalleryPath#Thumbnails/" />
			
<!---First of all, clear files and data uploaded at previous time.--->

<!---Delete source files.--->
<cfdirectory name="galleryDir" action="list" 
			directory="#absGalleryPath#" filter="*" />
<cfloop query="galleryDir">
	<cfif FileExists("#absGalleryPath##Name#")>
		<cffile action="delete" file="#absGalleryPath##Name#" />
	</cfif>
</cfloop>

<!---Delete thumbnails.--->
<cfdirectory name="thumbnailsDir" action="list" 
			directory="#absThumbnailsPath#" filter="*">
<cfloop query="thumbnailsDir"> 
	<cfif FileExists("#absThumbnailsPath##Name#")>
		<cffile action="delete" file="#absThumbnailsPath##Name#" />
	</cfif>
</cfloop>
		
<!---NOTE: If you do not want to delete previously uploaded files, just 
remove or comment out the code above.--->

<!---Create XML file which will keep information about files (image dimensions, description, etc). 
XML is used solely for brevity. In real-life application most likely you will use database instead.--->
<cfset descriptions=XmlParse("<files />") />

<!---Get total number of uploaded files (all files are uploaded in a single package) and
iterate through uploaded data and save the original file, thumbnail, and description.--->
<cfloop index="i" from="1" to="#Form.FileCount#">

	<!--- Get source file and save it to disk. --->
	<cffile action="UPLOAD" filefield="SourceFile_#i#" 
		destination="#absGalleryPath#" 
		nameconflict="MakeUnique">
		
	<cfset fileName="#serverFile#">

	<!---In this demo the following imaging operations are applied:
	  
	  - If the user rotates the image in Image Uploader, rotate the original file.
	  - Add a border around the image.--->


	<!---Create a COM-object "GraphicsMill.Bitmap".--->
	<cfobject type="COM" name="bitmap" class="GraphicsMill.Bitmap" action="CREATE">

	<cfset bitmap.LoadFromFile("#absGalleryPath##fileName#") />	
	
	<cfdump var="bitmap" ></cfdump>

<!---
			<!---Rotate the image if necessary--->
			<cfset angle=Int(evaluate("Form.Angle_#i#")) />
			
			<!---Check if user rotated the image--->
			<cfset transforms=bitmap.Transforms />
			<cfif angle is not 0>
				<cfset transforms.Rotate(angle) />
			</cfif>
			
			<!---Add extra pixels to the image to be able to draw a border.--->
			<cfset width=bitmap.Data.Width />
			<cfset height=bitmap.Data.Height />

<!---			&hFFDDDDDD--->

			<cfset transforms=bitmap.Transforms />
			<cfset bitmap.transforms.Crop(-15, -15, width + 30, height + 30, 012) />
---->
<!---			
			<!---Draw double border.--->
			&hFF0000FF
			<cfset bitmap.Graphics.Pen.ForeColor = 15 />
			<cfset bitmap.Graphics.DrawRectangle(13, 13, width + 3, height + 3, False, True) />
			<cfset bitmap.Graphics.DrawRectangle(10, 10, width + 9, height + 9, False, True) />
--->
	
	<!---Save the result file to disk.--->
	<cfset bitmap.SaveToFile("#absGalleryPath##fileName#") />	

	<!--- Get first thumbnail (the single thumbnail in this code sample) and save it to disk. --->
	<cffile action="UPLOAD" filefield="Thumbnail1_#i#" 
		destination="#absThumbnailsPath#" 
		nameconflict="MakeUnique">

	<!--- Rename thumbnail file so that it has .jpg extension --->
	<cffile action="rename"
		source="#absThumbnailsPath#/#serverFile#"
		destination="#absThumbnailsPath#/#fileName#.jpg"> 

	<!---Save file info.--->
	<cfset xmlFile=XmlElemNew(descriptions, "file") />
	<cfset xmlFile.XmlAttributes.name=fileName />
	<cfset xmlFile.XmlAttributes.width=evaluate("Form.Width_#i#") />
	<cfset xmlFile.XmlAttributes.height=evaluate("Form.Height_#i#") />
	<cfset xmlFile.XmlAttributes.height=evaluate("Form.Description_#i#") />
	<cfset ArrayAppend(descriptions.files.XmlChildren, xmlFile) />		
</cfloop>

<cffile action="write" file="#absGalleryPath#Descriptions.xml" 
		output="#toString(descriptions)#" />
