在imagecreatetruecolor()之后使用imagefill()处理白色背景的问题


image processing - PHP Issue with using imagefill() for a white background after imagecreatetruecolor()

我有一个图库工具,其中我所有的图像必须适合515px X 343px的空间,同时居中。我保留了比例,并成功地将图像居中,并在两侧(上,右,下,左)设置黑色背景,这些背景在调整大小后仍然打开。图像可能比给定的规格更高或更宽。

我需要使黑色背景白色,并尝试在这个问题上建议的答案imagescreateecolor与白色背景在一个更高的图像上,我成功地把左边变成白色,但我不能得到图像的右边是白色。以下是我的部分代码:

$file1new = imagecreatetruecolor($framewidth, $frameheight);
if(isset($fromMidX) && $fromMidX > 0){
    $white = imagecolorallocate($file1new, 255, 255, 255); 
    $rightOffset = ($framewidth - $fromMidX) + 1;
    imagefill($file1new, 0, 0, $white);  //Line 1
    imagefill($file1new, $rightOffset, 0, $white); //Line 2 
}

变量$framewidth = 515, $framheight = 343, $fromMidX是x坐标偏移量。如果我将$rightOffset替换为第2行的静态值510,右侧仍然是全黑的(即使是最后5像素)。更好的是,如果我注释掉第一行而不是第二行,左边仍然是白色,右边仍然是黑色。

我理解imageffill()如何工作的方式是,它从给定的X,Y坐标开始,并用新颜色填充该像素上的任何颜色,在我的情况下是255,255,255。所以我想我之所以只在一侧使用白色是因为我的图像将画布分成了两部分。这就是我为右侧添加第二个imagefill()的原因。

唯一让我感到困惑的是,在我将上传图像的对象带入方程之前,我正在使用imagefill()。我不知道这是如何影响被白色填充的部分的。

如有任何见解,不胜感激。

编辑1:在上面的代码之后,我有了这个:

$source = imagecreatefromjpeg($image); //Line 3
imagecopyresampled($file1new, $source , $fromMidX, $fromMidY , 0, 0, $framewidth, /$frameheight, $w, $h); //Line 4
imagejpeg($file1new, $image,85);
imagedestroy($file1new);
变量$image是我上传的图片在 之后的位置
move_uploaded_file($_FILES['image']['tmp_name'], $location);
$image = $location;

如果我注释掉第3行和第4行,那么生成的图像是全白色的。

我还认为,在对图像应用任何调整大小的操作之前,我已经用白色填充了图像。

尝试在加载请求的图像后重新填充右侧?

<?php
$source = imagecreatefromjpeg($image); //Line 3
$file1new = imagecreatetruecolor($framewidth, $frameheight);
$white = imagecolorallocate($file1new, 255, 255, 255);
//fill whole image with white color
imagefill($file1new, 0, 0, $white);  //Line 1
//find right side of image
$rightOffset = ($framewidth - $fromMidX) + 1;
//insert source file into new image
imagecopyresampled($file1new, $source , $fromMidX, $fromMidY , 0, 0, $framewidth, $frameheight, $w, $h); //Line 4
//fill image right hand side with white
imagefill($file1new, $rightOffset, 0, $white); //Line 2 
imagejpeg($file1new, $image,85);
imagedestroy($file1new);
?>