在 PHP 中,如何在非正方形照片周围添加空白,使其始终为 200x200 像素?

In PHP, how do you add whitespace around a non-square photo, so that it is always 200x200 pixels?

下面的脚本可以很好地处理上传的图像并调整其大小,使最大高度或宽度(以较长者为准)为 200 像素。所以如果它是完美的正方形图像,它可能是 200x200,或者 200x140,或者 140x200,等等。

if(isset($_FILES['image'])) {
        $img = $_FILES['image']['name'];
        $tmp = $_FILES['image']['tmp_name'];

        // get uploaded file's extension
        $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

        //checking if image exists for this pool and removing if so, before adding new image in its place
        if(file_exists("uploads/".$poolid.".png")) {
         unlink("uploads/".$poolid.".png");
        }

        // checks valid format
        if(in_array($ext, $valid_extensions))  { 
        //re-size the image and make it a PNG before sending to server
        $final_image = $poolid . ".png";
        $path = "uploads/".strtolower($final_image); 
        $size = getimagesize($tmp);
        $ratio = $size[0]/$size[1]; // width/height
        if( $ratio > 1) {
            $width = 200;
            $height = 200/$ratio;
        }
        else {
            $width = 200*$ratio;
            $height = 200;
        }
        $src = imagecreatefromstring(file_get_contents($tmp));
        $dst = imagecreatetruecolor($width,$height);
        imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
        imagedestroy($src);
        imagepng($dst, $path); // adjust format as needed
        imagedestroy($dst);

        $_SESSION['image_uploaded']="yes";
        echo $path ."?".rand(1,32000); 
        } else {
          echo 'invalid file';
        }
    }   

现在,使用 OpenGraph 的 Facebook 共享要求图像至少为 200x200。因此 140x200 的图片无法使用他们的共享功能。

反正我不喜欢非正方形的图像,所以我想拍摄图像,如果它还不是正方形,我想在两侧(或 top/bottom) 并每次都将其保存为完美的 200x200 正方形。

我在下面尝试了这个,但它不起作用(根本没有创建图像)。我尝试做的事情有什么问题?这看起来并不过分复杂,但显然我遗漏了一些东西。

if(isset($_FILES['image'])) {
    $img = $_FILES['image']['name'];
    $tmp = $_FILES['image']['tmp_name'];

    // get uploaded file's extension
    $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

    //checking if image exists for this pool and removing if so, before adding new image in its place
    if(file_exists("uploads/".$poolid.".png")) {
     unlink("uploads/".$poolid.".png");
    }

    // checks valid format
    if(in_array($ext, $valid_extensions))  { 
    //re-size the image and make it a PNG before sending to server
    $final_image = $poolid . ".png";
    $path = "uploads/".strtolower($final_image); 
    $size = getimagesize($tmp);
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = 200;
        $height = 200/$ratio;
    }
    else {
        $width = 200*$ratio;
        $height = 200;
    }
    $src = imagecreatefromstring(file_get_contents($tmp));
    $dst = imagecreatetruecolor($width,$height);
    $orig_img=imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
    imagedestroy($src);

    // create new image and fill with background colour
    $new_img = imagecreatetruecolor($output_w, $output_h);
    $bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red
    imagefill($new_img, 0, 0, $bgcolor); // fill background colour

    // copy and resize original image into center of new image
    $final_img=imagecopyresampled($new_img, $orig_img, 0, 0, 0, 0, 200, 200, $width, $height);
        imagepng($final_img, $path); // adjust format as needed

    imagedestroy($dst);
    $_SESSION['image_uploaded']="yes";
    echo $path ."?".rand(1,32000); 
    } else {
      echo 'invalid file';
    }
}

您不需要临时中间图像。在用背景填充后,您可以将重新采样的源图像直接粘贴到目标图像中。看这里:

  $src = imagecreatefromstring(file_get_contents($tmp));  

  // Create new image and fill it with background color
  $dst = imagecreatetruecolor($output_w,$output_h);
  $bgcolor = imagecolorallocate($dst, 255, 0, 0);
  imagefill($dst, 0, 0, $bgcolor);

  // Copy resampled src image into dst
  if ($ratio > 1)
    imagecopyresampled($dst, $src, 0, ($output_h - $height) / 2, 0, 0, $width, $height, $size[0], $size[1]);
  else
    imagecopyresampled($dst, $src, ($output_w - $width) / 2, 0, 0, 0, $width, $height, $size[0], $size[1]);    

  imagepng($dst, $path); // adjust format as needed

  imagedestroy($src);
  imagedestroy($dst);