Php 调整图像大小(don';不适用于所有图像

Php 调整图像大小(don';不适用于所有图像,php,image,gd,Php,Image,Gd,当我调整图片大小并将其转换为jpeg/jpg时,它并不适用于每个PNG和jpg文件,我不知道为什么?文件未上载到服务器,但路径信息存储到数据库中。 这是调整大小的代码(同样对于png,我用白色代替透明填充)。我使用的是GD模块而不是Imagick,因为服务器上没有安装Imagick function imagecreatefromfile( $filename ) { if (!file_exists($filename)) { throw new InvalidArgu

当我调整图片大小并将其转换为jpeg/jpg时,它并不适用于每个PNG和jpg文件,我不知道为什么?文件未上载到服务器,但路径信息存储到数据库中。 这是调整大小的代码(同样对于png,我用白色代替透明填充)。我使用的是GD模块而不是Imagick,因为服务器上没有安装Imagick

function imagecreatefromfile( $filename ) {
    if (!file_exists($filename)) {
        throw new InvalidArgumentException('File "'.$filename.'" not found.');
    }
    switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
        case 'jpeg':
        case 'jpg':
            return imagecreatefromjpeg($filename);
        break;

        case 'png':
            return imagecreatefrompng($filename);
        break;

        default:
            throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg or png.');
        break;
    }
}
/* do resizing of picture */    
$src = imagecreatefromfile($target);
$path1 = strtolower( pathinfo( $target, PATHINFO_EXTENSION ));
list ($width, $height) = getimagesize($target);
$newwidth = 300;
if($heigth > $width){
  $newheight = ($height/$width)*$newwidth;
}
else if($width == $height){
  $newheight = 300;     
}
else if($width > $height){
  $newheight = ($width/$height)*$newwidth;
}

unlink($target);

switch ($path1)
{
    case 'jpg':
        $tmp = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($tmp, $target, 90);
    imagedestroy($src);
    imagedestroy($tmp);
    break;
    case 'png':
        $backgroundImg = imagecreatetruecolor($width, $height);
    $color = imagecolorallocate($backgroundImg, 255, 255, 255);
    imagefill($backgroundImg, 0, 0, $color);
    imagecopy($backgroundImg, $src, 0, 0, 0, 0, $width, $height);
    $tmp2 = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($tmp2, $backgroundImg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($tmp2, $target, 90);
    imagedestroy($src);
    imagedestroy($backgroundImg);
    imagedestroy($tmp2);
        break;
}
?>