Php 如何删除图像的背景并复制到另一个图像中?

Php 如何删除图像的背景并复制到另一个图像中?,php,image-processing,gd,Php,Image Processing,Gd,我正在研究验证码。在这个想法中,任务是:从JPEG创建一个图像并删除白色背景,然后从另一个JPEG创建另一个图像,然后通过添加第二个图像作为背景创建最终图像,并在该背景上复制第一个图像,当然,保留创建的透明区域。代码如下: header("Content-Type: image/jpeg"); $nFundo = rand(0,4); $Dirs = array(rand(0,7), rand(0,7), rand(0,7), rand(0,7)); // Will be four times

我正在研究验证码。在这个想法中,任务是:从JPEG创建一个图像并删除白色背景,然后从另一个JPEG创建另一个图像,然后通过添加第二个图像作为背景创建最终图像,并在该背景上复制第一个图像,当然,保留创建的透明区域。代码如下:

header("Content-Type: image/jpeg");

$nFundo = rand(0,4);
$Dirs = array(rand(0,7), rand(0,7), rand(0,7), rand(0,7)); // Will be four times all

$_SESSION["form_captcha"] = $Dirs;

$image = ImageCreatetruecolor(320, 80);
ImageAlphaBlending($image, FALSE);
ImageSaveAlpha($image, TRUE);

$image_seta = ImageCreateFromJPEG("_captcha-seta.jpg"); // Image do copy over
$image_fundo = ImageCreateFromJPEG("_captcha-fundo-".$nFundo.".jpg"); // Image to make the background

for($i=0; $i<4; $i++){
    ImageCopy($image, $image_fundo, $i*80, 0, 0, 0, 80, 80);
}
// So far so good, a background with a pattern repeated four times
$color_white = ImageColorAllocate($image_seta, 255, 255, 255);
ImageColorTransparent($image_seta, $color_white);
ImageSaveAlpha($image_seta, TRUE);

for($i=0; $i<4; $i++){
    $image_seta_rot = imageRotate($image_seta, $Dirs[$i]*45, $color_white);
    ImageCopyResampled($image, $image_seta_rot, $i*80, 0, 0, 0, 80, 80, 80, 80); // Try
}

echo(imagejpeg($image));

imagedestroy($image);
标题(“内容类型:图像/jpeg”);
$nFundo=兰特(0,4);
$Dirs=数组(rand(0,7)、rand(0,7)、rand(0,7)、rand(0,7));//总共四次
$\会话[“格式验证码”]=目录;
$image=ImageCreatetruecolor(320,80);
ImageAlphaBlending($image,FALSE);
ImageSaveAlpha($image,TRUE);
$image_seta=ImageCreateFromJPEG(“_captcha-seta.jpg”);//图像复制吗
$image\u fundo=ImageCreateFromJPEG(“\u captcha-fundo-”$nFundo..jpg”);//以图像作为背景

对于($i=0;$ii如果您是从没有alpha的JPEG创建它,那么您将如何确定背景?这本质上是要求您编写代码来分析图片数据,对吗?这一点并不简单。我正在创建一个图像对象来处理GD,据我所知,JPEG将不会保留在内存中,因此不重要。我在这里看到了一些问题,大家都知道在图像对象中使用alpha,但在复制时可能会出现一些问题…您尝试过了吗。看起来它可能会忽略alpha通道。它使用ImageCopyMerge将在整个图像上产生不同的半透明度效果,我只希望不使用背景作为GIF图像。