Resize the images

lulu50
lulu50 used Ask the Experts™
on
Hi,

I need to resize my images before upload them to my folder.
How would I do this?

here's what I have so far:


<cfset numberoffields = 4>
<cfset targetDirectory = ExpandPath('.')&"\LoadImg\">
  <cfif isdefined("form.upload")>
    <cfset message = "">
    <cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
      <cfset filename = "form.file" & #i#>
      <cfif evaluate(variables.filename) neq "">
        <cffile action="UPLOAD" 
           destination="#targetDirectory#" 
           nameconflict="OVERWRITE" 
           filefield="#variables.filename#"/>
        <cfset message = message & ",File%20#i#%20(#file.serverfile#)%20was%20uploaded">
      <cfelse>
        <cfset message = message & ",File%20#i#%20was%20empty">
      </cfif>
    </cfloop>
    <cflocation url="PostAd.cfm?ChID=1&msg=#variables.message#">
   
 <cfelse>
     <cfif isdefined("url.msg")>
       <div style="color:#FF0000;">
       <cfloop list="#url.msg#" index="i">
         <cfoutput>#i#</cfoutput><br />
       </cfloop>
       </div>
    </cfif>

<form name="RTEDemo" action=""#CGI.script_name#"?ChID=1" method="post" onSubmit="return submitForm();" enctype="multipart/form-data">



     <cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
      <cfset filename = "file" & #i#>
      <input type="File" name="<cfoutput>#variables.filename#</cfoutput>" /><br />
      </cfloop>
  <input type="Submit" name="upload" value="upload">

</form>
</cfif>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015

Commented:
Unfortunately I said that before I found out you weren't using CF8. I haven't used it much, but if you're on MX7 try ImageUtil.cfc http://imageutils.riaforge.org/ 


<cfset filename = "form.file" & #i#>
<cfif evaluate(variables.filename) neq "">

I'm not sure where you got that code from but a lot of it's not recommended.  For example, don't use evaluate. for dynamic variables just use:
<cfif form["form.file" & i] ... >
- don't overuse # signs. a lot of those # signs aren't necessary
<cfset message = message & ",File%20#i#%20(#file.serverfile#)%20was%20uploaded">

- don't use the FILE scope. it was deprecated years ago. Also no need to manually url escape values. Just use the URLEncodedFormat function
Most Valuable Expert 2015

Commented:
Unfortunately I said that before I found out you weren't using CF8

In other words, ImageResize (and a lot of other functions) won't be available to you if you're using MX7 (are you ...?)  If you're on MX7 or earlier, try one of these instead

http://imageutils.riaforge.org/ 
http://www.opensourcecf.com/imagecfc/
www.alagad.com/projects.imageComponent
Brijesh ChauhanStaff IT Engineer

Commented:
You can also use CFX_OPENIMAGE tag, which can be found here http://www.kolumbus.fi/~kr0626/cfx_openimage/, we have been using this with CF 4.5 + and it works great...
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
_agx_:

I am using MX7

Author

Commented:
brijeshchauhan:

I download the zip files but I would I used it  on my project?

Author

Commented:
brijeshchauhan:

Can you please walk me through the installation

So, a short summary what to do…
1. Install the tag and all the other files to some directory
(I can do this)

2. set a system enviroment variable CFX_OPENIMAGE_FULLPATH with value of full
pathname to the installation folder
(I am not sure how to set the variable enviroment)

3. register the dll to Cold Fusion via CF admin page
(I am not sure how to register dll to cold fusion)

4. Try out the samples
(where is the sample that I can try I don't see them?)

Brijesh ChauhanStaff IT Engineer

Commented:
3. register the dll to Cold Fusion via CF admin page

=====

GO to CF Administrator, look for EXTENSIONS on your left hand pannel, then click on CFX TAGS

Extensions > CFX Tags

On This page, click on Register C++ CFX tag

Then Enter the following

Tag Name -> CFX_OPENIMAGE
Server Library -> C:\cfx_openimage\cfx_openimage.dll

Then click Submit.

2. set a system enviroment variable CFX_OPENIMAGE_FULLPATH with value of full
pathname to the installation folder
(I am not sure how to set the variable enviroment)

===============

Right Click on My Computer -> Properties -> Advanced -> Enviroment Variables

Here you can add the EV

Brijesh ChauhanStaff IT Engineer

Commented:
Refer this screen shot for setting Enviroment Variable above

http://www.kolumbus.fi/~kr0626/cfx_openimage/snapshot.png
Most Valuable Expert 2015

Commented:
@lulu50

You can use any of those 3 cfc's with MX7. You don't need to register anything. Just copy the files into your web root and it's ready to use. Resizing:

http://www.opensourcecf.com/imagecfc/docs/resize.cfm
<cfset imageCFC = createObject("component","image")>
<cfset imgInfo = imageCFC.resize("", "C:\Inetpub\wwwroot\myimage.jpg", "C:\Inetpub\wwwroot\myimage2.jpg",100,200)>
<img src="myimage2.jpg" width="100" height="200" alt="resized image to a specific width and height."/>
Brijesh ChauhanStaff IT Engineer

Commented:
4. Try out the samples
(where is the sample that I can try I don't see them?)

=====

In your downloaded FOLDER, you will have a sub-folder called iml_examples, those are the ones which you can run and try.
Most Valuable Expert 2015
Commented:
The alagad image component is equally simple. Just download it, unzip the files into your web root.

http://www.alagad.com/projects.imageComponent.Resize
<!--- create the object --->
<cfset myImage = CreateObject("Component","Image") />
<!--- open the image to resize --->
<cfset myImage.readImage("d:\examples\fatherAndSon.jpg") />
<!--- resize the image to a specific width and height --->
<cfset myImage.scalePixels(100, 100) />
<!--- output the image in JPG format --->
<cfset myImage.writeImage("d:\examples\fatherAndSon-small.jpg", "jpg") />
<!--- the new images --->
<p>
<b>fatherAndSon.jpg:</b><br>
<img src="/examples/fatherAndSon.jpg">
</p>
<p>
<b>fatherAndSon-small.jpg:</b><br>
<img src="/examples/fatherAndSon-small.jpg">
</p>

Open in new window

Brijesh ChauhanStaff IT Engineer

Commented:
Installing the TAG is very easy and will NOT take you more than a couple of minutes. Also it's a very powerfull TAG written in C++, you can use it for doing LOT of Image Manupulations and functions, please read the TAG guid which is there in documentation folder...
Most Valuable Expert 2015

Commented:
@lulu50 - you have the options. rather than drowning you out in a flurry of posts ;-) I'll leave it up to you to choose whichever one suits you best.
Brijesh ChauhanStaff IT Engineer

Commented:
From the options that _agx_ has suggested, I would recommend Image CFC -> http://www.opensourcecf.com/imagecfc/

I have used this on a LINUX box as CFX OPENIMAGE is not supported on LINUX, and IMAGE CFC worked really well and infact for us on that particular project, it performed better than the inbuild cfimage tag (FOR CF 8) which used to CRASH a lot while resizing.
Most Valuable Expert 2015

Commented:
Personally I prefer alagad's. But for basic resizing they're all fine. Time for me to sleep. I'm sure brij will have more to say on it, lol. So you guys continue on without me. Night all!

Author

Commented:
agx_:

I like to use alagad's because it is simple. but I have an error that says:

It is looking for the component.  where can I get this component?

Could not find the ColdFusion Component Image.  
Please check that the given name is correct and that the component exists.  
 
The error occurred in xxx line 144
 
142 :
143 :
144 : <cfset myImage = CreateObject("Component","Image") />
145 :
146 :  <!---open the image to resize --->
 
Brijesh ChauhanStaff IT Engineer

Commented:
where is your Image.cfc component ??
Brijesh ChauhanStaff IT Engineer

Commented:
You have to specify the complete page RELATIVE to web-root, say you have installed the tag in a directory called /webroot/alagad/Image.cfc, then you have to create object like

<cfset myImage = CreateObject("Component","alagad.Image") />

Author

Commented:
_agx_:

the links are broken.  I cannot access or download the zip file

"The alagad image component is equally simple. Just download it, unzip the files into your web root.
http://www.alagad.com/projects.imageComponent.Resize"

Author

Commented:
I cannot open this site to download the component

http://www.alagad.com/downloads/ImageComponent.zip
Brijesh ChauhanStaff IT Engineer
Commented:
You can use IMAGE CFC.. it's equally simple.. download from here

http://imagecfc.riaforge.org/..

_agx_ have already suggested the code.. comment 35774585
Most Valuable Expert 2015

Commented:
the links are broken.  I cannot access or download the zip file
There must be something wrong with your connection.  All four links work fine.

http://www.alagad.com/projects.imageComponent > Select download
http://www.alagad.com/downloads/ImageComponent.zip   (Direct download link)
http://imageutils.riaforge.org/
http://www.opensourcecf.com/imagecfc/

Author

Commented:
Thank you guys I got it to work.

Great !!!!

Thank you both.

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial