doulin1867 2012-07-07 09:41
浏览 49
已采纳

如何在上传后检测被破坏或崩溃的图像

How i can detect spoiled images after upload?

Im using some code like 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 some time i give 2 error tandem,

  1. at lines that have comment // Error 1

imagecreatefromwbmp() [href='function.imagecreatefromwbmp'>function.imagecreatefromwbmp]: >'images/userphoto/4ff7db9800871.bmp' is not a valid WBMP file

imagecreatefromgif() [href='function.imagecreatefromgif'>function.imagecreatefromgif]: >'images/usersphoto/4fe70bb390758' is not a valid GIF file

  1. 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?
  • 写回答

1条回答 默认 最新

  • drqj8605 2012-07-07 09:55
    关注

    Before you do any further processing, you should check the return value of getimagesize() .

    If getimagesize() returns a FALSE, that means it has failed and something is wrong with the file, so you should terminate. Once the image passes this check, you can continue with your processing.

    As for why you are getting supplied argument is not a valid Image resource, the explaination is simple: Most likely, the image stored in $userphoto is actually FALSE because imagecreatefrompng() and other imagecreatefrom functions have failed, returning a FALSE.

    Solution: Check that the uploaded image is valid with getimagesize() before continuing.

    Some possible reasons as to why the uploads are failing:

    • Something is corrupting the images when they are written to the temp directory.
    • User is uploading corrupted files.

    Regarding imagecreatefromwbmp() failing, it is because gd does not support BMP files. You need to convert the BMP into a format gd can work with. For example, you can use bmp2png.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳