title: imagejpeg - failed to open stream
body: <p>I use function resizeImage() to resize images but <strong>occasionally</strong> fails. Most time it is working but I have sometime this warnings that it can't find file. What should be the problem ? How to fix that problem ?</p>
  
  <p>Ideas:</p>
  
  <ol>
  <li><del>Permissions to file</del></li>
  <li>Bad path of image</li>
  <li><del>Sessions</del></li>
  <li><del>Permissions to write to dir</del></li>
  </ol>
  
  <p>Usage:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  define('DS', DIRECTORY_SEPARATOR);
  define('ROOT_DIR', realpath(dirname(__FILE__)) . DS);
  
  $image = ROOT_DIR . 'cache' . DS . $_SESSION['file_name'];
  resizeImage($image, 400, 400);
  </code></pre>
  
  <p>Throws <strong>error:</strong></p>
  
  <pre><code>Warning: imagejpeg(C:\wamp\www\mvc\cache\temp_1385972631.jpg): failed to open stream: Invalid argument in C:\wamp\www\mvc\application\helpers\image_helper.php on line 95
  </code></pre>
  
  <p>Line #95: <code>imagejpeg($newImage, $image, 100);</code></p>
  
  <p>Here is <strong>resizeImage()</strong> function:</p>
  
  <pre><code>&lt;?php
  
  function resizeImage($image, $max_width, $max_height) {
  
      $img = getimagesize($image);
  
      $mime = $img['mime'];
      $width = $img[0];
      $height = $img[1];
  
      if ($height &gt; $width) {
          $scale = $max_height / $height;
      }
      elseif ($width &gt; $height) {
          $scale = $max_width / $width;
      }
      else {
          $scale = 1;
      }
  
      $newImageWidth = ceil($width * $scale);
      $newImageHeight = ceil($height * $scale);
      $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
      imagealphablending($newImage, false);
      imagesavealpha($newImage, true);
      switch ($mime) {
          case "image/gif":
              $source = imagecreatefromgif($image);
              break;
          case "image/pjpeg":
          case "image/jpeg":
          case "image/jpg":
              $source = imagecreatefromjpeg($image);
              break;
          case "image/png":
          case "image/x-png":
              $source = imagecreatefrompng($image);
              break;
      }
      imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
  
      switch ($mime) {
          case "image/gif":
              imagegif($newImage, $image);
              break;
          case "image/pjpeg":
          case "image/jpeg":
          case "image/jpg":
              imagejpeg($newImage, $image, 100); // line #95
              break;
          case "image/png":
          case "image/x-png":
              imagepng($newImage, $image);
              break;
      }
  
      chmod($image, 0777);
      return $image;
  }
  </code></pre>
  
  <p><strong>Edit:</strong>
  added var_dump before imagejpeg($newImage, $image, 100); // line #102</p>
  
  <pre><code>//var_dump($newImage);
  resource(18) of type (gd)
  
  //var_dump($image);
  string(41) "C:\wamp\www\mvc\cache\temp_1385978306.jpg"
  
  Warning: imagejpeg(C:\wamp\www\mvc\cache\temp_1385978306.jpg): failed to open stream: Invalid argument in C:\wamp\www\mvc\application\helpers\image_helper.php on line 102
  </code></pre>