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

Thread: Image Upload

  1. #1
    hep
    hep is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Image Upload

    Please help.

    I'm almost illiterate when it comes to PHP coding. I am attempting to modify a script that allows me to upload an image to my server and automatically re sizes it to a "normal" size and a "thumbnail". I like that it does this, but it also saves the original to the server. I can't figure out hot to comment out the lines necessary to NOT save the "original" image. The code is below (with some slight modifications made by me.)
    Any help would be greatly appreciated.


    Code:
    <?php
    /*************************************************
    *
    *
     ****************************************************/
    class maxImageUpload {
        // Maximum upload size
        var $maxUploadSize = 10; // 10 Mb
        
        // Image sizes
        var $normalWidth   = 640;
        var $normalHeight  = 480;
        var $thumbWidth    = 128;
        var $thumbHeight   =  96;
        
        // Image quality
        var $imageQualityNormal = 3; //1:Poor ... 5:Very good
        var $imageQualityThumb  = 1; //1:Poor ... 5:Very good
       
        // Directories to store images
        var $baseDir     = '';
        var $originalDir = 'original';
        var $normalDir   = '../gallery/images/Album';
        var $thumbDir    = 'thumbnail';
        
        // File postfixes
        var $originalPrefix = '';
        var $normalPrefix   = '';
        var $thumbPrefix    = 'thumb_';
        
        // Internal used variables
        var $error = '';
        var $maxMemoryUsage = 128;  // 128 Mb
        
        /**
         * Constructor to initialize class varaibles
         * The upload locations will be set to the actual 
         * working directory
         *
         * @return maxImageUpload
         */
        function maxImageUpload(){
           
           if (!file_exists($this->baseDir)) {
              if (!@mkdir($this->baseDir)){
                 $this->baseDir = getcwd();
              }
           }
    
       /**
         *
         * 
         * I removed the thumb because I dont need it for this !! 
         * see the commented out lines below.
         * ANDY HEP  
         */
              
            $this->originalDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->originalDir.DIRECTORY_SEPARATOR;
           if (!file_exists($this->originalDir)) {
              mkdir($this->originalDir);
          }
           $this->normalDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->normalDir.DIRECTORY_SEPARATOR;
           if (!file_exists($this->normalDir)) {
              mkdir($this->normalDir);
           }
    //       $this->thumbDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->thumbDir.DIRECTORY_SEPARATOR;
    //       if (!file_exists($this->thumbDir)) {
    //          mkdir($this->thumbDir);
    //       }
        }
    
        /**
         * This function sets the directory where to upload the file
         * In case of Windows server use the form: c:\\temp
         * In case of Unix server use the form: /tmp
         *
         * @param String Directory where to store the files
         */
        function setUploadBaseLocation($dir){
            $this->baseDir = $dir;
        }
        
        function showUploadForm($msg='',$error=''){
    ?>
           <div id="container">
                <div id="header"><div id="header_left"></div>
                <div id="header_main">Image Uploader</div><div id="header_right"></div></div>
                <div id="content">
    <?php
    if ($msg != ''){
        echo '<p class="msg">'.$msg.'</p>';
    } else if ($error != ''){
        echo '<p class="emsg">'.$error.'</p>';
    
    }
    ?>
                    <form action="" method="post" enctype="multipart/form-data" >
                         <center>
                             <label>File:
                                 <input name="myfile" type="file" size="30" />
                             </label>
                             <label>
                                 <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                             </label>
                         </center>
                     </form>
                 </div>
                 <div id="footer"></div>
             </div>
    <?php
        }
    
        function uploadImage(){
            $result = true;
            
            if (!isset($_POST['submitBtn'])){
                $this->showUploadForm();
            } else {
                $msg = '';
                $error = '';
                
                //Check image type. Only jpeg images are allowed
                if ( (($_FILES['myfile']['type'])=='image/pjpeg') || (($_FILES['myfile']['type'])=='image/jpeg')) {
                   
                   // Check the output directories
                  if ($this->checkDirs()){
                      $target_path = $this->originalDir . basename( $_FILES['myfile']['name']);
    
                       if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
                          $msg = basename( $_FILES['myfile']['name']).
                         " (".filesize($target_path)." bytes) was stored!";
                      } else{
                          $error = "The upload process failed!";
                          $result = false;
                       }
    
                     // Store resized images
                       if ($result){
                          $this->setMemoryLimit($target_path);
    
                          // Create normal size image
                          $dest = $this->normalDir.$this->normalPrefix.basename($_FILES['myfile']['name']);
                          $this->resizeImage($target_path,$dest,$this->normalWidth,$this->normalHeight,$this->imageQualityNormal);
                          $msg .= "<br/>".basename($dest)." (".filesize($dest)." bytes) was stored!";
    
                          // Create thumbnail image
    //I removed thumbs ooption   $dest = $this->thumbDir.$this->thumbPrefix.basename($_FILES['myfile']['name']);
    //I removed thumbs ooption   $this->resizeImage($target_path,$dest,$this->thumbWidth,$this->thumbHeight,$this->imageQualityThumb);
    //I removed thumbs ooption    $msg .= "<br/>".basename($dest)." (".filesize($dest)." bytes) was stored!";
                          
                       }
                    }
                } else {
                   echo "Only jpeg images are allowed!";
                }
    
                $this->showUploadForm($msg,$error);
            }
    
        }
        
        function checkDirs(){
           $result = true;
           
           if (!file_exists($this->originalDir)){
              $this->error = "The target directory ($this->originalDir) doesn't exists!";
              $result = false;
           } else if (!is_writeable($this->originalDir)) {
              $this->error = "The target directory ($this->originalDir) is not writeable!";
              $result = false;
           } else if (!is_writeable($this->normalDir)) {
              $this->error = "The target directory ($this->normalDir) is not writeable!";
              $result = false;
           } else if (!is_writeable($this->normalDir)) {
              $this->error = "The target directory ($this->normalDir) is not writeable!";
              $result = false;
           } else if (!is_writeable($this->thumbDir)) {
              $this->error = "The target directory ($this->thumbDir) is not writeable!";
              $result = false;
           } else if (!is_writeable($this->originalDir)) {
              $this->error = "The target directory ($this->thumbDir) is not writeable!";
              $result = false;
           }
    
           return $result;      
        }
        
        function setMemoryLimit($filename){
           $width  = 0;
           $height = 0;
           $size   = ini_get('memory_limit');
           
           list($width, $height) = getimagesize($filename);
           $size = $size + floor(($width * $height * 4 * 1.5 + 1048576) / 1048576);
           
           if ($size > $this->maxMemoryUsage) $size = $this->maxMemoryUsage;
             
           ini_set('memory_limit',$size.'M');
    
        }
        
        function resizeImage($src,$dest,$new_width,$new_height,$quality){
           $width  = 0;
           $height = 0;
           
           list($width, $height) = getimagesize($src);
           
           $newImage = imagecreatetruecolor($new_width, $new_height);
           $oldImage = imagecreatefromjpeg($src);
           
           $this->fastimagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height, $quality);
    
           imagejpeg($newImage, $dest, 100);
        }
        
        // Function to resize images
        // Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
        function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
           if (empty($src_image) || empty($dst_image)) { return false; }
           
           if ($quality <= 1) {
             $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
             imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
             imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
             imagedestroy ($temp);
           } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
             $tmp_w = $dst_w * $quality;
             $tmp_h = $dst_h * $quality;
             $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
             imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
             imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
             imagedestroy ($temp);
           } else {
             imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
           }
           return true;
       }
    
    }
    ?>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    . I thought I had the right answer, but realize I didn't
    Last edited by mlseim; 04-30-2009 at 01:14 PM.

  • Users who have thanked mlseim for this post:

    hep (05-01-2009)


  •  

    Posting Permissions

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