Cold Fusion Image Resize not working

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

swaggrKAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
<!--- 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
 
_agx_Commented:
How are you determining the image is not resized?
0
 
swaggrKAuthor Commented:
By noting the file size on the server as well as downloading the file from the server.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
swaggrKAuthor Commented:
Also, my server is CF9.
0
 
_agx_Commented:
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
 
swaggrKAuthor Commented:
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
 
swaggrKAuthor Commented:
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
 
swaggrKAuthor Commented:
It worked this time but it seems very inconsistent.
0
 
_agx_Commented:
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
 
swaggrKAuthor Commented:
@_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
 
_agx_Commented:
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
 
_agx_Commented:
> I have it working now,

Did you do anything extra beyond what was mentioned above?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.