Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cold Fusion Image Resize not working

Posted on 2012-08-10
12
Medium Priority
?
534 Views
Last Modified: 2012-08-17
I am using the following code to upload and then resize an image. The code uploads but does not resize the image.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<cfif structKeyExists( FORM, 'imageName' )  >
<cftry>

    <!--- Kevin's standard upload code: begin --->
	<cfscript>  foo = ExpandPath("*.*");  bar = GetDirectoryFromPath(foo);  
dirCount = listLen(bar,"\");  myDir = listGetAt(bar, dirCount - 1, "\")
;</cfscript>
<cfset oneupDirectory = ListDeleteAt("#bar#",ListLen("#bar#","\"),"\")>

<cfset hardPath = "#oneupDirectory#\p">

<cfset thePath="#hardPath#\#form.imageName#.jpg"><!--- set the absolute path to the image --->
<cffile action="upload" filefield="form.myImage"	destination="#thePath#"
	nameconflict="overwrite"         accept="image/gif, image/jpg, image/jpeg, image/pjpeg"> 
	   <cfset fileRead = false> <!--- give some time to upload, prevents trying to resize an image not completely uploaded --->
      <cfset beginTime = getTickCount()>
      <cfloop condition="fileRead EQ false">
		<CFIF FileExists(#thePath#)>
		 <cfset fileRead = true>
				</cfif>           
            <!--- End Loop after 5 minutes --->
            <cfset currentTime = getTickCount()>
            <cfset timeLapsed = (currentTime - beginTime)/1000>
            <cfif timeLapsed GTE 5>
                  <cfset fileRead = true>
            </cfif>
      </cfloop>
	<cfif cffile.fileWasSaved>
		Upload successful!
		<cfset pic = imageread(#thePath#)>
	
		 <cfimage action="resize" 
		 width="300" 
		 height="" 
		 source="#pic#" 
		 destination="#thePAth#" overwrite="yes"> 
		 <!--- resize the image --->
		<cfquery name="imgInsert" datasource="yourDSN">
		<!--- insert into db --->
		insert into mytable (imageid, Imagename) values (<cfqueryparam cfsqltype="cf_sql_integer" value="#createuuid#">,<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.imageName#">)
		</cfquery>

	<cfelse>
	Error:
	<cfoutput>#cfcatch.Message#</cfoutput>
	</cfif>
<cfcatch>

        <cfif FindNoCase("not accepted", cfcatch.Message)>
           <cfset error ="Only the following file types are allowed: .jpg, .gif, .bmp, .png.">
<cfoutput>               #error#</cfoutput>
         </cfif>
    </cfcatch>

</cftry>




<cfelse>

	
<form id="uploadForm" action="upload.cfm" method="post" enctype="multipart/form-data"> 
    Upload Image: <input type="file" name="myImage" /> 
	Image Name: <input type="text" value="" name="imageName">
    <input type="submit" value="Upload" /> 
</form>

</cfif>
</body>
</html>
                                            

Open in new window

0
Comment
Question by:swaggrK
  • 6
  • 6
12 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 38282343
How are you determining the image is not resized?
0
 

Author Comment

by:swaggrK
ID: 38282374
By noting the file size on the server as well as downloading the file from the server.
0
 

Author Comment

by:swaggrK
ID: 38282379
Also, my server is CF9.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 52

Expert Comment

by:_agx_
ID: 38282408
File size isn't always a reliable indicator. As far as downloading, make sure you're not just being tripped up by browser caching.

Add some debugging to your code so you can see the dimensions before and after:

      <cfset pic = imageread( thePath )>
      Before Dimensions <cfdump var="#pic#">

        <cfimage action="resize"
             width="300"
             height=""
             source="#pic#"
             destination="#thePAth#" overwrite="yes">

      <cfset pic = imageread( thePath )>
      After Dimensions <cfdump var="#pic#">
        ...
0
 

Author Comment

by:swaggrK
ID: 38285924
Dump 1 is empty.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<cfif structKeyExists( FORM, 'imageName' )  >
<cftry>

    <!--- Kevin's standard upload code: begin --->
	<cfscript>  foo = ExpandPath("*.*");  bar = GetDirectoryFromPath(foo);  
dirCount = listLen(bar,"\");  myDir = listGetAt(bar, dirCount - 1, "\")
;</cfscript>
<cfset oneupDirectory = ListDeleteAt("#bar#",ListLen("#bar#","\"),"\")>

<cfset hardPath = "#oneupDirectory#\p">

<cfset thePath="#hardPath#\#form.imageName#.jpg"><!--- set the absolute path to the image --->
<cffile action="upload" filefield="form.myImage"	destination="#thePath#"
	nameconflict="overwrite"         accept="image/gif, image/jpg, image/jpeg, image/pjpeg"> 
	   <cfset fileRead = false> <!--- give some time to upload, prevents trying to resize an image not completely uploaded --->
      <cfset beginTime = getTickCount()>
      <cfloop condition="fileRead EQ false">
		<CFIF FileExists(#thePath#)>
		 <cfset fileRead = true>
				</cfif>           
            <!--- End Loop after 5 minutes --->
            <cfset currentTime = getTickCount()>
            <cfset timeLapsed = (currentTime - beginTime)/1000>
            <cfif timeLapsed GTE 5>
                  <cfset fileRead = true>
            </cfif>
      </cfloop>
	<cfif cffile.fileWasSaved>
		Upload successful!
	Dunmp 1:<br>
	<cfset pic = imageread(#thePath#)>
	<cfdump var="#pic#">
		
		 <cfimage action="resize" 
		 width="300" 
		 height="" 
		 source="#pic#" 
		 destination="#thePAth#" overwrite="yes"> 
		 
		 Dump 2:<br>
		 <cfset pic = imageread( thePath )>
      <cfdump var="#pic#">
		 <!--- resize the image --->
		<cfquery name="imgInsert" datasource="yourDSN">
		<!--- insert into db --->
		insert into mytable (imageid, Imagename) values (<cfqueryparam cfsqltype="cf_sql_integer" value="#createuuid#">,<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.imageName#">)
		</cfquery>

	<cfelse>
	Error:
	<cfoutput>#cfcatch.Message#</cfoutput>
	</cfif>
<cfcatch>

        <cfif FindNoCase("not accepted", cfcatch.Message)>
           <cfset error ="Only the following file types are allowed: .jpg, .gif, .bmp, .png.">
<cfoutput>               #error#</cfoutput>
         </cfif>
    </cfcatch>

</cftry>




<cfelse>

	
<form id="uploadForm" action="upload.cfm" method="post" enctype="multipart/form-data"> 
    Upload Image: <input type="file" name="myImage" /> 
	Image Name: <input type="text" value="" name="imageName">
    <input type="submit" value="Upload" /> 
</form>

</cfif>
</body>
</html>
                                            

Open in new window

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 600 total points
ID: 38288523
<!--- give some time to upload, prevents trying to resize an image
          not completely uploaded --->

Hm.. maybe the image wasn't saved yet.   Add some more debugging.  What's the value of #fileRead# before you do the resize?

             ....
            </cfloop>
             fileRead = <cfoutput>#fileRead#</cfoutput>
             fileExists = <cfoutput>#FileExists(thePath)#</cfoutput>
           <cfif cffile.fileWasSaved>
                 Upload successful!
                    Dump 1:<br>
                    <cfset pic = imageread(#thePath#)>
                    IsImage = <cfdump var="#IsImage(pic)#">
                    <cfdump var="#pic#">
                       ... rest of code
             </cfif>
0
 

Author Comment

by:swaggrK
ID: 38288570
Here you go...

Results:

fileRead = true fileExists = YES
Upload successful! Dunmp 1:
\ IsImage = YES

struct
colormodel 	
struct
alpha_channel_support 	NO
alpha_premultiplied 	NO
bits_component_1 	8
bits_component_2 	8
bits_component_3 	8
colormodel_type 	ComponentColorModel
colorspace 	Any of the family of RGB color spaces
num_color_components 	3
num_components 	3
pixel_size 	24
transparency 	OPAQUE
height 	1200
source 	D:\Websites\C150364\secc-chicago.org\wwwroot\secc_chicago\p\golf.jpg
width 	1920
Dump 2:
struct
colormodel 	
struct
alpha_channel_support 	NO
alpha_premultiplied 	NO
bits_component_1 	8
bits_component_2 	8
bits_component_3 	8
colormodel_type 	ComponentColorModel
colorspace 	Any of the family of RGB color spaces
num_color_components 	3
num_components 	3
pixel_size 	24
transparency 	OPAQUE
height 	188
source 	D:\Websites\C150364\secc-chicago.org\wwwroot\secc_chicago\p\golf.jpg
width 	300 

Open in new window

0
 

Author Comment

by:swaggrK
ID: 38288577
It worked this time but it seems very inconsistent.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38288673
Hm.. the cfdump being blank is odd.  I've never had that happen unless the value was overwritten.  Plus the original code wasn't checking the value of #fileRead#. You should probably include that variable in your check too:

  <cfif fileRead  AND cffile.fileWasSaved>
           ... do save and resize code
  <cfelse>
         failed .. throw error
  </cfif>

              > nameconflict="overwrite"  

Also if there's the potential for two users to be modifying the same image at the same (ie upload + resizing) time you'll probably want to add locking to prevent two thread operations from clashing.
0
 

Author Comment

by:swaggrK
ID: 38290503
@_agx_

I have it working now, but I have one last question...
How can I account for various image extension besides .JPG


<cfset thePath="#hardPath#\#form.imageName#.jpg">

<cffile action="upload" 
filefield="form.myImage"	
destination="#thePath#"
	nameconflict="overwrite"         
	accept="image/gif, image/jpg, image/jpeg, image/pjpeg"> 

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 38292517
I like to create a list of the allowed extensions:

      <cfset allowedExtensions = "jpg,jpeg,png,gif,..etc...">

Then use it to validate the extension of the uploaded file.  

       <cfif listFindNoCase(allowedExtensions, cffile.serverFileExt)>
                it's allowed. do something
        <cfelse>
                not allowed. do some error handling here ...
        </cfif>

The "accept" attribute doesn't do much for security. At least not pre-CF10. So you need to combine it with file extension checks, placing files outside the web root, etc...
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38305287
> I have it working now,

Did you do anything extra beyond what was mentioned above?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question