具有白色背景且未集中显示的PHP缩略图

时间:2018-12-20 12:54:17

标签: php background center thumbnails gd

我有以下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      



        }

    }
}
}

1 个答案:

答案 0 :(得分:0)

黑色是默认背景,您只需将新图像的背景颜色设置为白色。

//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);

如果您知道哪个尺寸与新的缩略图尺寸不匹配,可以简化此操作。