Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php simpleImage类捕获异常错误_Php - Fatal编程技术网

php simpleImage类捕获异常错误

php simpleImage类捕获异常错误,php,Php,加载图像时SimpleImage类未捕获异常代码如下: public function load($filename) { //echo $filename; exit; $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if ($this->image_type == IMAGETYPE_JPEG) { $this->i

加载图像时SimpleImage类未捕获异常代码如下:

public function load($filename) {
        //echo $filename; exit;
    $image_info = getimagesize($filename);
    $this->image_type = $image_info[2];

    if ($this->image_type == IMAGETYPE_JPEG) {
        $this->image = imagecreatefromjpeg($filename);
    } elseif ($this->image_type == IMAGETYPE_GIF) {
        $this->image = imagecreatefromgif($filename);
    } elseif ($this->image_type == IMAGETYPE_PNG) {
        $this->image = imagecreatefrompng($filename);
    }
}

您没有捕获异常,如果异常不满足条件,则最后一次捕获异常。这是更新后的代码

public function load($filename) {
        //echo $filename; exit;
    $image_info = getimagesize($filename);
    $this->image_type = $image_info[2];

    if ($this->image_type == IMAGETYPE_JPEG) {
        $this->image = imagecreatefromjpeg($filename);
    } elseif ($this->image_type == IMAGETYPE_GIF) {
        $this->image = imagecreatefromgif($filename);
    } elseif ($this->image_type == IMAGETYPE_PNG) {
        $this->image = imagecreatefrompng($filename);
    } else {
        throw new Exception("The file you're trying to open is not supported");
    }
}

我认为这会解决问题。

检查
$image\u info
是否为数组。否则,如果文件名不是图像文件或无法作为图像读取,则可能会产生错误。