为什么以下代码返回空白页但仍然上传照片

时间:2012-11-05 06:51:08

标签: php html forms

继承我的页面。我的问题是当我上传图片时表单返回空白页面的原因。它工作正常,直到我添加了最后一个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>

1 个答案:

答案 0 :(得分: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";
    }