title: PHP image resizing for different mime types
body: <p>I'm a php noob and trying to create a image resizing script that will shrink uploaded images into smaller dimensions. With my current script below I get the original image displayed. The new width and height aren't registering it seems. Any help is appreciated.</p>
  
  <pre><code>    &lt;?php
  
      if ($_SERVER['REQUEST_METHOD']=='POST') {
        if (isset($_FILES['image']['tmp_name'])) {
          if ($_FILES['image']['error']==0){
          $new_dir = '/PHP_MySQL_Practice/uploaded/images';
          $fullpath = $_SERVER['DOCUMENT_ROOT'].$new_dir;
          if(!is_dir($fullpath)) {
              mkdir($fullpath, 0777, TRUE); 
          }
          //get file name and type(extension)
          $name = $_FILES['image']['name'];
          $type = $_FILES['image']['type'];
  
          //separate image name after first "dot"
          $separated = explode(".", $name);
          //get string before the dot which was stored as the 1st item in the separated array.
          $first = $separated[0];
          //use php function pathinfo to get extension of image file
          $ext = pathinfo($name, PATHINFO_EXTENSION);
          //concatenate current timestamp (to avoid file overwrites) with the extracted file 'firstname' and add the extension
          $name = time().'_'.$first.'.'.$ext;
          $target = $fullpath.'/'.$name;
  
          if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  
              resizeImage($target, $name);
              echo '&lt;img src="' . $target . '"/&gt;';
  
          }
  
      }
      else {
  
          echo 'there was an error in saving ur image file.';
      }
  }   
  else {
  
      echo 'ur image could not be uploaded.';
  }
      }
      else {
      ?&gt;
  &lt;form method="POST" enctype="multipart/form-data"&gt;
      &lt;label&gt;upload image: &lt;/label&gt;
      &lt;input type="file" name="image" /&gt;
      &lt;input type="submit" /&gt;
  &lt;/form&gt; 
      &lt;?php   
      }
  
      //function to resize image
      function resizeImage($dir, $img) {
  list($src_w, $src_h) = getimagesize($dir);
  $max_w = 150;
  $max_h = 150;
  if ($src_w &gt; $max_w) {
      $ratio = $max_w / $src_w;
      $new_img_w = $src_w * $ratio;
  }
  else if ($src_h &gt; $max_h) {
      $ratio = $max_h / $src_h;
      $new_img_h = $src_h * $ratio;
  }
  $img_mime_type = getimagesize($dir);
  switch ($img_mime_type['mime']) {
      case 'image/jpeg':
      case 'image/pjpeg':
          $src = imagecreatefromjpeg($dir);
          return $src;
          break;
      case 'image/gif':
          $src = imagecreatefromgif($dir);
          return $src;
          break;
      case 'image/png':
          $src = imagecreatefrompng($dir);
          return $src;
          break;
      default:
          return FALSE;
          break;
  }
  $new_img = imagecreatetruecolor($new_img_w, $new_img_h);
  imagecopyresampled($new_img, $src, 0, 0, 0, 0, $new_img_w, $new_img_h, $max_w, $max_h);
  switch ($img_mime_type['mime']) {
      case 'image/jpeg':
      case 'image/pjpeg':
          imagejpeg($new_img, $dir, 100);
          break;
      case 'image/gif':
          imagegif($new_img, $dir);
          break;
      case 'image/png':
          imagepng($new_img, $dir);
          break;
      default:
          return FALSE;
          break;
  }
  
  imagedestroy($src);
  imagedestroy($new_img);
      }
      ?&gt;
  </code></pre>