6 Replies Latest reply on Apr 24, 2009 4:30 PM by craigkaminsky

    cfimage and resizing

    J_Tremain

      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.

        • 1. Re: cfimage and resizing

          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>

          1 person found this helpful
          • 2. Re: cfimage and resizing
            craigkaminsky Level 3

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

            1 person found this helpful
            • 3. Re: cfimage and resizing
              J_Tremain Level 1

              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.

              • 4. Re: cfimage and resizing
                J_Tremain Level 1

                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"/>

                • 5. Re: cfimage and resizing
                  Level 1

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

                  • 6. Re: cfimage and resizing
                    craigkaminsky Level 3

                    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!