我有这个代码,用户输入上传图像到我的页面然后它显示给他。那部分工作正常,但是当我想用他的图像制作缩略图时,它就不会显示出来。这是将图像转换为.jpg,然后是缩略图:
<?php
// connect to database
$link = mysqli_connect("localhost", "root", "iyadmajid", "moviesite");
if (!$link) {
echo "Connection lost: " . mysqli_connect_error();
}
//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$file = $_FILES['image_filename'];
$image_tempname = $file['name'];
$today = date("Y-m-d");
// upload image and check for image type
$ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images/";
$ImageThumb = $ImageDir . "thumbs/";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($file['tmp_name'], $ImageName)) {
// get info about the imag ebeing uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);
if ($type > 3) {
// image isnt accepted
echo "Sorry, but the file you uploaded was not a GIF, JPG, or
PNG";
echo "Please hit your browser's 'back' button and try again.";
} else {
// image is accepted
$lastpic = uniqid('', true) ;
$pic = explode(".", $lastpic);
$lastpic = $pic[0];
// insert info into images
$insert = "INSERT INTO image
(`image_caption`, `image_username`, `image_date`,
`image_code`)
VALUES
('$image_caption', '$image_username', '$today',
'$lastpic')";
$insertresults = mysqli_query($link, $insert);
$picid = mysqli_insert_id($link);
$newfilename = $ImageDir . $lastpic . ".jpg";
if ($type == 2) {
rename($ImageName, $newfilename);
} else {
if ($type == 1) {
$image_old = imagecreatefromgif($ImageName);
} elseif ($type == 3) {
$image_old = imagecreatefrompng($ImageName);
// 'convert' the image to jpg
$image_jpg = imagecreatetruecolor($width, $height);
imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width,
$height, $width, $height);
imagejpeg($image_jpg, $newfilename);
imagedestroy($image_old);
imagedestroy($image_jpg);
}
$newthumbname = $ImageThumb . $image_code . ".jpg";
//get dimensions for the thumbnail
$thumb_width = $width * 0.10;
$thumb_height = $height * 0.10;
//create thumbnail
$largeimage = imagecreatefromjpeg($newfilename);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, $thumb_width,
$thumb_height, $width, $height);
imagejpeg($thumb, $newthumbname);
imagedestroy($largeimage);
imagedestroy($thumb);
}
$url = "Location:showimage.php?id=" . $picid;
header($url);
}
}
?>
以下是应显示缩略图的代码:
<?php
// connect to database
$link = mysqli_connect("localhost", "root", "iyadmajid", "moviesite");
if (!$link) {
die ("Connection lost: " . mysqli_connect_error($link));
}
$ImageDir = "../chapter7/images/";
$ImageThumb = $ImageDir . "thumbs/";
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to our Photo Gallery</title>
</head>
<body>
<p align="center">Click on any image to see it full sized</p>
<table align="center">
<tr>
<td align="center">Image</td>
<td align="center">Caption</td>
<td align="center">Uploaded</td>
<td align="center">Date Uploaded</td>
</tr>
<?php
//get the thumbs
$getpic = mysqli_query($link, "SELECT * FROM image");
if (!$getpic) {
die ("Invalid query: " . mysqli_error($link));
}
while ($row = mysqli_fetch_array($getpic)) {
extract($row);
echo "<tr>\n";
echo "<td><a href='" . $ImageDir . $image_code . ".jpg>'";
echo "<img src='" . $ImageThumb . $image_code . ".jpg'
border='0'>";
echo "<td>" . $image_caption . "</td>\n";
echo "<td>" . $image_username . "</td>\n";
echo "<td>" . $image_date . "</td>\n";
echo "</tr>\n";
}
?>
</table>
</body>
</html>
Here is what the page looks like. It should have the thumbnail in the 'image' column
非常感谢所有帮助。谢谢!