我必须调整上传的图像upload.image,但是当我们尝试调整它的大小时,显示一个调整了高度和宽度的黑屏。我使用下面的代码来调整大小
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension ';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo "You have exceeded the size limit";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
$filename = "images/". $_FILES['file']['name'];
$filename1 = "images/small". $_FILES['file']['name'];
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
请帮我调整图片大小
答案 0 :(得分:1)
我已经进行了让它在我的WAMP服务器上运行所需的更改。 这些更改在代码中进行了注释。
<form action="upload.php" method="post" enctype="multipart/form-data">
<h1><b>Uploader stub</b></h1>
<input type="file" name="filename"><br>
<input type="hidden" name="action" value="upload">
<input type="submit" value="Upload File">
</form>
<?php
define("MAX_SIZE", 200); // Missing constant
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["filename"]["name"]; // The _FILES var does not contain an array named file, I have changed that to filename
$uploadedfile = $_FILES['filename']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['filename']['name']);
//$extension = getExtension($filename); // Use PHP built in path parser.
$path_parts = pathinfo($filename);
$extension = $path_parts['extension'];
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
echo ' Unknown Image extension ';
$errors = 1;
}
else {
$size=filesize($_FILES['filename']['tmp_name']);
if ($size > MAX_SIZE * 1024) {
echo "You have exceeded the size limit";
$errors = 1;
}
if($extension == "jpg" || $extension == "jpeg" ) {
//$uploadedfile = $_FILES['filename']['tmp_name']; // Not needed
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension == "png") {
//$uploadedfile = $_FILES['filename']['tmp_name']; // Not needed
$src = imagecreatefrompng($uploadedfile);
}
else {
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height) = getimagesize($uploadedfile);
$newwidth = 60;
$newheight =($height/$width)*$newwidth;
$tmp = imagecreatetruecolor($newwidth,$newheight);
$newwidth1 = 25;
$newheight1 = ($height/$width)*$newwidth1;
$tmp1 = imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight,
$width,$height);
imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newwidth1,$newheight1,
$width,$height);
$filename = "images/". $_FILES['filename']['name'];
$filename1 = "images/small/". $_FILES['filename']['name'];
imagejpeg($tmp,$filename, 100);
imagejpeg($tmp1,$filename1, 100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}