I have a method to save an image, which is meant to deal gracefully with an error, setting $imageSrc to a particular image in the event of failure. My method works fine if the image is present, but no error conditions seems to be handled correctly.
$imageSrc = save_pic($PIC_URL, $pk); function save_pic($pic_url, $pk) { $imageDir = './'; if (!strlen($pic_url)) return 'removed.jpg'; if (!is_dir($imageDir) || !is_writable($imageDir)) { return 'removed.jpg'; } $image = file_get_contents($pic_url); if (empty($image)) { return 'removed.jpg'; } $r = file_put_contents($imageDir.$pk.'.jpg', $image); if ($r) { return './$pk.jpg'; } else { return 'removed.jpg'; } }
If the image does not exist, I get :
Warning: getimagesize(http://127.0.0.1/555.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\Program Files\EasyPHP 2.0b1\www\get_auction.php on line 144 Array ( [type] => 2 [message] => getimagesize(http://127.0.0.1/555.jpg) function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found [file] => C:\Program Files\EasyPHP 2.0b1\www\get_auction.php [line] => 144 ) 1
returned.jpg is never returned in any event.
edit: added imageResize code:
function imageResize($imageURL, $maxWidth, $maxHeight) { global $outputWidth, $outputHeight, $maxWidth, $maxHeight; $size = getimagesize($imageURL); if ($size) { $imageWidth = $size[0]; $imageHeight = $size[1]; $wRatio = $imageWidth / $maxWidth; $hRatio = $imageHeight / $maxHeight; $maxRatio = max($wRatio, $hRatio); if ($maxRatio > 1) { $outputWidth = $imageWidth / $maxRatio; $outputHeight = $imageHeight / $maxRatio; } else { $outputWidth = $imageWidth; $outputHeight = $imageHeight; } } else { die(print_r(error_get_last())); } }
The problem was not a code issue, but was caused by file corruption. This was determined while testing on other machines.