Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help with image resize, upload and write info to database

    I am having problem with the code below. The code is meant to resize an image and create a thumbnail of the image, then upload both image and thumbnail to a /images directory and write their info into the database. But when I run the script, the thumbnail is created while the main image is not resized. here is the script.
    PHP Code:
    <?php
    //Get and work on your extensions
    error_reporting(0);

     
    define ("MAX_SIZE","3600");
     
     function 
    getExtension($str) {
             
    $i strrpos($str,".");
             if (!
    $i) { return ""; }
             
    $l strlen($str) - $i;
             
    $ext substr($str,$i+1,$l);
             return 
    $ext;
     }

     
    $errors=0;
      
     if(
    $_SERVER["REQUEST_METHOD"] == "POST")
     {
         
    $image =$_FILES["file"]["name"];
        
    $uploadedfile $ran2.$extension;
         
     
         if (
    $image
         {
         
             
    $filename stripslashes($_FILES['file']['name']);
         
              
    $extension getExtension($filename);
             
    $extension strtolower($extension);
            
            
     if ((
    $extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
             {
            
                 
    $change='<div class="msgdiv">Unknown Image extension </div> ';
                 
    $errors=1;
             }
    //work on file size
             
    else
             {
     
    $size=filesize($_FILES['file']['tmp_name']);


    if (
    $size MAX_SIZE*3600)
    {
        
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
        
    $errors=1;
    }

    //creates a unique ID with a random number as a prefix - more secure than a static prefix 
    $c uniqid (rand (),true);

    //this md5 encrypts the username from above, so its ready to be stored in your database
    $md5c md5($c);


    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 

    $ran rand () ;

    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 $md5c.".";

    //This assigns the subdirectory you want to save into... make sure it exists!
    $target "images/";

    //This combines the directory, the random file name, and the extension
    $target $target $ran2.$extension

    echo (
    $target);

    if(
    $extension=="jpg" || $extension=="jpeg" )
    {
    $uploadedfile $_FILES['file']['tmp_name'];
    $src imagecreatefromjpeg($uploadedfile);

    }
    else if(
    $extension=="png")
    {
    $uploadedfile $_FILES['file']['tmp_name'];
    $src imagecreatefrompng($uploadedfile);

    }
    else 
    {
    $src imagecreatefromgif($uploadedfile);
    }

    echo 
    $scr;

    list(
    $width,$height)=getimagesize($uploadedfile);


    $newwidth=120;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);


    $newwidth1=25;
    $newheight1=($height/$width)*$newwidth1;
    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
     


    $filename "images/"$ran2.$extension;

    $filename1 "images/small"$ran2.$extension;

    imagejpeg($tmp,$filename,100);

    imagejpeg($tmp1,$filename1,100);

    //apply other form info and save to db

    //This gets all the other information from the form 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $phone=$_POST['phone']; 
    $pic $ran2.$extension

    // Connects to your Database 
    mysql_connect("localhost""root""") or die(mysql_error()) ; 
    mysql_select_db("bizprofile") or die(mysql_error()) ; 

    //Writes the information to the database 
    mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 

    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 


    //Tells you if its all ok 
    echo "The file "basename$_FILES['file']['tmp_name']). " has been uploaded, and your information has been added to the directory"

    else { 

    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."




    }}}
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
    <meta content="en-us" http-equiv="Content-Language">

        <title>picture demo</title>
        
       <link href=".css" media="screen, projection" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery_002.js"></script>
    <script type="text/javascript" src="js/displaymsg.js"></script>
    <script type="text/javascript" src="js/ajaxdelete.js"></script>
        
         
      <style type="text/css">
      .help
    {
    font-size:11px; color:#006600;
    }
    body {
         color: #000000;
     background-color:#999999 ;
        background:#999999 url(<?php echo $user_row['img_src']; ?>) fixed repeat top left;
        
        
        font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; 
        
        }
            .msgdiv{
        width:759px;
    padding-top:8px;
    padding-bottom:8px;
    background-color: #fff;
    font-weight:bold;
    font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px;
    }
    #container{width:763px;margin:0 auto;padding:3px 0;text-align:left;position:relative; -moz-border-radius: 6px;-webkit-border-radius: 6px; background-color:#FFFFFF }
    </style>

      </head><body>
         <div align="center" id="err">
    <?php echo $change?>  </div>
       <div id="space"></div>
       
     
      
      
      
      <div id="container" >
       <div id="con">
       
          
          
            <table width="502" cellpadding="0" cellspacing="0" id="main">
              <tbody>
                <tr>

                  <td width="500" height="238" valign="top" id="main_right">
                 
                  <div id="posts">
                  &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename?>" />  &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename1?>"  />
                    <form method="post" action="" enctype="multipart/form-data" name="form1">
                    <table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="200"><div align="right" class="titles">Picture : </div></td>
              <td width="350" align="left">
                <div align="left">
                  <input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>
              </div></td>
            </tr>
            <tr><Td></Td>
            <Td valign="top" height="35px" class="help">I</Td>
            </tr>
            <tr><Td></Td><Td valign="top" height="35px"><input type="submit" id="mybut" value="       Upload        " name="Submit"/></Td></tr>
            <tr>
              <td width="200">&nbsp;</td>
              <td width="350"><table width="200" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="200" align="center"><div align="left"></div></td>
                    <td width="100">&nbsp;</td>
                  </tr>
              </table></td>
            </tr>
          </table>
                    </form>
     
      
                  
                  
                  </div>
                  
                  
                  
                  
                  </td>
                
                </tr>
              </tbody>
         </table>
          

          
        
            <p>&nbsp;</p>
       </div>
           
      </div>
      

        
    </body></html>
    The db is
    PHP Code:
    -- --------------------------------------------------------

    --
    -- 
    Table structure for table `employees`
    --

    CREATE TABLE IF NOT EXISTS `employees` (
      `
    namevarchar(30) DEFAULT NULL,
      `
    emailvarchar(30) DEFAULT NULL,
      `
    phonevarchar(30) DEFAULT NULL,
      `
    photovarchar(100) DEFAULT NULL
    ENGINE=MyISAM DEFAULT CHARSET=latin1;

    --
    -- 
    Dumping data for table `employees`
    -- 

  • #2
    New Coder
    Join Date
    Feb 2009
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I have figured it out. the problem is on line 68 where I re-assigned the variable of target.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •