php imagecreatefromjpeg无法正常工作

时间:2014-03-26 03:41:20

标签: php

我是PHP新手,所以这可能很简单。 我只想在上传图像并通过POST发送到脚本后显示图像。但我无法看到图像。

请找到以下代码: php文件的名称是test.php

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    if(isset($_FILES['photo'])
    && is_uploaded_file($_FILES['photo']['tmp_name'])
    && $_FILES['photo']['error'] == UPLOAD_ERR_OK){

        if ($_FILES['photo']['type']=='image/jpeg') {

            $tmp_img = $_FILES['photo']['tmp_name'];
            $image = imagecreatefromjpeg($tmp_img);
            header('Content-Type: image/jpeg');
            imagejpeg($image,'',90);
            imagedestroy($image); 

        }else{
            echo "Uploaded file was not JPEG","</br>";
        }   
    }else{
        echo "No file uploaded","</br>";

    }
}else{
    ?>
    <form action="test.php" method="post" enctype="multipart/form-data">
        <label for="photo">Photo : </label>
        <input type="file" name="photo"/>
        <input type="submit" value="Upload a JPEG photo"/>
    </form>

<?php } ?>

2 个答案:

答案 0 :(得分:2)

你有任何错误吗?

是否安装了GD? imagecreatefromjpeg是一种GD方法。

http://php.net/manual/en/image.installation.php

答案 1 :(得分:0)

你需要在这里传递NULL:

        imagejpeg($image,NULL,90);

来自Parameters下的文档:

filename

  The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

  To skip this argument in order to provide the quality parameter, use NULL.