CFX_pureImage

v1.0

02/April/2004


© Claude Schnéegans

DESCRIPTION

This tag reads an image in JPEG format and removes all information not strictly necessary for it's display.

Most Image management software and hardware (cameras) produce files that embed information about creation date and method. This information, albeit useful in some circumstances, is useless for display in a browser.

The average size of this data varies from 5 to 10k, and can even grow up to 58k, as I personnally found in Photo Shop files! If you have a bunch of files which contain 5k images (ie Id photos), they will all take more than 50k each, a total of 10 times the normal size!. If you have say 1000 of them, this means a loss of 50 megs on your server disk. If you have 10000 of them, you may be paying your site hosting company for half a Gig for nothing! Many times the price of this tag.

Although most software have a function to remove this information, it is generally hidden some where in the settings and few people actually use it or are even aware of their existence.

This very simple tag then just reads a JPEG file and rewrites a pure image in its simpler possible form.

notes:
1. The tag supports both the standard JPEG format (.JPG) and the EXIF format (.JPE), but not the JPEG 2000 format.

2. The tag can be applied to any file. If a standard JPEG format is not recognized, the file is left untouched.

Important: The JPEG code is never decompressed and recompressed, so the operation is performed with absolutely no quality loss.

NOTE

If you find this custom tag useful, or if you have any suggestion to make it even more useful, just let me know, I'll be glad to enhance it.

INSTALLATION

To use this tag, you need to take the included pureImage.dll, install it on a drive on your server and make it known to the ColdFusion administrator.

You do this by going to the "CFX Tags" tab in the administrator,

  1. click the "add" button.
  2. Specify the name as CFX_pureImage.
  3. For the Serverlibrary(dll), specify the path to the .dll (the directory into which you copied pureImage.dll.
  4. It should say for "Procedure", "ProcessTagRequest".
That's it!

For more information refer to the ColdFusion documentation "Managing CFXs".

SYNTAX

<CFX_pureImage FILE="full path name of the file">

RETURNED VARIABLES

The tag sets the following variables in the calling program:
PI_originalSize = the original size of the file;
PI_newSize = the new size of the file;
PI_width = the width in pixels of the image;
PI_height = the height in pixels of the image;

In the case the file contains no superfluous information that can be removed, the file is only read, but the tab can still be used to read the width and height of the image. This tag may then replace any other tag used only to get this information. If the file is not a recognized JPEG format, all variables are returned set to 0.

EXAMPLES

<CFX_pureImage FILE="C:\Images\MyImage.jpg">

Optimize the size of an uploaded image

Before

<CFSET Path=C:\Photos\">
<CFFILE ACTION="Upload"
    FILEFIELD="File_Name"
    DESTINATION="#Path#"
    NAMECONFLICT="MakeUnique">
<CFSET FileName = Path & File.ServerFile>
<CFSET FileSize = File.FileSize>

After

<CFSET Path=C:\Photos\">
<CFFILE ACTION="Upload"
    FILEFIELD="File_Name"
    DESTINATION="#Path#"
    NAMECONFLICT="MakeUnique">
<CFSET FileName = Path & File.ServerFile>
<CFX_pureImage FILE="#FileName#">		
<CFSET FileSize = PI_newSize>

Reduce the size of all files in a directory

<CFSET Path=C:\Photos\">
<CFDIRECTORY 
    directory = "#path#"
    name = "getFiles"
    filter = "*.jp*"> 
<CFLOOP QUERY="getFiles">
    <CFX_pureImage FILE="#path##name#">
    <CFSET saved = PI_originalSize - PI_newSize>
    <CFOUTPUT><P><B>#name#</B> : size = #PI_originalSize#
    <BR>new size = #PI_newSize#
    <BR>saved = #saved#
    </CFOUTPUT>
</CFLOOP>
See other cool tags by
See other cool tags