title: Boosting performance of a PHP image upload/resize script
body: <p>So I was on this mission to create a moderately flexible, but most importantly, re-usable handler PHP script for image uploading projects.  As I cruised along I ran into a PHP memory limit question that I posted on stackoverflow (can be found here: <a href="https://stackoverflow.com/questions/3792058/php-memory-limit">PHP Memory Limit</a>) and the awesome and helpful answers I got made me realize that I basically suck at optimizing my PHP scripts.  I thought that I'd post what I currently have as my 're-usable' PHP form handler for upload scripts and welcome any feedback the smart devs out there might have to boost performance or all around improve it.</p>
  
  <p>To sum what this handler should do:<br/>
  1) Allow images to be uploaded<br/>
  2) Save a full size version of the image that is resized to a desired width<br/>
  3) Save a thumbnail sized version of the image that is resized to a desired width<br/>
  4) Place a watermark on both images.</p>
  
  <p>I'm using two open source scripts to help with the resizing and watermarking.  How efficiently I'm using them I'm not positive about, but they work and are pretty user friendly.</p>
  
  <pre><code>Simple Image PHP Script: 
  http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  
  Zubrak's Thumbnail Script:
  http://www.zubrag.com/scripts/watermark-image.php
  </code></pre>
  
  <p>Here's my handler:</p>
  
  <pre><code>&lt;?php
  // If a file is being uploaded, do somethin' about it!:
  if (!empty($_FILES)) {
  
      // CONFIGURE:
      // How many pixels wide should the full size image be?
      $fullSizeWidth        =    800;
  
      // How many pixels wide should the thumbnail image be?
      $thumbnailWidth        =    100;
  
      // What is the path to the image upload directory?
      $pathToImageDirectory    =    "path/to/image/directory/";
  
      // Create an array of allowable extension types:
      $validExtensions    =    array('jpg', 'jpeg', 'png');
  
      // What will the thumbnail version's suffix be?
      $thumbnailSuffix    =    "_thumbnail";
  
      // What is the path to your watermark image file?
      $pathToWatermark = "path/to/watermark/watermark.png";
  
  
  
  
  
  
      // INCLUDE NEEDED FILES
      // Require the simpleImage class for basic image modifications
      require_once('simpleImage.php');
  
      // Require the Zubrag_watermark class for adding your watermark to images
      require_once('Zubrag_watermark.php');
  
  
  
  
  
  
      // GET THE USER DATA FROM THE FORM (for demo we'll just say they're submitting an image file only):
      // Get the file's temporary name:
      $tempFile             =     $_FILES['file']['tmp_name'];
  
      // Get the file's original name:
      $userFileName        =    $_FILES['file']['name'];
  
      // Get the file's extension:
      $extension = strtolower(end(explode(".", $userFileName)));
  
  
  
  
  
      // UPLOAD DESITNATION:
      // Re-name the image something cool (We'll just hash it for now):
      $theImageName            =    sha1($userFileName);
  
      // Create the full sized image destination by combining it all
      $imageDestination             =    $pathToImageDirectory . $theImageName . "." . $extension;
  
      // Create the thumbnail sized image destination by combining it all
      $thumbnailDestination    =    $pathToImageDirectory . $theImageName . $thumbnailSuffix . "." . $extension;
  
  
  
  
  
      // VALIDATE THE IMAGE:
      // Check to see if the uploaded file has an acceptable extension
      if(in_array($extension, $validExtensions)) {
          $validExtension        =    true;    
      } else {
          $validExtension        =    false;    
      }
  
      // Run getImageSize function to check that we're really getting an image
      if(getimagesize($tempFile) == false) {
          $validImage        =    false;    
      } else {
          $validImage        =    true;    
      }
  
  
  
  
  
  
      // If the extension is valid and the image is valid, accept the file, resize it, and watermark it:
      if($validExtension == true &amp;&amp; $validImage == true) {
          if(move_uploaded_file($tempFile,$imageDestination)) {
              // RESIZE THE IMAGES
  
              // Create simpleImage object
              $image = new SimpleImage();
  
              // Load the uploaded file to memory
              $image-&gt;load($imageDestination);
  
              // Resize the image to desired full size width
              $image-&gt;resizeToWidth($fullSizeWidth);
  
              // Save the image's full sized version
              $image-&gt;save($imageDestination);
  
              // Resize the image to the desired thumbnail width
              $image-&gt;resizeToWidth($thumbnailWidth);
  
  
              // Save the image's thumbnail sized version
                 $image-&gt;save($thumbnailDestination);
  
              // Free the image from memory (note: I added this function to the simpleImage class -- it's simply: imagedestroy($this-&gt;image);)
              $image-&gt;Free();
  
              // WATERMARK THE IMAGES
              // Load the full size image into memory
              $watermark = new Zubrag_watermark($imageDestination);
  
              // Apply the watermark
               $watermark-&gt;ApplyWatermark($pathToWatermark);
  
              // Save the watermarked full-sized file
                $watermark-&gt;SaveAsFile($imageDestination);
  
              // Free the full sized image from memory
              $watermark-&gt;Free();
  
              // Load the thumbnail sized image into memory
              $watermark = new Zubrag_watermark($thumbnailDestination);
  
              // Apply the watermark
               $watermark-&gt;ApplyWatermark($pathToWatermark);
  
              // Save the thumbnail-sized File
                $watermark-&gt;SaveAsFile($thumbnailDestination);
  
              // Free the image from memory
              $watermark-&gt;Free();    
          }
      } else {
          // Error handling for an image that did not pass validation
          echo "So we're basically thinking you tried to upload something that wasn't an image.";
      }
  } else {
      // Error handling for running this script without a file being uploaded
      echo "You should probably upload a file next time.";
  }
  </code></pre>
  
  <p>Thanks all... Any help/thoughts/debate/feedback would be really appreciated.</p>