enter code hereThis is my first post on Stack Overflow, so please forgive me if I am too vague and do not give you the necessary information to diagnose my problem right away!
I have created a temporary page that uses a form to upload an image. The form then posts to a php page that resamples the image and creates a new image, resized and cropped.
The problem I have is that on the php page that the image uploads to, all of the PHP code posts properly and functions as I expect it to, BUT all of the HTML code displays in the browser without formatting. I am not sure why, I have never come into this problem before.
Here is the code for the upload form page:
<! 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); ?>
Here is what I have for the processing PHP that is giving me the problem and outputting all the HTML tags. (Please note, the PHP IS processed and NOT displayed in the browser.)
<!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>
Currently using WAMPSERVER running PHP 5.3.5.
The test server runs all PHP pages with no errors, except for this one that I have been battling for a few weeks now.
ADDED: The output in Internet Explorer is this when trying to upload an image named “8.jpg.” This is the actual view output by 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>
Any help would be greatly appreciated, thank you.
There seems to be a problem when submitting the image for upload. This was either caused by the enctype in the form, or by the converter.php which was creating a header for mime_type of image/jpeg.
Either way, after trying multiple browsers the code would never work, and Chrome would show the page as a broken image.
I parsed the code and made converter.php an exclusive processing script with no output. This script then redirected to a new PHP page I sharply named ServedUp.php. The result was shown properly with newly resized images.
I am unsure if this conflict was created in the script, or created somewhere in translation on the server side. Needless to say, the problem is now fixed.
Thank you for everyone’s thoughts and opinions. I will be sure to use Stack Overflow again soon when another problem rises!