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

时间:2014-06-09 03:22:31

标签: 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 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);

我尝试将$image_seta_rot替换为$image_seta(&#34;尝试&#34;行)以查看旋转是否有问题,但即使没有旋转,白色也未被删除,图像只是&#34;擦除&#34;之前创建的背景。因此副本失败或者白色从未被删除......我可能会创建一个透明背景的PNG,但学习如何动态制作它会很有趣,你不觉得吗?有什么想法吗?

0 个答案:

没有答案
相关问题