如何在php GD中应用的旋转功能后删除黑色背景


How to remove black background after rotation function applied in php GD?

就我而言,我创建了一个php脚本,用于将许多图像合并为单个图像。在合并过程中,我想旋转一些图像。

脚本工作正常。但是旋转的图像显示黑色背景颜色。

我应该删除黑色背景颜色。

我正在使用以下脚本在 php 中处理旋转功能。此脚本可以将三个具有旋转功能的 png 图像合并。

问题:

  • 在IE 8中旋转后,图像周围会出现一些暗线。
  • 背景显示黑色而不是白色

如何解决这些问题。

请修复此脚本问题并发布您的答案。

$im = imagecreatetruecolor(500, 600);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 000, 000, 000);

$source=imagecreatefrompng("http://cn1.kaboodle.com/img/c/0/0/179/4/AAAADDpe-ZwAAAAAAXlIhA/funny-shirt-pikachu-pokemon-anime-mens-cool-humor-t-shirt.png?v=1313557837000");
$asd=imagerotate($source, 280, 0);
imagecolortransparent($asd, $white);
imageantialias($asd, true);
$insert_x = imagesx($asd); 
$insert_y = imagesy($asd);
imagecopymerge ( $im , $asd , 0 , 0 , 0 , 0 , $insert_x , $insert_y , 100 );

$source1=imagecreatefrompng("http://www.vouchersmate.com/content/images/thumbs/0001595_worlds_best_dad_to_be_t_shirts.png");
$asd1=imagerotate($source1, 180, 0);
imagecolortransparent($asd1, $white);
imageantialias($asd1, true);
$insert_x1 = imagesx($asd1); 
$insert_y1 = imagesy($asd1);
imagecopymerge ( $im , $asd1 , 110 , 55 , 0 , 0 , $insert_x1 , $insert_y1 , 100 );

$source2=imagecreatefrompng("http://goalwa.files.wordpress.com/2011/11/green_shirt.png?w=300&h=300");
$asd2=imagerotate($source2, 320, 0);
imagecolortransparent($asd2, $white);
imageantialias($asd2, true);
$insert_x2 = imagesx($asd2); 
$insert_y2 = imagesy($asd2);
imagecopymerge ( $im , $asd2 , 170 , 90 , 0 , 0 , $insert_x2 , $insert_y2 , 100 );

imagecolortransparent($im, $black);
imageantialias($im, true);
imagealphablending($im, true);
imagesavealpha($im, true);
header('Content-Type: image/png');
imagepng($im);

我正在使用以下脚本在 php 中处理旋转功能。此脚本可以将三个具有旋转功能的 png 图像合并。

问题:

  • 在IE 8中旋转后,图像周围会出现一些暗线。
  • 背景显示黑色而不是白色

如何解决这些问题。

请修复此脚本问题并发布您的答案。

$im = imagecreatetruecolor(500, 600);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 000, 000, 000);

$source=imagecreatefrompng("http://cn1.kaboodle.com/img/c/0/0/179/4/AAAADDpe-ZwAAAAAAXlIhA/funny-shirt-pikachu-pokemon-anime-mens-cool-humor-t-shirt.png?v=1313557837000");
$asd=imagerotate($source, 280, 0);
imagecolortransparent($asd, $white);
imageantialias($asd, true);
$insert_x = imagesx($asd); 
$insert_y = imagesy($asd);
imagecopymerge ( $im , $asd , 0 , 0 , 0 , 0 , $insert_x , $insert_y , 100 );

$source1=imagecreatefrompng("http://www.vouchersmate.com/content/images/thumbs/0001595_worlds_best_dad_to_be_t_shirts.png");
$asd1=imagerotate($source1, 180, 0);
imagecolortransparent($asd1, $white);
imageantialias($asd1, true);
$insert_x1 = imagesx($asd1); 
$insert_y1 = imagesy($asd1);
imagecopymerge ( $im , $asd1 , 110 , 55 , 0 , 0 , $insert_x1 , $insert_y1 , 100 );

$source2=imagecreatefrompng("http://goalwa.files.wordpress.com/2011/11/green_shirt.png?w=300&h=300");
$asd2=imagerotate($source2, 320, 0);
imagecolortransparent($asd2, $white);
imageantialias($asd2, true);
$insert_x2 = imagesx($asd2); 
$insert_y2 = imagesy($asd2);
imagecopymerge ( $im , $asd2 , 170 , 90 , 0 , 0 , $insert_x2 , $insert_y2 , 100 );

imagecolortransparent($im, $black);
imageantialias($im, true);
imagealphablending($im, true);
imagesavealpha($im, true);
header('Content-Type: image/png');
imagepng($im);

没有任何代码要检查,很难准确地指出你的问题,但我会尝试:

你看到的黑色是没有颜色。您可能希望该区域是透明的。尝试添加以下代码:

imagealphablending($im, true);
imagesavealpha($im, true);

你在寻找的背景是什么?

您可以使用图像填充矩形以您喜欢的任何颜色填充背景。例如,假设您有一个宽 500 像素、高 250 像素的图像;

$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 500, 250, $white);

您现在有一个完整的白色背景可供使用。你也可以看看图像颜色透明;http://php.net/manual/en/function.imagecolortransparent.php 是否要创建透明背景。