我将用户上传的图像重新调整为特定尺寸(即特定宽度和高度)。我想制作尺寸 940 px * 370 px 的上传图片。但在这样做的同时,我并不想影响用户最初上传的图像质量。
我的代码所面临的问题是图像尺寸已缩小到指定尺寸,但图像质量受到严重影响。上传的图片正在缩小。
我没有得到如何保持上传图片质量的完整性
为了实现此功能,我已经编写了以下代码。
HTML代码:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP代码:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 5242880)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
//Store the name of the temporary copy of the file stored on the server
$images = $_FILES["file"]["tmp_name"];
/*Create a new file name for uploaded image file :
*prepend the string "upload" to it's original file name
*/
$new_images = "upload".$_FILES["file"]["name"];
//Copies a file contents from one file to another
//copy($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
$width = 940;
//Determine the size of a given image file and return the dimensions along with the file type
$size=GetimageSize($images);
//$height=round($width*$size[1]/$size[0]);
$height = 370;
//Create a new image from file or URL & returns an image identifier representing the image obtained from the given filename.
$images_orig = ImageCreateFromJPEG($images);
//Get image width of originally uploaded image
$photoX = ImagesX($images_orig);
//Get image height of originally uploaded image
$photoY = ImagesY($images_orig);
//Create a new true color image & returns an image identifier representing a black image of the specified size.
$images_fin = ImageCreateTrueColor($width, $height);
/*Copy and resize part of an image with resampling
*copies a rectangular portion of one image to another image,
*smoothly interpolating pixel values so that, in particular,
*reducing the size of an image still retains a great deal of clarity.
*/
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
/*Output image to browser or file
*creates a JPEG file from the given image.
*/
ImageJPEG($images_fin,"upload/".$new_images);
/*Destroy an image
*frees any memory associated with image image.
*/
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
如果有人能在这方面帮助我,对我来说会非常有帮助。
提前致谢。
为了更清楚地了解我面临的问题,我正在使用PHP代码附加原始图像和修改后的图像:
原始图片如下:
上传服务器后的图像(即受影响的图像)如下:
答案 0 :(得分:1)
您应该缩放图像以保持纵横比,而不是始终将图像缩放到940x370。你可以这样做:
$scaleX = $width / $photoX;
$scaleY = $height / $photoY;
$scale = min($scaleX, $scaleY);
$scaleW = $scale * $photoX;
$scaleH = $scale * $photoY;
$images_fin = ImageCreateTrueColor($width, $height);
$background = ImageColorAllocate($images_fin, 0, 0, 0);
ImageFill($images_fin, 0, 0, $background);
ImageCopyResampled($images_fin, $images_orig, $width / 2 - $scaleW / 2, $height / 2 - $scaleH / 2, 0, 0, $scaleW+1, $scaleH+1, $photoX, $photoY);
这将确保生成的图像始终适合940x370尺寸。图像的其余部分(如果纵横比不与940匹配,则为370)将用黑色填充。