0

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!

frequent
  • 27,643
  • 59
  • 181
  • 333
  • I don't know it this i relevant, but the class attribute is not listed as a valid attribute in the CFML docs for the CFIMAGE tag. Does this code work but hang? or just hang? – Dpolehonski Aug 24 '12 at 12:38
  • it works. I'm also setting `id` and `title` and a custom `data-xyz` attribute besides class. I read somewhere that you could add attributes to cfimage allright. Let me see if I find the link. GOt it [here](http://www.bennadel.com/blog/846-Styling-The-ColdFusion-8-WriteToBrowser-CFImage-Output.htm) – frequent Aug 24 '12 at 12:54

1 Answers1

2

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>
Nicklepedde
  • 566
  • 2
  • 11
  • That would also be an option. I liked my solution because it removed the images after a while by itself. Is there some way to do this = cleaning up stored images or setting validity when saving them or do I have to manually clean up? – frequent Aug 24 '12 at 13:32
  • You could timestamp the images in the name or metadata. And at the head of the page execute a CFTHREAD that will clean-up any images older than a certain time. By using a CFthread you won't be waiting for the task to complete. Fire and forget. – Dpolehonski Aug 24 '12 at 13:56
  • I would do as Dpole suggested and use the created datetime of the file to clean up images older then X time. That way you can build some logic the files ie if(image_resized not exists) rezie else use the image so you only need to resize it once per cleanup cycle – Paul Aug 24 '12 at 14:28
  • 1
    You could have a scheduled task that lists the contents of that directory and if a file is over 24 hours old, delete it. Which I believe is a shorter life span than what you'll get with "writetobrowser" – Nicklepedde Aug 24 '12 at 14:29
  • @Nicklepedde: do you have a pointer for scheduled tasks to get started? – frequent Aug 24 '12 at 16:50