Many databases store images as BLOB data. To extract BLOB data from a database, create a query with the cfquery tag. The following example shows how to extract BLOB data and use the cfimage tag to write them to files in PNG format:
<!--- Use the cfquery tag to retrieve employee photos and last names from the database. ---> <cfquery name="GetBLOBs" datasource="myblobdata"> SELECT LastName,Image FROM Employees </cfquery> <cfset i = 0> <table border=1> <cfoutput query="GetBLOBs"> <tr> <td> #LastName# </td> <td> <cfset i = i+1> <!--- Use the cfimage tag to write the images to PNG files. ---> <cfimage source="#GetBLOBs.Image#" destination="employeeImage#i#.png" action="write"> <img src="employeeImage#i#.png"/> </td> </tr> </cfoutput> </table>
The following example shows how to use the ImageNew function to generate ColdFusion images from BLOB data:
<!--- Use the cfquery tag to retrieve all employee photos and employee IDs from a database. ---> <cfquery name="GetBLOBs" datasource="myBlobData"> SELECT EMLPOYEEID, PHOTO FROM Employees </cfquery> <!--- Use the ImageNew function to create a ColdFusion images from the BLOB data that was retrieved from the database. ---> <cfset myImage = ImageNew(GetBLOBs.PHOTO)> <!--- Create thumbnail versions of the images by resizing them to fit in a 100-pixel square, while maintaining the aspect ratio of the source image. ---> <cfset ImageScaleToFit(myImage,100,100)> <!--- Convert the images to JPEG format and save them to files in the thumbnails subdirectory, using the employee ID as the filename. ---> <cfimage source="#myImage#" action="write" destination="images/thumbnails/#GetBLOBs.EMPLOYEID#.jpg">
For information on converting a ColdFusion image to a BLOB, see Inserting an image as a BLOB in a database.
Base64 is a way to describe binary data as a string of ASCII characters. Some databases store images in Base64 format rather than as BLOB data. You can use the cfimage tag or the ImageReadBase64 function to read Base64 data directly from a database. This eliminates the intermediary steps of binary encoding and decoding.
The following examples show how to use the cfimage tag to create a ColdFusion image from a Base64 string:
<!--- This example shows how to create a ColdFusion image from a Base64 string with headers (used for display in HTML). ---> <cfimage source="data:image/jpg;base64,/9j/4AAQSkZJRgABAQA.............." destination="test_my64.jpeg" action="write" isBase64="yes"> <!--- This example shows how to use the cfimage tag to write a Base64 string without headers. ---> <cfimage source="/9j/4AAQSkZJRgABAQA.............." destination="test_my64.jpeg" action="write" isBase64="yes">
The following examples show how to use the ImageReadBase64 function to create a ColdFusion image from a Base64 string:
<!--- This example shows how to use the ImageReadBase64 function to read a Base64 string with headers. ---> <cfset myImage=ImageReadBase64("data:image/jpg;base64,/9j/4AAQSkZJRgABAQA..............")> <!--- This example shows how to read a Base64 string without headers. ---> <cfset myImage=ImageReadBase64("/9j/4AAQSkZJRgABAQA..............")>
For more information on Base64 strings, see Converting an image to a Base64 string.
You use the ImageCopy function to copy a rectangular area of an existing image and generate a new ColdFusion image from it. You can paste the new ColdFusion image onto another image, or write it to a file, as the following example shows:
<!--- Use the cfimage tag to create a ColdFusion image from a JPEG file. ---> <cfimage source="../cfdocs/images/artgallery/lori05.jpg" name="myImage"> <!--- Turn on antialiasing to improve image quality. ---> <cfset ImageSetAntialiasing(myImage)> <!--- Copy the rectangular area specified by the coordinates (25,25,50,50) in the image to the rectangle beginning at (75,75), and return this copied rectangle as a new ColdFusion image. ---> <cfset dupArea = ImageCopy(myImage,25,25,50,50,75,75)> <!--- Write the new ColdFusion image (dupArea) to a PNG file. ---> <cfimage source="#dupArea#" action="write" destination="test_myImage.png" overwrite="yes">
Another way to create a ColdFusion image is to duplicate it. Duplicating an image creates a clone, which is a copy of an image that is independent of it: if the original image changes, those changes do not affect the clone, and vice versa. This is useful if you want to create several versions of the same image. Duplicating an image can improve processing time because you retrieve image data from a database or a file once to create the ColdFusion image. Then you can create several clones and manipulate them in memory before writing them to files. For example, you might want to create a thumbnail version, a grayscale version, and an enlarged version of an image uploaded to a server. To do this, you use the cfimage tag or the ImageNew function to create a ColdFusion image from the uploaded file. You use the Duplicate function to create three clones of the ColdFusion image.
To create a clone, you can pass a ColdFusion image variable to the Duplicate function:
<!--- Use the ImageNew function to create a ColdFusion image from a JPEG file. ---> <cfset myImage=ImageNew("../cfdocs/images/artgallery/paul01.jpg")> <!--- Turn on antialiasing to improve image quality. ---> <cfset ImageSetAntialiasing(myImage)> <!--- Use the Duplicate function to create three clones of the ColdFusion image. ---> <cfset cloneA=Duplicate(myImage)> <cfset cloneB=Duplicate(myImage)> <cfset cloneC=Duplicate(myImage)> <!--- Create a grayscale version of the image. ---> <cfset ImageGrayscale(cloneA)> <!--- Create a thumbnail version of the image. ---> <cfset ImageScaleToFit(cloneB,50,"")> <!--- Create an enlarged version of the image. ---> <cfset ImageResize(cloneC,"150%","")> <!--- Write the images to files. ---> <cfset ImageWrite(myImage,"paul01.jpg","yes")> <cfset ImageWrite(cloneA,"paul01_bw.jpg","yes")> <cfset ImageWrite(cloneB,"paul01_sm.jpg","yes")> <cfset ImageWrite(cloneC,"paul01_lg.jpg","yes")> <!--- Display the images. ---> <img src="paul01.jpg"> <img src="paul01_bw.jpg"> <img src="paul01_sm.jpg"> <img src="paul01_lg.jpg">
Also, you can use the cfimage tag and the ImageNew function to duplicate images, as the following example shows:
<!--- Use the cfimage tag to create a ColdFusion image (myImage) and make a copy of it (myImageCopy). ---> <cfimage source="../cfdocs/images/artgallery/paul01.jpg" name="myImage"> <cfimage source="#myImage#" name="myImageCopy"> <!-- Use the ImageNew function to make a copy of myImage called myImageCopy2. ---> <cfset myImageCopy2 = ImageNew(myImage)>