• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfimage and resizing

New Here ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

I have what seems like a basic question having to do with cfimage but I haven't been able to find the answer.

Right now I have a form that allows users to upload images using cffile to my server. Upon uploading I run cfimage and the info action to determine the photo's dimensions. I would like to force all images that are over 200px in width to be resized to exactly 200px in width but preserve the ratio with the height. Since people will be uploading pictures with varying dimensions, I can't give a set height to resize to, nor can I use percentage as a resize option. Basically I'm wanting to know if there's an option to hardcode a width and preserve the image's ratio on the height.

TOPICS
Advanced techniques

Views

3.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

Here's some code I use to do what I think you are looking for. Basically if you leave the width or the height blank, cfimage will scale the image and maintain the aspect ratio correctly....

<cfcomponent>

    <cffunction name="ScaleImage" returntype="boolean">
        <cfargument name="inFile" type="string" required="true">
        <cfargument name="outFile" type="string" required="true">
        <cfargument name="intMaxWidth" type="numeric" required="true">
        <cfargument name="intMaxHeight" type="numeric" required="true">
        <cfargument name="quality" type="numeric" default="1" required="false">
       
        <cfset success = TRUE>
       
        <cftry>
            <cfimage action="RESIZE" source="#inFile#"
            destination="#outFile#" height="#intMaxHeight#" width="" overwrite="yes" />
            <cfcatch type="any">
                <cfset success = FALSE>
            </cfcatch>
        </cftry>
           
        <cfif success>
            <cftry>
                <cfimage action="info" structName="resizedHeight" source="#outFile#" />
                <cfcatch type="any">
                    <cfset success = FALSE>
                </cfcatch>
            </cftry>
           
            <cfif (isDefined('resizedHeight.width')) AND (resizedHeight.width GT intMaxWidth)>
                <cftry>
                    <cfimage action="RESIZE" source="#outFile#"
                    destination="#outFile#" height="" width="#intMaxWidth#" overwrite="yes" />
                    <cfcatch type="any">
                        <cfset success = FALSE>
                    </cfcatch>
                </cftry>
            </cfif>
        </cfif>
       
        <cftry>
            <cffile action="delete" file="#inFile#">
            <cfcatch type="any">
            </cfcatch>
        </cftry>
       
        <cfreturn success>
       
    </cffunction>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

You actually don't need to do anything special for this. If you specify the width attribute for cfimage and leave the height attribute blank, ColdFusion will maintain the aspect ratio and set the height accordingly.

<cfimage action="resize" width="200" height="" ...other atts... />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

Cool, that would explain where I messed up. I thought you could leave the height setting empty but instead of giving it a blank value, I just omitted writing that line of code, so that's where my problem was.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

Hmm something must still be wrong with my code because the image still isn't getting resized even after removing the code for checking the original picture dimensions.

Here's the code I'm using

<cfimage

    action = "resize"

    source = "e:\artist_images\#cffile.ServerFile#"

    width = "200"

    height = ""

    destination = "e:\artist_images\#cffile.ServerFile#"

    overwrite = "yes"/>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

try it where the input file and output file are not the same -- i never got the resize with one a height or a width to work otherwise...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 24, 2009 Apr 24, 2009

Copy link to clipboard

Copied

LATEST

I just ran a quick test with the following code:

<cfimage action="resize" source="/Users/me/Desktop/photo.jpg" destination="/Users/me/Desktop/photo.jpg" overwrite="yes" width="480" height="" />

Not only did ColdFusion not resize the image and overwrite the original file, it actually deleted the file completely from my system (wasn't in the trash; literally wiped off the drive) and returned a Java I/O error (generic file not found). The same result occurs regardless of whether or not the height attribute has a value. I tried using explicit values in width and height as well as percentages, all with the same end-result (file deleted).

I did a little snooping, and this issue sounded a lot like an issue with CFImage that prompted a hot fix:

http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403411&sliceId=1

I downloaded the hot fix, followed the instructions to install it that are on the tech note, and re-ran the above code. Worked perfectly. I think you'll be set if you simply install the hot fix on your server.

If you can't install this hot fix for some reason (no server control, etc.), the only way to have the process work without the hot fix in place is to follow sugaractive's advice regarding a unique file name in the destination attribute:

<cfimage action="resize" source="/Users/me/Desktop/photo.jpg" destination="/Users/me/Desktop/photo1.jpg" overwrite="yes" width="480" height="" />

Hope that helps!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation