imagejpeg - failed to open stream
php
Solution
i had the same problem. it might be because you did not create folder that named 'cache'. (it seems that GD does not create directories and assumes that directory is created before and has required privileges(e.g 0777) .)
Problem
I use function resizeImage() to resize images but occasionally 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 ? Ideas: - Permissions to file - Bad path of image - Sessions - Permissions to write to dir Usage: ``` <?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); ``` Throws error: ``` 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 ``` Line #95: `imagejpeg($newImage, $image, 100);` Here is resizeImage() function: ``` <?php function resizeImage($image, $max_width, $max_height) { $img = getimagesize($image); $mime = $img['mime']; $width = $img[0]; $height = $img[1]; if ($height > $width) { $scale = $max_height / $height; } elseif ($width > $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; } ``` Edit: added var_dump before imagejpeg($newImage, $image, 100); // line #102 ``` //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 ```