dongwen5351
2016-11-04 16:09
浏览 44
已采纳

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

I'm trying to display the uploaded image and it's url after it has been processed using this code but I'm a bit stuck on how I would achieve this instead of the page returning with "done..."

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

Any help would be greatly appreciated.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan5991518 2016-11-04 16:14
    已采纳

    You save the file as

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

    Therefor you can simply show it by giving back an IMG-Tag with the 'src' attribute:

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

相关推荐 更多相似问题