Copy link to clipboard
Copied
Hi all,
Complete ColdFusion newbie who's taken over a massive website written in CF8. I'm slowly fumbling my way through things but thought I should at least start using the forums for help a bit more than spending countless hours working stuff out on my own.
I've got the following code which checks an images size, then tries to resize if its too big. It's of course failing with details below. Any help would be great. The code is:
<cfif FileExists("#SITE_PATH#/images/wines/bottles/big/#fileName#")> | |||||||
<cfset bottleImage = "#SITE_PATH#/images/wines/bottles/big/#fileName#"> | |||||||
</cfif> | |||||||
<cfif bottleImage.width gt 345> | |||||||
<cfset imageScaleToFit(#bottleImage#,"250","")> | |||||||
<img src="#bottleImage#" border="0" alt="#prdName#"> | |||||||
</cfif> |
I'm getting an error which states:
I've just started learning Java at work so I understand that it thinks bottleImage is a String. But then I'm asking it the test the Strings image size which of course doesn't work. How do I feed into ColdFusion the actual image "object" so that it can then use methods based on it?
Thanks all!
Matt
Copy link to clipboard
Copied
You need to feed the path (physical file path or url) into the ImageNew() function. If the path represents a valid image, the function will load and return an image object. You can find more details and examples in the documentation
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_h-im_34.html#5179132
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_02.html#3532194
-Leigh
Copy link to clipboard
Copied
In addition to Owain's answer, it is this command,
<cfset bottleImage = "#SITE_PATH#/images/wines/bottles/big/#fileName#">
that creates the bottleImage variable. Given the opening and closing double quotes, it's a string.
In future, when you get that type of error message, check the line number in the error message and see what variable was created. Then cfdump that variable to see what it actually is.
By the way, here is the reverse of that error message:
Troubleshooting remains the same.
Copy link to clipboard
Copied
I think the problem isn't there fellows... it's rather the imagewitdh that wouldn be set
check http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Images_20.html
there you'll find <cfif ImageGetHeight(myImage) gt 800 or ImageGetWidth(myImage) gt 800> and more hints to make your script better.
also don't forget to read the image before that line <cfimage action="read" source="#fileUpload.serverfile#" name="myImage">
Copy link to clipboard
Copied
it's rather the imagewitdh that wouldn be set
No, the syntax imageObjectVariable.width is valid. The problem is the original code is not actually creating an image object, just a string.
Copy link to clipboard
Copied
that's where the cfimage action read part comes in handy 😉
Copy link to clipboard
Copied
As does the earlier one on imageNew()
Copy link to clipboard
Copied
Well I've got it to at least not crash with an error, but instead now shows nothing as if something has gone wrong between the image and outputting the image via the src="xxxx".
Here's the code:
<cfif FileExists("#SITE_PATH#/images/wines/bottles/big/#fileName#")>
<!-- <cfset bottleDisplay = ImageNew("#SITE_PATH#/images/wines/bottles/big/#fileName#")> -->
<cfimage action="read" source="#SITE_PATH#/images/wines/bottles/big/#fileName#" name="bottleDisplay"> <--- Have tried both, neither makes a difference
</cfif>
<cfif bottleDisplay.width gt 345>
<cfset imageScaleToFit(bottleDisplay,"250","")>
</cfif>
<img src="#bottleDisplay#" border="0" alt="#prdName#">
I'm thinking it's something in the src line, but what am I to know? It shows the ALT tag fun, just doesn't produce the image anywhere. Any ideas?? Do I have to write the image back (I wouldn't have thought so....)
Thanks,
Matt
Copy link to clipboard
Copied
Your src refers to a path and should refer to the url