Code 2.0 - Process Thumbnails and Images

Discussion in 'Programming' started by digga121, Aug 26, 2008.

  1. #1
    Following for use with Cold Fusion 8.0

    Creating Thumbnails, and Re-sizing pictures after uploading can be a pain in the ass. This code will re-size your images, keeping aspect ratio. This can be made into a function, however, I just have it as a .cfm. You would include this, after your upload of the picture ..This will resize your image, to a maximum height of 600, and max width of 450.


    perform_process_image.cfm

    <cfoutput>
    #currentpath#\img\uploadimages\#imagename#
    <CFSET currentpath = ExpandPath("./")> 
    <cfimage action="info" structname="imagetemp" source="#currentpath#img\uploadimages\#imagename#">
    <cfset x=min(600/imagetemp.width, 450/imagetemp.height)>
    <cfset newwidth = x*imagetemp.width>
    <cfset newheight = x*imagetemp.height>
    <cfif newheight LT 20>
    <cfset newheight = 40>
    </cfif>
    <cfif newwidth LT 20>
    <cfset newwidth = 40>
    </cfif>
    <cfimage action="resize" source="#currentpath#img\uploadimages\#imagename#" overwrite = "true" width="#newwidth#" height="#newheight#" destination="#currentpath#gallery\images\#imagename#">
    </cfoutput>
    Code (markup):
    This is extremely usefull for clients that do not use phoshop, and need to upload images to your application, wheather it be a real estate engine, or anything with a picture. I would create 2 of these actions, and include them after your upload. One for regular picture size, which we see here, and one for your thumbnail image..Here is my thumbnail image re-size code. It does not re-size, but rather, starts at the top, and goes 60 accross, and 60 down, then crops it. This makes for a much better looking thumbnail image, then a re-sized one that may be off ratio ....This one is a little more complicated ...

    Perform_process_thumbnail_image.cfm

    <cfoutput>
     
    <CFSET currentpath = ExpandPath("./")> 
    <cfimage action="info" structname="imagetemp" source="#currentpath#img\uploadimages\#imagename#">
    <br /><br />oldwidth|#imagetemp.width# - 
    oldheight|#imagetemp.height#
     
    <cfif imagetemp.width GT imagetemp.height>
    - HORIZONTAL<br />
    <cfset x=min(190/imagetemp.width, 135/imagetemp.height)>
    <cfelse>
     
    -VERTICAL<br />
    <cfset x=min(190/imagetemp.width, 135/imagetemp.height)>
     
     
    
    </cfif>
    <cfset newwidth = x*imagetemp.width>
    <cfset newheight = x*imagetemp.height>
    <br />newwidth|#newwidth# <br /> 
    newheight|#newheight# <br />
    <cfset newheight = #round(newheight)#>
    <cfif newheight LT 20>
    <cfset newheight = 40>
    </cfif>
    <cfif newwidth LT 20>
    <cfset newwidth = 40>
    </cfif>
     
     <cfimage action="resize" source="#currentpath#img\uploadimages\#imagename#" overwrite = "true" width="#newwidth#" height="#newheight#" destination="#currentpath#gallery\images\fromthegallery\#imagename#">
     
    <cfimage action="read" name="img1" source="#currentpath#gallery\images\fromthegallery\#imagename#">
    <cfoutput>
    #imagetemp.width#<br />
    #img1#</cfoutput>
    <cfset ImageCrop(img1,10,10,imagetemp.width,135)>
     <cfif imagetemp.height GT imagetemp.width>  
     
    <cfimage source="#img1#" action="write" destination="#currentpath#gallery\images\fromthegallery\#imagename#" overwrite="yes">
    <img src="\gallery\images\fromthegallery\#imagename#" />
     
    </cfif>
     
    
     
    <cfimage action="read" namer="img2" source="#currentpath#gallery\images\fromthegallery\#imagename#">
    
    <cfset ImageCrop(img2,0,0,100,100)>
    <cfimage source="#img2#" action="write" destination="#currentpath#gallery\images\thumbnails\#imagename#" overwrite="yes">
    <img src="\gallery\images\thumbnails\#imagename#" />
    </cfoutput>
    Code (markup):
    Just do a <cfinclude template="perform_proccess_image.cfm">
    after your upload call, and your original image will be saved, and your resized images will apear in the appropriate folder. You can also do a delete on the original picture that is uploaded if you like, I keep it there, unless hosting has limited space.
     
    digga121, Aug 26, 2008 IP