Ask Ben: Zipping Images With ColdFusion 8's CFZip And CFHttp

Posted November 7, 2007 at 2:18 PM

Tags: ColdFusion, Ask Ben

I have to download image files off of a another server and zip them up. I have a url for each image and right now I'm using cfhttp to get the image. Any idea how to zip them up without writing them out to a tmp directory?

When I read this question, I got all giddy 'cause I thought I was gonna be able rock some spicy CFImage - CFZip cohabitation. I did go down that road, but in the long run, it didn't work. Originally, I had this plan to use ColdFusion 8's new CFImage tag to grab the images from the given URLs. Then, all I would have to do is grab the image Blob object (ImageGetBlob()) and zip that into the archive.

This had sweet benefits. For starters, it's much less code than a CFHttp call - you put in the URL as the CFImage source attribute and it just downloads and creates the ColdFusion 8 image object. There was also implicit type validation; we were gonna have to use CFTry / CFCatch no matter what, since we are dealing with an external resource, but CFImage had the implicit image type validation since you can't just load any old file into a ColdFusion image object. It would have also put us in a great position if we wanted to do any type of image conversion.

So, with all the goodness, what went wrong with the CFZip / CFImage approach? There were simply too many variables when it comes to dealing with URL-based images. Most importantly, though, we lose file-type information since we cannot see the response header. Sure, all is easy when you are calling .JPG or .GIF files directly, but what happens when you call a ColdFusion page (.CFM) that is serving up image content? The CFImage tag knows how to read that in, but what type of image is it from our perspective? We didn't see the response header so we don't know. This isn't a problem when we are writing the images to the file system, but this causes a huge problem when we need to utilize the Blob data (which is file-type dependent).

It would have been sweet-ass-sweet, but when it comes to in-memory blob usage and zip archiving, CFImage just didn't quite get us there. The next solution is to rely on ColdFusion's standard CFHttp call. Using ColdFusion's CFHttp tag, we can still grab the image files as binary objects, which complements ColdFusion 8's new CFZipParam tag quite nicely, and it still gives us the ability to look the response header and more easily determine the file type.

When it comes to zipping the image data, I am using a Hash() of the URL so that we lower the chances of us overwriting other images we are downloading at the same time. If you KNOW THE FILE NAMES ahead of time and you know how the foreign server is serving up files, then you don't need this; additionally, you could use ColdFusion 8's CFImage tag as well. I tried to build the more flexible solution, but if you know the file naming for fact, then you could have written something a bit shorter and more elegant.

As far as the zip file is concerned, you do have to have the zip file written to the file system. There is no getting around that using ColdFusion 8's CFZip tag as it does not allow variable-based file creation. This is not a bad thing since Zip files might become very large and become a drain on the system RAM. If it is absolutely necessary to do all in-memory stuff, you might want to check out my ZipUtility.cfc which can handle in-memory zipping.

That being said, here is what I came up with:

 Launch code in new window » Download code as text file »

  • <!---
  • Param the FORM variable that will hold the list of URLs
  • (delimited by line breaks and carriage returns).
  • --->
  • <cfparam
  • name="FORM.urls"
  • type="string"
  • default=""
  • />
  •  
  •  
  • <!--- Check to see if the user has entered any URLs. --->
  • <cfif Len( FORM.urls )>
  •  
  • <!---
  • Get a temporary file in the system's temp directory.
  • Since CFZip cannot write to memory, we have to have
  • some sort of physical file (but this will be our
  • only one).
  • --->
  • <cfset strZipPath = GetTempFile(
  • GetTempDirectory(),
  • "images"
  • ) />
  •  
  •  
  • <!---
  • Set a valid flag so that we will know if at least one
  • valid image was zipped.
  • --->
  • <cfset blnIsValidZip = false />
  •  
  •  
  • <!--- Now, let's loop over the URLs. --->
  • <cfloop
  • index="strImageURL"
  • list="#FORM.urls#"
  • delimiters="#Chr( 13 )##Chr( 10 )#">
  •  
  • <!---
  • Try to read in the image url using CFHttp. Since
  • we have to rely on a network connection and an
  • outside source, be sure to wrap in CFTry / CFCatch.
  • --->
  • <cftry>
  •  
  • <!--- Grab the image using CFHttp. --->
  • <cfhttp
  • method="get"
  • getasbinary="yes"
  • url="#strImageUrl#"
  • useragent="#CGI.http_user_agent#"
  • result="objImage">
  •  
  • <!--- Deal with hot-linking issues. --->
  • <cfhttpparam
  • type="header"
  • name="referer"
  • value="#GetDirectoryFromPath( strImageURL )#"
  • />
  •  
  • </cfhttp>
  •  
  •  
  • <!---
  • Check to make sure the get was successful.
  • This means that we got a 200 status code and
  • that the return type was some sort of image.
  • --->
  • <cfif (
  • FindNoCase( "200", objImage.StatusCode ) AND
  • StructKeyExists( objImage.ResponseHeader, "content-type" ) AND
  • FindNoCase( "image", objImage.ResponseHeader[ "content-type" ] )
  • )>
  •  
  • <!--- Grab the file type. --->
  • <cfset strExt = REReplace(
  • objImage.ResponseHeader[ "content-type" ],
  • "^.*?image[\\\/](\w+).*$",
  • "\1"
  • ) />
  •  
  • <cfelse>
  •  
  • <!--- The image was not valid in some way. --->
  • <cfset objImage = "" />
  •  
  • </cfif>
  •  
  •  
  • <!--- Catch any errors. --->
  • <cfcatch>
  • <!--- Reset image. --->
  • <cfset objImage = "" />
  • </cfcatch>
  •  
  • </cftry>
  •  
  •  
  • <!---
  • Check to see if we now have a valid image to
  • work with. If something went wrong with the image,
  • then it will be a simple value.
  • --->
  • <cfif NOT IsSimpleValue( objImage )>
  •  
  • <!---
  • Add this image to the Zip. All we have to do is
  • grab the File Content from our CFHttp grab. I am
  • using the Hash() of the URL so as to not worry
  • about naming conflicts / overwriting.
  • --->
  • <cfzip
  • action="zip"
  • file="#strZipPath#">
  •  
  • <cfzipparam
  • entrypath="#Hash( strImageURL )#.#strExt#"
  • content="#objImage.FileContent#"
  • />
  •  
  • </cfzip>
  •  
  •  
  • <!---
  • Set the valid flag so that we know at least one
  • valid image was archived in the zip file.
  • --->
  • <cfset blnIsValidZip = true />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • Check to see if we have any zip files. If we do, then
  • we want to stream the zip. If we do not, then we want
  • to delete it.
  • --->
  • <cfif blnIsValidZip>
  •  
  • <!--- Set the header information. --->
  • <cfheader
  • name="content-disposition"
  • value="attachment; filename=images.zip"
  • />
  •  
  • <!---
  • Set the content and stream the zip file. Be sure
  • to delete it when the streaming is done.
  • --->
  • <cfcontent
  • type="application/zip"
  • file="#strZipPath#"
  • deletefile="true"
  • />
  •  
  • <cfelse>
  •  
  • <!--- Delete the unused Zip file. --->
  • <cffile
  • action="delete"
  • file="#strZipPath#"
  • />
  •  
  • </cfif>
  •  
  • </cfif>
  •  
  •  
  • <!---
  • ASSERT: If we have made it this far then either the user
  • did not enter any URLs or none of the urls were valid. We
  • will need to render the form page once again.
  • --->
  •  
  •  
  • <!--- Set the content type and reset the buffer. --->
  • <cfcontent
  • type="text/html"
  • reset="true"
  • />
  •  
  • <cfoutput>
  •  
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>ColdFusion 8 CFZip / CFHttp Download Example</title>
  • </head>
  • <body>
  •  
  • <h1>
  • ColdFusion 8 CFZip / CFHttp Download Example
  • </h1>
  •  
  • <p>
  • Enter in your image URLs, one per line, in the
  • textarea below and then submit. Your images will
  • be downloaded, zipped, and returned to you.
  • </p>
  •  
  • <form action="#CGI.script_name#" method="post">
  •  
  • <label for="urls">
  • Image URLs:
  • </label>
  •  
  • <textarea name="urls" id="urls"></textarea>
  •  
  • <button>Zip Images</button>
  •  
  • </form>
  •  
  • </body>
  • </html>
  •  
  • </cfoutput>

Check out the Online Demo if you want to see this in action. Hope that helps a bit.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 7, 2007 at 3:37 PM // reply »
7 Comments

Awesome dude, I was able to make it work but like you said I had to write the zip out to a file. The issue I was having was I was neglecting to use the getasbinary attribute!


Nov 7, 2007 at 4:06 PM // reply »
7,572 Comments

@Paul,

If you use the Java ZIP libraries, you can write the zip file directly to a byte array output stream, but then you lose the convenience of the CFZip tag.


Jan 19, 2010 at 1:19 PM // reply »
7 Comments

Streaming the zip is a brilliant solution. Every time I visit this site I think I get a little smarter.


Jan 19, 2010 at 1:24 PM // reply »
7,572 Comments

@Jon,

Ha ha, awesome :D Thanks!


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »