显示通过Web表单上载的图像,而不存储在文件夹或数据库中

时间:2015-02-12 20:47:15

标签: php file header

我有一个项目需要网站访问者填写表格,其中一部分是文件上传。我希望客户能够在点击按钮后查看图片;目前,数据不存储在任何文件夹或数据库中;我只想要一个页面来加载表单数据。我的代码不起作用:当我点击按钮时会弹出一个对话框询问我想用php文件做什么。选项iclude:用firefox打开,保存等。请问,我哪里错了? 这是processphoto_form.php

的过程
<?php
if(isset($_FILES['userFoto']['name']) && $_FILES['size']<=1000000){
$tempName = $_FILES['userFoto']['temp_name'];
$fhand = fopen($tempName, 'r');
$userFoto = fread($fhand, filesize($tempName));
$userFoto = addslashes($userFoto);
fclose($fhand);
$user = $_POST['user'];
header('Content-type:$userFoto/JPEG');
echo"<img scr = '$userFoto' height ='100' width = '100'/>"."<br/>";
cho$user;
}

?>

HTML

<form enctype = 'multipart/form-data' method ='post' action ='processphoto_form.php'>
<table>
<tr><td>Name</td><td><input type = 'text' name = 'user'/></td></tr>
<tr><td>Foto</td><td><input type = 'file' name = 'userFoto'/></td></tr>
<tr><td colspan ='2' ><input type = 'submit' value = 'VIEW DATA'/></td></tr>
</table>
</form>

1 个答案:

答案 0 :(得分:0)

如果你确定上传的图片是jpeg并且它是正方形的,你可以执行以下操作:

    $tempName = $_FILES['userFoto']['temp_name'];    
    list($originalWidth, $originalHeight) = getimagesize($tempName);
    $src = imagecreatefromjpeg($tempName);
    $img = imagecreatetruecolor(100, 100);
    ImageCopyResampled($img, $src, 0, 0, 0, 0, 100, 100, $originalWidth, $originalHeight);
    header('Content-Type: image/jpeg');
    ob_start();
    imagejpeg($img);
    $size = ob_get_length();
    header("Content-Length: " . $size);
    ob_end_flush();
    imagedestroy($img);
    exit;