如何在php上传后显示生成的图像?

时间:2016-11-04 16:09:16

标签: php forms upload image-uploading

我正在尝试显示已上传的图片,并且在使用此代码处理后它是网址但是我有点卡在我如何实现这一点而不是使用“完成...”返回的页面

http://llngg6czd-site.1tempurl.com/tester/index.php

的index.php

<!DOCTYPE html>
<html>
<head>
<title>Upload Files using normal form and PHP</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload_image.php">
<div class="row">
<label for="image">Select a File to Upload</label><br />
<input type="file" name="image" /> 
</div>
<div class="row">
<input type="submit" value="Upload" />
</div>
</form>
</body>
</html>

image_upload.php

<?php
require_once('ImageManipulator.php');
if ($_FILES['image']['error'] > 0) {
echo "Error: " . $_FILES['image']['error'] . "<br />";
} else {
// array of valid extensions
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
// get extension of the uploaded file
$fileExtension = strrchr($_FILES['image']['name'], ".");
// check if file Extension is on the list of allowed ones
if (in_array($fileExtension, $validExtensions)) {
$newNamePrefix = time() . '_';
$manipulator = new ImageManipulator($_FILES['image']['tmp_name']);
// resizing to 200x200
$newImage = $manipulator->resample(200, 200);
// saving file to uploads folder
$manipulator->save('uploads/' . $newNamePrefix . $_FILES['image']['name']);
echo 'Done ...';
} else {
echo 'You must upload an image...';
}
}

Imagemanipulator.php

https://gist.github.com/philBrown/880506

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您将文件另存为

'uploads/' . $newNamePrefix . $_FILES['image']['name']

因此,你可以通过给出带有'src'属性的IMG-Tag来显示它:

echo '<img src="./uploads/' . $newNamePrefix . $_FILES['image']['name'] . '">';