memory-leakscoldfusioncfimage

Prevent memory leak when using cfimage and cfxml?


I'm doing a site in coldfusion8/mysql 5.0.88 with the front end in Jquery Mobile. I'm also using the photoswipe.js plugin, which allows images to be zoomed and browsed in a separate view layer.

To setup photoswipeable images, I need to output

<cfoutput>
<a class="swipeMe" rel="external" href="#variables.imageSrc#">
    <img src="#variables.imageSrc#" class="adaptImg ui-li-thumb" />
</a>
</cfoutput>

The problem is imageSrc is supplied by users, so I have to grab/validate/resize the image before displaying it AND I need the path of the image for the photoswipe-link.

I have been fiddling with this for a while and came up with the following solution:

 // read img from user specs
 <cfimage name="myImage" source="#bildpfad##bilddateiname#" action="read" />
 <cfif IsImage(myImage) is true>
      // resize
      <cfscript>
           ImageSetAntialiasing(myImage,"on");
           variables.breite = 400;
           ImageScaleToFit(myImage, variables.breite,"", "highestPerformance");
      </cfscript>
      // write to xml, so I can get the path
      <cfxml variable="imageXml">
           <cfimage quality=".5" action="writetobrowser" source="#myImage#" class="adaptImg ui-li-thumb"/
      </cfxml>
      <cfset variables.imageSrc = imageXml.xmlRoot.xmlAttributes.src>
      // output
      <cfoutput>
         <a class="swipeMe" rel="external" href="#variables.imageSrc#">#imageXml#</a>
      </cfoutput>
 </cfif>

While this works it pretty much stalls the application and memory seems to leak as well, because I'm loosing more and more memory as I'm running this.

Question:
Is there any obvious problem with the above code causing memory leaks? I imagin the images are being written to some sort of temporary directory (CFFileservelet?) and stay there for a while blocking my memory. If so, what would be alternative approaches to handling this in an image search?

Thanks!


Solution

  • Why not just create a /tmp folder on your server, and write the manipulated images there, like:

    <cfset newImageName=CreateUUID()&".jpg">
    <cfimage action="write" destination="/tmp/#newImageName#" source="#myImage#">
    

    Then you can use it:

      <cfoutput>
         <a class="swipeMe" rel="external" href="/tmp/#newImageName#"><img src="/tmp/#newImageName#" class="..."></a>
      </cfoutput>
    

    Sample scheduled task to delete temp files:

    <cfdirectory action="LIST" directory="#expandpath('tmp/')#" name="tempfiles" filter="*.jpg">
    <cfloop query="tempfiles">
        <cfif dateadd('h',24,dateLastModified) lt now()>
            <cffile action="DELETE" file="#expandpath('tmp/')##name#">
        </cfif>
    </cfloop>