Register
Hello There, Guest!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The most effective method to Resize And Add An Image To A Database
#1
Little instructional exercise on the best way to re measure a picture with ColdFusion. 


To begin with we will make a shape where you can transfer the picture. 

Lets call it index.cfm

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</title>
</head>
<body>
   <table border="0" cellpadding="0" cellspacing="1" width="500" align="center">
   <cfform action="picture_add.cfm" name="AddPic" method="post" enctype="multipart/form-data">
       <tr>
           <td align="right" class="Copy" valign="top">Photo:</td>
           <td align="left" class="Copy" valign="top">
               <cfinput type="file" name="PICTURE" required="no">
           </td>
       </tr>
       <tr>
           <td>&nbsp;</td>
           <td align="left">
               <cfinput type="submit" name="Submit" value="Submit">
           </td>
       </tr>
       </cfform>
   </table>
</body>
</html>

As should be obvious in our <CFFORM ></CFFORM >tags we call the page picture_add.cfm. 




So when you tap on submit catch the picture_add.cfm page will be called. 



Lets code the picture_add.cfm page.


Code:
<cfparam name="Error" default="">

<cfif Picture neq "">
   <cftry>
       <cffile
           action="upload"
           filefield="Picture"
           destination="C:\ColdFusion9\wwwroot\test\images\tempupload"
           accept="image/jpeg, image/jpg, image/pjpeg"
           nameconflict="makeunique">

       <cfimage
           action = "info"
           source = "C:\ColdFusion9\wwwroot\test\images\tempupload\#serverfile#"
           structname = "Picturetest">
       
       <cfoutput>
           <cfset Picturewidth =  #Picturetest.width#>
           <cfset Pictureheight =  #Picturetest.height#>
           <cfset PictureSmallwidth = 150>
           <cfset PictureSmallHeight = #Pictureheight# / #Picturewidth# * #PictureSmallwidth#>
       </cfoutput>

       <cfimage action = "resize"
           height = "#Round(PictureSmallHeight)#"
           width="150"
           source = "#C:\ColdFusion9\wwwroot\test\images\tempupload\#serverfile#"
           destination = "C:\ColdFusion9\wwwroot\test\images\picturesmall\#serverfile#"
           overwrite = "yes">

   <cfcatch type="any">
       <cfset Error = "1">
   </cfcatch>
   </cftry>
</cfif>

<cfif ERROR eq 1>
   <script language="Javascript">
   alert ("Corrupt or invalid file type. .:Only jpg files accepted:.")
   </script>
       
<cfelse>
   <cfquery name="INSERTPRODUCT" datasource="yourdb">
       INSERT INTO yourname
       (PICTURE)
       VALUES
       ('#SERVERFILE#')
   </cfquery>
   <script language="Javascript">
        alert ("Your picture has been uploaded.")
   </script>
   <cflocation url="index.cfm">
</cfif>

Alright we have now coded the picture_add.cfm page 


Gives up through it well ordered. 

Initially we begin off with a parameter called "Blunder". 

Sick disclose to you later what this variable does. 

We at that point have a cfif articulation. 

This announcement tests the photo field in the index.cfm, it sees were there is a comment. 

The second articulation is there for blunder dealing with, an attempt proclamation on the off chance that anything isn't right with picture a mistake ought to be tossed. 

Our third field is the place the first picture will be transferred it will be sent to area picked by you. 

Your acknowledge writes disclose to you what document expansions to acknowledge. 

Nameconflict will instruct coldfusion if there are two pictures with the same. 

In the <cfimage> label the accompanying happens. 

The photos get re-sized and transferred to your particular registry. 

You should control the sizes of a picture since it will look irregular if the stature and width isn't correct. 

Next there is a catch proclamation to get all blunders. 

Keep in mind the variable that Error we pronounced right before all else this is the place we utilize it. 

We utilize another if explanation to test this esteem. 

On the off chance that our variable is equivalent to one we will utilize a bit of javascript code to tell the client that there is a mistake with the picture. 

We have a cfelse articulation this will execute if our blunder = ""(nothing). 

At that point we have an inquiry that will embed the photo into a database. 

I trust this appeared well and good and made a difference.
Reply
#2
I didn’t realize that ColdFusion is still around. I remember that name from when I was studying Macromedia software.


[Image: giphy.gif] [Image: giphy.gif][Image: giphy.gif]
Reply
#3
(04-20-2018, 12:57 AM)Shadow Fox Wrote: I didn’t realize that ColdFusion is still around. I remember that name from when I was studying Macromedia software.


[Image: giphy.gif] [Image: giphy.gif][Image: giphy.gif]

Its good to go old schoolSmile This brings back good memories from college.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)