Image Manipulation ColdFusion Wrapper Component

<cfcomponent
	output="false"
	hint="Wraps an image to provide dot-notation image functionality and chaining.">
 
	<!--- Our target image. --->
	<cfset VARIABLES.Image = "" />
 
	<!---
		The page context object that will give us access to the built-in
		CF functions that will be used in dynamic evaluation.
	--->
	<cfset VARIABLES.Page = GetPageContext().GetPage() />
 
 
	<cffunction
		name="Init"
		access="public"
		returntype="any"
		output="false"
		hint="Returns an initialized image wrapper.">
 
		<!--- Define arguments. --->
		<cfargument
			name="Image"
			type="any"
			required="true"
			hint="The ColdFusion image object that we are going to be modifying."
			/>
 
		<!--- Store the image. --->
		<cfset VARIABLES.Image = ARGUMENTS.Image />
 
		<!--- Return This reference. --->
		<cfreturn THIS />
	</cffunction>
 
 
	<cffunction
		name="Get"
		access="public"
		returntype="any"
		output="false"
		hint="Returns the target image.">
 
		<cfreturn VARIABLES.Image />
	</cffunction>
 
 
	<cffunction
		name="OnMissingMethod"
		access="public"
		returntype="any"
		output="false"
		hint="Handles missing methods that allow us to trap and leverage the ColdFusion image functionality with as little effort as possible.">
 
		<!--- Define arguments. --->
		<cfargument
			name="MissingMethodName"
			type="string"
			required="true"
			hint="The method that was called."
			/>
 
		<cfargument
			name="MissingMethodArguments"
			type="struct"
			required="true"
			hint="The arguments that were passed to the above mentioned method."
			/>
 
		<!--- Define the local scope. --->
		<cfset var LOCAL = StructNew() />
 
 
		<!---
			Set up a value to hold the return value from our
			ColdFusion method. This needs to be a scoped value
			since we are expecting it to be NULL sometimes.
		--->
		<cfset LOCAL.Return = "{null}" />
 
		<!---
			Build the name of the method that we want to access.
			We are going to assume that this method's name is
			the name that was accessed with "IMAGE" prepended.
		--->
		<cfset LOCAL.MethodName = ("Image" & ARGUMENTS.MissingMethodName) />
 
 
		<!--- Check to see if this method is valid. --->
		<cfif StructKeyExists( GetFunctionList(), LOCAL.MethodName )>
 
			<!---
				Now that we have a valid method to execute, we
				need to build the proper string. Since, there is
				no nice, clean way to dynamically execute a
				built-in method in ColdFusion, we need to do all
				this using Evaluate().
 
				Since all of the ColdFusion image methods take
				the image object as the first argument, let's put
				that as the first element in our argument string.
			--->
			<cfset LOCAL.Arguments = "VARIABLES.Image" />
 
			<!---
				Loop over the missing method arguments to pass
				them to the method.
			--->
			<cfloop
				index="LOCAL.ArgumentIndex"
				from="1"
				to="#ArrayLen( ARGUMENTS.MissingMethodArguments )#">
 
				<!--- Append argument. --->
				<cfset LOCAL.Arguments &= (
					", " &
					"ARGUMENTS.MissingMethodArguments[ #LOCAL.ArgumentIndex# ]"
					) />
 
			</cfloop>
 
 
			<!---
				Execute the given method. We have to execute this
				method through the undocumented Page object since
				ColdFusion doesn't like having VOID-returning
				methods executed dynamically on their own.
			--->
			<cfset LOCAL.Return = Evaluate(
				"VARIABLES.Page.#LOCAL.MethodName#( #LOCAL.Arguments# )"
				) />
 
 
			<!---
				At this point, our return value is either
				nullified (and therefore doesn't exist) or it
				contains a valid return value.
			--->
			<cfif (
					StructKeyExists( LOCAL, "Return" )
				AND
					(
							(
								IsSimpleValue( LOCAL.Return ) AND
								(LOCAL.Return NEQ "{null}")
							)
						OR
							(NOT IsSimpleValue( LOCAL.Return ))
					)
				)>
 
				<!--- We have a real value to return. --->
				<cfreturn LOCAL.Return />
 
			<cfelse>
 
				<!---
					The return value doesn't exist so the method
					we called did not have a return value.
					Return This reference for method chaining.
				--->
				<cfreturn THIS />
 
			</cfif>
 
		<cfelse>
 
			<!--- That method was not supported. --->
			<cfthrow
				type="ImageWrapper.InvalidMethod"
				message="The method you called does not exist."
				detail="The method that you called, #UCase( ARGUMENTS.MissingMethodName )#, is not currently supported."
				/>
 
		</cfif>
	</cffunction>
 
</cfcomponent>

For Cut-and-Paste