Enforcing size restrictions

ColdFusion provides several functions for retrieving information associated with images, including the height and width of an image. For example, you can use the ImageGetWidth and ImageGetHeight functions to determine whether an image is too large to upload to a website or database.
The following example shows how to prevent users from uploading images that are greater than 300 pixels wide or 300 pixels high:

<cfimage action="read" source="#fileUpload.serverFile#" name="myImage">
<!--- Determine whether the file is greater than 300 pixels in width or height. --->
<cfif ImageGetHeight(myImage) gt 300 or ImageGetWidth(myImage) gt 300>
<!--- If the file exceeds the size limits, delete it from the server. --->
<cffile action="delete" file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">
<cfoutput>
<!--- Display the following message. --->
<p>
The image you uploaded was too big. It must be less than 300 pixels wide and 300 pixels
high. Your image was #imageGetWidth(myImage)# pixels wide and
#imageGetHeight(myImage)# pixels high.
</p>
</cfif>

For information about retrieving image metadata, see the ImageGetEXIFTagImageGetIPTCTag, and ImageInfo functions in the CFML Reference.

Get help faster and easier

New user?