title: Speeding up Image Resizing
body: <p>Problem: Script seems to be running slow. This script is inside a function that is run four times for different image sizes. Is there any way to speed up the code below?</p>
  
  <pre><code>$outputFile = "../data/assets/temp.jpg";
  $maxTempWidth  = 45;
  $maxTempHeight = 45;
  $image_info = getimagesize($setXsmallNewName);
  
  if($image_info['mime'] == 'image/jpeg'){
  $image = imagecreatefromjpeg($setXsmallNewName);
  }elseif($image_info['mime'] == 'image/gif'){
  $image = imagecreatefromgif($setXsmallNewName);
  }elseif($image_info['mime'] == 'image/png'||$image_info['mime'] == 'image/x-png'){
      $image = imagecreatefrompng($setXsmallNewName);
  }
  
  $width = imagesx( $image );
  $height = imagesy( $image );
  
  if ($width &gt; $maxTempWidth || $height &gt; $maxTempHeight){   
      if ( $width &gt; $height ){
          $newwidth = $maxTempWidth;
          $ratio = $maxTempWidth / $width;
          $newheight = floor($height * $ratio);
  
          if ($newheight &gt; $maxTempHeight){
              $newheight = $maxTempHeight;
              $ratio = $maxTempHeight / $height;
              $newWidth = floor($width * $ratio);
          }
      }else{
          $newheight = $maxTempHeight;
          $ratio = $maxTempHeight / $height;
          $newwidth = floor($width * $ratio);
  
          if ($newwidth &gt; $maxTempWidth){
              $newwidth = $maxTempWidth;
              $ratio = $maxTempWidth / $width;
              $newheight = floor($height * $ratio);
          }
      }
  }else{
      $newwidth = $width;
      $newheight = $height;
  }   
  $final_image = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresampled($final_image, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  </code></pre>