Avatar of Dusty
Dusty
Flag 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>
ColdFusion Language

Avatar of undefined
Last Comment
_agx_

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
_agx_

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dusty

ASKER
Hi _agx_,

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

Thanks!
_agx_

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", "")>
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
_agx_

> 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.
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>
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
_agx_

(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>
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.
Dusty

ASKER
Thanks again!
Your help has saved me hundreds of hours of internet surfing.
fblack61
_agx_

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.
Dusty

ASKER
I haven't had a chance to even look at it today. Would you use the ImagePaste function to accomplish this?
_agx_

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.