PHP 白色背景且未集中的缩略图

PHP thumbnail with white background and not centralized

我有以下 php 脚本可以在上传图像后创建缩略图。它工作正常,但它会创建一个黑色背景并且如果不是您想要的尺寸则不会将图像居中,有谁知道如何保留白色背景并将其居中?

如何修复 https://imgur.com/a/uTrPadZ

if($_SERVER['REQUEST_METHOD']=='POST')
{
$filetmp = $_FILES["image"]["tmp_name"];
$filename = $_FILES["image"]["name"];
$filetype = $_FILES["image"]["type"];
$filesize = $_FILES["image"]["size"];
$fileinfo = getimagesize($_FILES["image"]["tmp_name"]);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
$filepath = "../uploads/";
$filepath_thumb = "../thumbnail/";

if($filetmp == "")
{
    echo "please select a photo";
}
else
{

    if($filesize > 2097152)
    {
        echo "photo > 2mb";
    }
    else
    {

        if($filetype != "image/jpeg" && $filetype != "image/png" && $filetype != "image/gif")
        {
            echo "Please upload jpg / png / gif";
        }
        else
        {
            $final_image = rand(1000,1000000).$filename;
            $filepath = $filepath.strtolower($final_image);
            move_uploaded_file($filetmp,$filepath);             

            if($filetype == "image/jpeg")
            {
              $imagecreate = "imagecreatefromjpeg";
              $imageformat = "imagejpeg";
            }
            if($filetype == "image/png")
            {                        
              $imagecreate = "imagecreatefrompng";
              $imageformat = "imagepng";
            }
            if($filetype == "image/gif")
            {                        
              $imagecreate= "imagecreatefromgif";
              $imageformat = "imagegif";
            }

            $new_width = "200";
            $new_height = "200";


            $filepath_thumb = $filepath_thumb.strtolower($final_image);
            $image_p = imagecreatetruecolor($new_width, $new_height);
            $image = $imagecreate($filepath); //photo folder
            if($filewidth > $fileheight) 
            {
                $thumb_w    =   $new_width;
                $thumb_h    =   $fileheight*($new_height/$filewidth);
            }

            if($filewidth < $fileheight) 
            {
                $thumb_w    =   $filewidth*($new_width/$fileheight);
                $thumb_h    =   $new_height;
            }

            if($filewidth == $fileheight) 
            {
                $thumb_w    =   $new_width;
                $thumb_h    =   $new_height;
            }

            $dst_img        =   ImageCreateTrueColor($thumb_w,$thumb_h);

            imagecopyresampled($image_p,$image,0,0,0,0,$thumb_w,$thumb_h,$filewidth,$fileheight);

            $imageformat($image_p, $filepath_thumb);//thumb folder      



        }

    }
}
}

默认背景为黑色,您只需先将新图片的背景色设置为白色即可。

//set background colour white before copying
$white = imagecolorallocate($image_p, 255, 255, 255);
imagefill($image_p, 0, 0, $white);

imagecopyresampled($image_p,$image,0,0,0,0,$thumb_w,$thumb_h,$filewidth,$fileheight);

$imageformat($image_p, $filepath_thumb);//thumb folder

此外,$dst_img 似乎没有被使用。

居中(未经测试,但数学足够简单):

$xOffset = (imagesx($p_image)-$thumb_w) / 2;
$yOffset = (imagesy($p_image)-$thumb_h) / 2;

imagecopyresampled($image_p,$image,$offsetX,$offsetY,0,0,$thumb_w,$thumb_h,$filewidth,$fileheight);

如果您知道哪个维度与新的缩略图维度不匹配,这可以简化。