0

这是我的页面。我的问题是为什么当我上传图片时表单返回空白页。在我添加最后一个 switch 语句之前它工作得很好。创建图像的那个。(imagejpeg 函数)当我没有 switch 语句时它可以工作。但在我添加 switch 语句后,表单消失了。所以我的问题是,我在 switch 语句中做错了什么?还是总共有代码?

    $imageFile = $_FILES['image']['tmp_name']; 
    $filename = basename( $_FILES['image']['name']);

    list($width, $height) = getimagesize($imageFile);
    $picture = getimagesize($imageFile);

    if ($width >= $height) 
    {
        $orig_w = 500;
        $orig_h =($height/$width)*$orig_w;
    } else
    {
        $orig_h = 500;
        $orig_w = ($width/$height)*$orig_h;
    }

    $picture['format'] = strtolower(preg_replace('/^.*?\//', '', $picture['mime']));

    switch( $picture['format'] ) {
    case 'jpg':
    case 'jpeg': 
        $src = imagecreatefromjpeg($imageFile);
    break;
    case 'png':
        $src = imagecreatefrompng($imageFile);
    break;
    case 'gif':
        $src = imagecreatefromgif($imageFile);
    break;
    default:
        echo "unsproted format";
    break;
}

    $tmp = imagecreatetruecolor($orig_w, $orig_h);
    imagecopyresampled($tmp, $src , 0,0,0,0, $orig_w,$orig_h, $width, $height);

    switch( $picture['format'] ) {
        case 'jpg':
        case 'jpeg':
            return imagejpeg($tmp, $folder.$filename, 100);
        break;
        case 'png':
            return imagepng($tmp, $folder.$filename);
        break;
        case 'gif':
            return imagegif($tmp, $folder.$filename);
        break;
        default:
            echo "unsproted format";
        break;
    }

    imagedestroy($tmp);
    imagedestroy($src);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<h1 > php image upload form </h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <p>
        <label for="image" > Image: </label>
        <input type="file" name="image" id="image" /> <br/>
    </p>
    <p>
        <input type="submit" name="submit" value="upload image" />
    </p>
</form>
</body>
</html>
4

1 回答 1

0

我做到了。显然我无法弄清楚 switch 语句有什么问题(我仍然是 php 中的菜鸟)呵呵.. 但将 switch 语句替换为:

$xtention = explode(".",$filename); 

if ($xtention[1] == 'jpg' || $xtention[1] == 'jpeg' ) {
        imagejpeg($tmp, $folder.$filename, 100);
    }
    elseif ($xtention[1] == 'png') {
        imagepng($tmp, $folder.$filename);
    }
    elseif ($xtention[1] == 'gif') {
        imagegif($tmp, $folder.$filename);
    }
    else {
        echo "unsproted format";
    }
于 2012-11-05T07:08:13.793 回答