enter code here
这是我在 Stack Overflow 上的第一篇文章,所以如果我太含糊,请原谅我,并且没有给你必要的信息来立即诊断我的问题!
我创建了一个使用表单上传图像的临时页面。然后表单发布到一个 php 页面,该页面重新采样图像并创建一个新图像,调整大小并裁剪。
我遇到的问题是,在图像上传到的 php 页面上,所有 PHP 代码都正确发布并按我预期的方式运行,但是所有 HTML 代码都显示在浏览器中而没有格式化。我不知道为什么,我以前从未遇到过这个问题。
这是上传表单页面的代码:
<! Image test: Testing the ability to upload an image, resize and crop. >
<! Idealy, this procedure would be performed on large images. >
<! Car Listing page images: 280 x 200 px >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php require_once("baseopen.php"); ?>
<?php
if (isset($_POST['error1'])) {
$error = "The file selected was not of the correct filetype.<br />
Please select a different file to upload.";
}
?>
<html>
<?php if (isset($error)) { echo $error; } ?>
<p>Select an image of type <b>.JPG, .GIF, .PNG, or .BMP</b> to upload.</p><br />
<form action="converter.php" method="post" enctype="multipart/form-data">
<label for="image">Picture:</label>
<input type="file" name="image"><br />
<input type="submit" name="submit" value="Upload">
</form>
</html>
<?php mysql_close($data); ?>
这是我处理 PHP 时遇到的问题并输出所有 HTML 标记的内容。(请注意,PHP 已处理,不会显示在浏览器中。)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$file = $_FILES['image']['name'];
$servfile = $_FILES['image']['tmp_name'];
echo "\$File: " . $file . "<br />";
echo "\$servfile: " . $servfile . "<br /><br />";
$filename = explode(".", $file);
echo $filename[0] . "<br />";
echo $filename[1] . "<br />";
$filename[1] = strtolower($filename[1]);
/* Begin execution once file extension is confirmed. */
if ($filename[1] == "jpg" || $filename[1] == "gif" || $filename[1] == "png" || $filename[1] == "bmp") {
echo "Successful recognition! <br />";
crop($servfile, $filename[1], "280", "200");
}
/* Send back to main page with an error code. */
else {
echo "Error, dude!";
}
}
function crop($filename, $type, $width, $height) {
/* Create a new image from the proper filetype. */
if ($type == "jpg") { $resource = imagecreatefromjpeg($filename); }
if ($type == "gif") { $resource = imagecreatefromgif($filename); }
if ($type == "png") { $resource = imagecreatefrompng($filename); }
if ($type == "bmp") { $resource = imagecreatefrombmp($filename); }
echo "Width is: " . imagesx($resource) . "<br />";
echo "Height is: " . imagesy($resource) . "<br />";
echo "Desired width is: " . $width . "<br />";
echo "Desired height is: " . $height . "<br />";
/* Check to make sure image is larger than needed size. */
if ( ($width < imagesx($resource)) && ($height < imagesy($resource))) {
echo "We can do this! <br />";
$new = imagecreatetruecolor($width, $height);
imagecopyresampled($new, $resource,
0, 0, 0, 0, $width, $height, imagesx($resource), imagesy($resource));
header('Content-type: image/jpeg');
imagejpeg($new, "temp.jpg", 100);
echo "<img src=\"temp.jpg\">";
}
/* Return with error that image is too small! */
else { echo "Nope, not big enough."; }
}
?>
</html>
当前使用运行 PHP 5.3.5 的 WAMPSERVER。
测试服务器运行所有 PHP 页面都没有错误,除了我已经奋斗了几个星期的这个页面。
添加:在尝试上传名为“8.jpg”的图像时,Internet Explorer 中的输出是这样的。这是converter.php的实际视图输出:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> $File: 8.jpg<br />$servfile: C:\wamp\tmp\php82F0.tmp<br /><br />8<br />jpg<br />Successful recognition! <br />Width is: 1680<br />Height is: 1050<br />Desired width is: 280<br />Desired height is: 200<br />We can do this! <br /><img src="temp.jpg"></html>
任何帮助将不胜感激,谢谢。