title: PHP image resize with GD use to many CPU time
body: <p>I have a script on PHP GD with simple functions how to resize on real-time images on my site, but this process "eat" all my CPU time ... I use VPS with 4 cores and it's not enough.</p>
  
  <p>From image tag i call PHP file (<code>&lt;img src="image.php?file=XXX&amp;width=XXX&amp;height=XXX" alt="" /&gt;</code>) with image name, new width and new height ... after that in PHP I make this operations:</p>
  
  <pre><code>ob_start();
  
  ini_set('max_execution_time', 0);
  set_time_limit(0);
  
  error_reporting(E_ALL^E_NOTICE^E_DEPRECATED);
  date_default_timezone_set('Europe/Sofia');
  
  header('Content-type: image/jpeg');
  
      $url = $_GET['file'];
  
      if(!empty($url) &amp;&amp; file_exists($url) &amp;&amp; is_file($url) &amp;&amp; is_readable($url)){
          $width = $_GET['width'];
          $height = $_GET['height'];
  
          list($width_orig, $height_orig) = getimagesize($url);
  
          if(empty($width)) $width = $width_orig/($height_orig/$height);
          if(empty($height)) $height = $height_orig/($width_orig/$width);
  
          $ratio_orig = $width_orig/$height_orig;
  
          if($width/$height &gt; $ratio_orig) {
              $width = $height*$ratio_orig;
          } else {
              $height = $width/$ratio_orig;
          }
  
          $image_p = imagecreatetruecolor($width, $height);
          $image = imagecreatefromjpeg($url);
  
          imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
          imagejpeg($image_p, null, 100);
          imagedestroy($image_p);
          imagedestroy($image);
      }
  
      ob_end_flush();
  </code></pre>
  
  <p>I don't understand why "eat" to match CPU time ... my original image size it's 1920x2880 px.
  How to reduce my CPU usage? Any ideas?</p>