Problem with php function to catch error on saving image
<p>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.</p>

<pre><code>$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";
    }
}
</code></pre>

<p>If the image does not exist, I get :</p>

<pre><code>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] =&gt; 2 [message] =&gt; getimagesize(http://127.0.0.1/555.jpg)
 function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 [file] =&gt; C:\Program Files\EasyPHP 2.0b1\www\get_auction.php
 [line] =&gt; 144
) 1
</code></pre>

<p>returned.jpg is never returned in any event.</p>

<p>edit: added imageResize code:</p>

<pre><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 &gt; 1) {

        $outputWidth = $imageWidth / $maxRatio;

        $outputHeight = $imageHeight / $maxRatio;

    } else {

        $outputWidth = $imageWidth;

        $outputHeight = $imageHeight;

    }

} else {

    die(print_r(error_get_last()));

}

}
</code></pre>
