Link to home
Start Free TrialLog in
Avatar of Dusty
DustyFlag for United States of America

asked on

Coldfusion - ImagescaleToFit function

I'm trying to use the ImagescaleToFit function on all the photos in a folder. I'm getting an error "Unable to cast an object of type java.lang.String to Image." when I run the code below. Can someone look at the code and tell me where I went wrong? Thanks in advance!

<!---IMAGE SCALE TO FIT--->
                  
<cfdirectory directory="#ExpandPath('/ftp/resize/')#" name="picFiles">

<cfloop query="picFiles">

<cfimage name="#picfiles.Name#" source="#ExpandPath('/ftp/resize/')##picfiles.Name#">

<cfset ImageScaleToFit(#picfiles.Name#,600,425)>

</cfloop>
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dusty

ASKER

Hi _agx_,

That runs without an error but does not resize/scale the images?

Thanks!
It won't always rescale, only if the image is too large. IIRC, the function does nothing if the image already fits within the given dimensions.

Edit: If you omit the width -OR- height it should force a resize. For example this should force a height of 100.

            <cfset ImageScaleToFit( someImage, "", 100)>

... or a width of 200

            <cfset ImageScaleToFit( someImage, "200", "")>
Avatar of Dusty

ASKER

I tried setting it to scale to 100px high being that my images are all above that but still they are unchanged. Also tried specifing a destination folder but no files were written there.
> Edit: If you omit the width -OR- height it should force a resize.

Did you try omitting the width or height (see previous edit). That works for me.

Plus it doesn't really make sense to supply both width and height, because then it's really just a resize and ImageResize is the function to use.
Avatar of Dusty

ASKER

Yes I did try that,  I even tried it on a couple different servers, and still no results.

<cfdirectory directory="#ExpandPath('/ftp/resize/')#" name="picFiles">

<cfloop query="picFiles">

<cfimage name="myImage" source="#ExpandPath('/ftp/resize/')##picfiles.Name#">

<cfset ImageScaleToFit(myImage,100,"","highPerformance")>

</cfloop>
Avatar of Dusty

ASKER

Hi _agx_,
I ran some more tests and adjusted for 200px tall images, then did a cfdump and it says the image is sized at 355x200 which would be correct, but if I look at the image in the folder it still says it's 640x361. ? Any idea what could be causing this? Thanks for all your help!


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       200
source       C:\WWW\2013\ftp\resize\110414O.JPG
width       355
(Sorry to drop off like that, I have a few fires to put out .. )

      > but if I look at the image in the folder it still says it's 640x361

Using which tool? Silly question, but are you saving the resized image back to disk? I do not see that in the earlier loop code.

ie  <cfloop ....>
            ... do resizing ...

            <!--- save resized image to disk --->
            <cfset ImageWrite( theImageVariable, "c:/path/to/targetFile.jpg")>
     </cfloop>
Avatar of Dusty

ASKER

Silly? no, LOL. I realized my err last night and got it working, but the result was not what I was looking for in the first place. Maybe you can point me in the "write" direction. here's the scenerio, I need all my images to be the same size dimensions to work in a slideshow. The problem is that the images come in a feed and they are all dif sizes, both landscape and portrait. I need to start with a canvas of say 600x420 and paste those images into this area while maintaining their aspect ratio.
Avatar of Dusty

ASKER

Thanks again!
Did you figure it out?  You can do it w/a little planning.  I'd normally scale the images, and center them on the blank 600x420 canvas, using the original width and height to determine if it's portrait or landscape.
Avatar of Dusty

ASKER

I haven't had a chance to even look at it today. Would you use the ImagePaste function to accomplish this?
Well it depends on the slideshow capabilities.  If it can auto position images that aren't quite 600x420, I'd just scale the images to fit in that area and let the slideshow take it from there.  Then you don't have to worry about things like canvas background.  The other option is use ImagePaste to center the scaled image.  So all images are 600x420. But keep in mind it's a one way street .. it's a lot easier to scale an image down than to crop it later on.