Reply to topic
Image resize.
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
What is the best way to resize an image and not have the image all streched.

I'm trying to resize some images that the user upload to be 120x150 but the image look funny after resize it.

I'm using CF8 and cfimage to do that.
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
Ennio,

Use CFImage with ImageScaleToFit() to resize the image proportionally

Heres an example:

Code:

<!--- GET THE SOURCE IMAGE --->
<cfimage source="/Images/MyImage.jpg" name="TheImage" >

<!--- TURN ON ANTIALIASING TO IMPROVE IMAGE QUALITY. --->
<cfset ImageSetAntialiasing(TheImage,"on")>

<!--- SCALE THE IMAGE PROPORTIONALLY --->
<cfset ImageScaleToFit(TheImage,'',150,"highestQuality")>

<!--- REWRITE BACK TO LOCATION --->
<cfimage action="write" source="#TheImage#" destination="/Images/" overwrite="yes" quality="1">


The above will resize the image proportionally to a max of 150px high. If you wish to resize based on width, just set 120 as the second argument, and set the third argument to ''.

Here are the quickdocs:
http://www.cfquickdocs.com/cf8/#ImageScaleToFit
emmet


Joined: 16 Nov 2004
Posts: 27
Reply with quote
I think he's looking for fixed aspect. Try out aspectcrop() in the imagUtils library. Go to riaforge.com to download them.

Check out an example here

You can download it here
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
emmet wrote:
I think he's looking for fixed aspect. Try out aspectcrop() in the imagUtils library. Go to riaforge.com to download them.

Check out an example here

You can download it here


This will do FixedAscpect. Why use thrid party utilities when CF8 image functions far exceed any of them. (This wasn't true with Pre CF8)
emmet


Joined: 16 Nov 2004
Posts: 27
Reply with quote
I think your misunderstanding. Scaletofit maintains aspect but does not apply a fixed size. He specifically said 120x150. Scaletofit will only insure its not greater than 120 wide or 150 tall.

Also, you didn't take a look at imageUtils i take it. They are mash up utils that only use cf8's built in image functionality and some math.
Jason101
Forum Regular

Joined: 14 Mar 2006
Posts: 548
Location: Harrisburg, PA
Reply with quote
emmet wrote:
I think your misunderstanding. Scaletofit maintains aspect but does not apply a fixed size. He specifically said 120x150. Scaletofit will only insure its not greater than 120 wide or 150 tall.

Also, you didn't take a look at imageUtils i take it. They are mash up utils that only use cf8's built in image functionality and some math.


Emmet, I did misunderstand. I will take a look at image Utils. Thanks for clarifying
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
Hi guys...

Sorry I've been busy and had much time to work on the image...
But I was looking for something to make the image on the fix ration 150x120

I will take a look tomorrow on that.
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
Emmet,
Thanks for the link that worked great.

Laughing
emmet


Joined: 16 Nov 2004
Posts: 27
Reply with quote
Glad it worked out. Smile If you have any suggestions for improvement on aspectcrop() just let me know. I tried to make it as straight forward as possible.
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
It's very simple to use nice job.

One question, today I was doing some more testing and I tried to upload a file from my digital camera, a JPGE image, and I keep on getting this error msg.

And I tried another image and it worked.

An exception occured while trying to write the image.
Ensure that the destination directory exists and that Coldfusion has permission to write to the given path or file. cause : java.io.FileNotFoundException: C:\Websites\em47126e\babyimg\Autumn Leaves.jpg (The system cannot find the file specified)

When I look at the website the image is beeing uploaded.
Any reason why that would happened?
Here is my code.

My Form
Code:

<cfform method="post" enctype="multipart/form-data" action="#CGI.SCRIPT_NAME#?acct=upload">
    <tr>
         <td width="33%">
          Select the file to upload:<a href="#" class="hintanchor" onMouseover="showhint('You can only upload image files, all others will be deleted.', this, event, '200px')">[?]</a></td>
          <td width="67%">
                   <input type="file" name="fileupload" size="20">
          </td>
    </tr>
     <tr>
           <td width="33%">&nbsp;</td>
             <td width="67%"><cfinput type="submit" name="btnUpload" value="Upload"></td>
      </tr>
</cfform>


And here is my upload code.
Code:

<cfif structKeyExists(form,"fileupload")>
<cfset mediapath = 'C:\Websites\em47126e\babyimg\'>
<cfset imageutils = createObject("component", "imageUtils")>
<cffile action="upload" filefield="fileupload" destination="#mediapath#" nameconflict="makeunique">
<!--- READ THE IMAGE --->
<cfimage name="uploadedImage" source="#MediaPath##file.serverFile#" >
<!--- USE THE ImageUtils.cfc TO CROP THE IMAGE FROM THE CENTER --->
<cfset aspect = ImageNew(uploadedImage)>
<cfset aspect = imageutils.aspectCrop(aspect,120,150,'center')>
                               
<!--- REWRITE BACK TO LOCATION --->
<cfimage action="write" source="#aspect#" destination="#MediaPath##file.serverFile#" overwrite="true">
</cfif>
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
I think the error is around this code

Code:

<cfset aspect = ImageNew(uploadedImage)>
<cfset aspect = imageutils.aspectCrop(aspect,120,150,'center')>
                               
<!--- REWRITE BACK TO LOCATION --->
<cfimage action="write" source="#aspect#" destination="#MediaPath##file.serverFile#" overwrite="true">

Because when I remove this part the image uploads ok to the server.
emmet


Joined: 16 Nov 2004
Posts: 27
Reply with quote
The only explanation is that cf isn't seeing your digital camera image as an image. The function fails gracefully and will throw an error. To see the error you need to use cfcatch around your write. Something like

<cftry>
<--- your code --->
<cfcatch>
<p>#cfcatch.message#</p>
</cfcatch>
</cftry>

If it errors it will give you a "The value passed to aspectCrop was not an image."

Your other quick option is just testing the image with isImageFile(yourimage) and seeing what it returns.

I'm curious though, is your image extension .jpge or was that a typo?
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
Sorry it was a typo it's jpg.

I get this error msg.

An exception occured while trying to write the image.
emmet


Joined: 16 Nov 2004
Posts: 27
Reply with quote
Thats your general exception or your cfcatch.message? Did you tet the image with isimagefile()?
Ennio


Joined: 17 Nov 2006
Posts: 98
Location: Scotch Plains, NJ
Reply with quote
I used the cfcatch.message
Image resize.
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 3  

  
  
 Reply to topic