php - How to detect spoiled or crashed images after upload -
how can detect spoiled images after upload?
im using code this:
$imagesize = getimagesize($tmp_name); if(!$imagesize || !in_array($imagesize['mime'], $allowmimetype)){ $this->error = 'bad image'; @unlink($tmp_name); return false; } $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 time give 2 error tandem,
- at lines have comment
// error 1
imagecreatefromwbmp() [href='function.imagecreatefromwbmp'>function.imagecreatefromwbmp]: >'images/userphoto/4ff7db9800871.bmp' not valid wbmp file
imagecreatefromgif() [href='function.imagecreatefromgif'>function.imagecreatefromgif]: >'images/usersphoto/4fe70bb390758' not valid gif file
- at line of comment
// error 2
imagecopyresampled(): supplied argument not valid image resource
before further processing, should check return value of getimagesize() .
if getimagesize()
returns false
, means has failed , wrong file, should terminate. once image passes check, can continue processing.
as why getting supplied argument not valid image resource
, explaination simple: likely, image stored in $userphoto
false
because imagecreatefrompng()
, other imagecreatefrom
functions have failed, returning false
.
solution: check uploaded image valid getimagesize()
before continuing.
some possible reasons why uploads failing:
- something corrupting images when written temp directory.
- user uploading corrupted files.
regarding imagecreatefromwbmp()
failing, because gd not support bmp files. need convert bmp format gd can work with. example, can use bmp2png.
Comments
Post a Comment