How to detect spoiled images after upload
===

How i can detect spoiled images after upload?

Im using some code like this:

    $imageSize = getimagesize($tmp_name);
    $tn = imagecreatetruecolor(80, 80);
    switch ($imageSize['mime']){
    	case 'image/jpeg':
    		$userphoto = imagecreatefromjpeg($tmp_name);// Error 1
    	break;
    	case 'image/png':
    		$userphoto = imagecreatefrompng($tmp_name);// Error 1
    	break;
    	case 'image/gif':
    		$userphoto = imagecreatefromgif($tmp_name);// Error 1
    	break;
    	case 'image/bmp':
    		$userphoto = imagecreatefromwbmp($tmp_name);// Error 1
    	break;
    	default:
    		$this->error = 'unknown image';
    		@unlink($tmp_name);
    		return false;
    	break;
    }
    imagecopyresampled($tn, $userphoto, 0, 0, 0, 0, 80, 80, $width, $height);// Error 2
    imagejpeg($tn,'images/userphoto/'.$this->username.'.jpg',100);
    chmod('images/userphoto/'.$this->username.'.jpg', 0777);
    @unlink($tmp_name);
    USERPROFILE::updatePhotoByUsersId($this->username.'.jpg', $this->users_id);

But some time i give 2 error tandem,

1. at lines that have comment `// Error 1` 
>imagecopyresampled(): supplied argument is not a valid Image resource


2. at line of comment `// Error 2`
>imagecopyresampled(): supplied argument is not a valid Image resource

I think its occurs because the file has damaged during uploading. If i'm right, how i can solve this problem? If not, What is the reason?
