如何使用PHP以最佳方式将用户上传的图像重新调整为特定尺寸(不影响上传图像的质量)


How to re-size the image to specific dimensions(without compromising the quality of uploaded image) uploaded by user in optimum way using PHP?

我的网站使用PHP、jQuery、AJAX、HTML、等。我是PHP的新手。

我在我的WebApp中使用了一个jQuery图像滑块。对于此图像滑块,用户上载图像。此外,这些上传的图像被显示在该图像滑块中。这个功能对我来说很好。

此图像滑块的固定宽度和高度为940像素*370像素。我想允许用户上传尺寸应大于或等于940像素*370像素的图像。

假设用户上传的图像尺寸为2215像素*875像素,则在上传到服务器之前(即在将图像保存到服务器之前),该图像的大小应重新调整为940像素*370像素40 px*370 px后,才应将其保存到服务器。

在重新调整大小的过程中,图像质量不得受到影响。重新调整大小的图像应该看起来像用户上传的原始图像。它不应该在任何意义上收缩或拉伸。

我应该如何通过使用PHP GD和图像函数以最佳方式实现此功能?

为了你的参考,我把我尝试的文件上传功能的代码放在下面:

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);
      $image_dimesnions = getimagesize($_FILES['file']['tmp_name']);
      $image_width = $image_dimesnions[0];
      $image_height = $image_dimesnions[1];
  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)
    && ($image_width < 940 || $image_height < 370)
    && 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"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
      }
    } else {
      echo "Invalid file";
    }
?>

您可以在服务器上使用imagemagik命令行工具或使用PHP GD库来完成此操作。

ImageMagick的更多信息链接-

  1. 关于ImageMagick
  2. 命令行选项
  3. 调整大小

PHP GD库的信息链接-

  1. PHP GD
  2. 从jpeg创建图像
  3. 图像创建自png
  4. 图像从gif创建

使用以下命令转换图像-

$w = 940;
$h = 370;
$temp_path = $_FILES["file"]["tmp_name"];
$dest_path = "upload/" . $_FILES["file"]["name"];
shell_exec("/usr/bin/convert -resize '"$w"."X".$h.">'"  $dest_path  $temp_path");

使用以下代码为PHP GD-

            <?php
$uploadPath = "/var/www/stack/26201855/upload/";
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$imageType = "";
if(strpos($_FILES["file"]["type"],'jpeg') || strpos($_FILES["file"]["type"],'jpg') || strpos($_FILES["file"]["type"],'pjpeg')){
$imageType = "jpeg";
}else if(strpos($_FILES["file"]["type"],'png') || strpos($_FILES["file"]["type"],'x-png')){
$imageType = "png";
}else if(strpos($_FILES["file"]["type"],'gif')){
$imageType = "gif";
}
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"] < 200000)
&& 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($uploadPath . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
$images = $_FILES["file"]["tmp_name"];
$new_images = $uploadPath.$_FILES["file"]["name"];
copy($images,$new_images);
$width=940;
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if($imageType === 'jpeg'){
$images_orig = imagecreatefromjpeg($images);
}else if($imageType === 'png'){
$images_orig = imagecreatefrompng($images);
}else if($imageType === 'gif'){
$images_orig = imagecreatefromgif($images);
}
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
if($imageType === 'jpeg'){
imagejpeg($images_fin,$new_images);
}else if($imageType === 'png'){
imagepng($images_fin,$new_images);
}else if($imageType === 'gif'){
imagegif($images_fin,$new_images);
}

ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Stored in: " . $uploadPath. $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
} 
?>

注意:您必须自己决定调整图像大小的方便方法。