简体   繁体   English

PHP将图像颜色更改为透明,然后用作遮罩

[英]PHP Change image color to transparent then use as a mask

I'm trying to change the background colour of an image from white, to transparent, then to use that as a mask to put over another image. 我正在尝试将图像的背景颜色从白色更改为透明,然后将其用作遮罩另一个图像的遮罩。 (Cannot be transparent already, because another part is which is used as a mask over yet another image)... layers are fun :) (已经不透明了,因为另一部分被用作另一张图像的遮罩)...层很有趣:)

Full code: 完整代码:

<?
include '../includes/db.php';
include '../includes/checks.php';

$type = $_GET['t']; //shirt | shorts
$style = $_GET['s'];
$z = $_GET['z']; // home | away
$a = $_GET['a'];
$b = $_GET['b'];

$aR = hexdec($a['0'].$a['1']);
$aG = hexdec($a['2'].$a['3']);
$aB = hexdec($a['4'].$a['5']);
$bR = hexdec($b['0'].$b['1']);
$bG = hexdec($b['2'].$b['3']);
$bB = hexdec($b['4'].$b['5']);

// main top
$imgname = '../images/kits/test/'. $uTime .'.png';
$im = imagecreatetruecolor( 100, 100); 
$red = imagecolorallocate($im, $aR, $aG, $aB);
imagefill($im, 0, 0, $red);

$mask = imagecreatefrompng('../images/kits/test/6-space.png'); 
imagecopyresampled($im, $mask, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($mask), imagesy($mask));
imagedestroy($mask);

$mask = imagecreatefrompng('../images/kits/test/4-shadows.png'); 
imagecopyresampled($im, $mask, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($mask), imagesy($mask));
imagedestroy($mask);

$mask = imagecreatefrompng('../images/kits/test/1-tag.png'); 
imagecopyresampled($im, $mask, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($mask), imagesy($mask));
imagedestroy($mask);

// design
$im2 = imagecreatetruecolor( 100, 100); 
$red = imagecolorallocate($im2, $bR, $bG, $bB);
imagefill($im2, 0, 0, $red);

$mask = imagecreatefrompng('../images/kits/test/5-logo.png'); 
imagecopyresampled($im2, $mask, 0, 0, 0, 0, imagesx($im2), imagesy($im2), imagesx($mask), imagesy($mask));
imagedestroy($mask);
imagecolortransparent($im2, imagecolorallocatealpha($im2, 0, 0, 0, 127));
imagealphablending($im2, false);
imagesavealpha($im2, true);
$white = imagecolorallocate($im2,255,255,255);
imagecolortransparent($im2, $white);

// merge and result
imagecopyresampled($im, $im2, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($im2), imagesy($im2));
imagepng($im, $imgname);
imagedestroy($im2);
imagedestroy($im);
echo '<img src="', $imgname ,'">';
?>

A couple of methods with Imagemagick ( the second example came from http://www.imagemagick.org/Usage/masking/ ) Imagemagick的几种方法(第二个示例来自http://www.imagemagick.org/Usage/masking/

Change all white in the image to transparent 将图像中的所有白色更改为透明

convert input.jpg -matte -fuzz 1% -transparent rgb(255,255,255) mask.png

Change all the white in an image that is connected to the pixel in the top left corner. 更改连接到左上角像素的图像中的所有白色。

convert input.jpg -alpha set -channel RGBA -fuzz 1% -fill none -floodfill +0+0 white mask.png

In php use like this: 在PHP中使用像这样:

exec("convert input.jpg -matte -fuzz 1% -transparent rgb\(255,255,255\) mask.png");

The command above is changing white rgb(255,255,255) actualy +- 1% of the rgb value to transparent 上面的命令将白色rgb(255,255,255)实际上将rgb值的1%更改为透明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM