PHP GD:需要找出 alpha 数量

发布于 2024-09-06 08:23:45 字数 1915 浏览 6 评论 0原文

想象一下这个。我有一个颜色为 (255,0,0) [红色] 的像素。没有人用* alpha 在上面涂上白色。我知道背景颜色,我知道它用什么颜色绘制,我需要获取绘制颜色的阿尔法。如何?

到目前为止我们已经:

$im = imagecreatefrompng('public/images/bg-tmp.png');

// we need to find a nearest match for this array in every pixel
/*$colors = array(
    array(255, 255, 255, 100),
    array(255, 255, 255, 75),
    array(255, 255, 255, 50),
 array(255, 255, 255, 25),
    array(255, 255, 255, 0),
);*/

// finding out the pixel color
$rgb = imagecolorat($im, 0, 6);

$r  = ($rgb >> 16) & 0xFF;
$g  = ($rgb >> 8) & 0xFF;
$b  = $rgb & 0xFF;

var_dump($r, $g, $b);

好的。所以我取得了进步。

$im = imagecreatefrompng('public/images/bg-tmp.png');

$pixels = array();

for($y = 0; $y < 24; $y++)
{
 for($x = 0; $x < 24; $x++)
 {
  $rgb  = imagecolorat($im, $x, $y);

// 前三个 RGB 值是背景颜色 $pixels[] = array($x, $y, calc_alpha_diff(0, 0, 0, ($rgb >> 16) & 0xFF, ($rgb >> 8) & 0xFF, $rgb & 0xFF)); } 这段

function calc_alpha_diff($R1,$G1,$B1,$R2,$G2,$B2)
{
 $color_diff = max($R1,$R2) - min($R1,$R2) + max($G1,$G2) - min($G1,$G2) + max($B1,$B2) - min($B1,$B2);

 $array  = array
 (
  100 => 510, // 1
  75 => 382, // 0.75
  50 => 256, // 0.5
  25 => 128, // 0.25
  0 => 0 // 0
 );

 // find the nearest value
 foreach($array as $key => $val)
 {
  $delta = abs($val - $color_diff);

  if(!isset($min_delta) || $min_delta > $delta)
  {
   $min_delta = $delta;
   $alpha  = $key;
  }
 }

 return $alpha;
}

$im  = imagecreatetruecolor(24, 24);

foreach($pixels as $p)
{
 if($p[2] != 0)
 {
  imagesetpixel($im, $p[0], $p[1], imagecolorallocatealpha($im, 255, 0, 0, $p[2]));
 }
}

// make image transperant
imagecolortransparent($im, 0);

imagepng($im);
imagedestroy($im);

header('Content-type: image/png');

exit;

代码将为您提供一个好的结果。然而,还不够好。请提出改进​​建议。

Imagine this. I have a pixel of color (255,0,0) [red]. No someone painted on it with white color with * alpha. I know the background color, I know with what color it was painted over, I need to get the alpha of the painted over color. How?

So far we have:

$im = imagecreatefrompng('public/images/bg-tmp.png');

// we need to find a nearest match for this array in every pixel
/*$colors = array(
    array(255, 255, 255, 100),
    array(255, 255, 255, 75),
    array(255, 255, 255, 50),
 array(255, 255, 255, 25),
    array(255, 255, 255, 0),
);*/

// finding out the pixel color
$rgb = imagecolorat($im, 0, 6);

$r  = ($rgb >> 16) & 0xFF;
$g  = ($rgb >> 8) & 0xFF;
$b  = $rgb & 0xFF;

var_dump($r, $g, $b);

Ok. So I make a progress.

$im = imagecreatefrompng('public/images/bg-tmp.png');

$pixels = array();

for($y = 0; $y < 24; $y++)
{
 for($x = 0; $x < 24; $x++)
 {
  $rgb  = imagecolorat($im, $x, $y);

// first three RGB values are background colors
$pixels[] = array($x, $y, calc_alpha_diff(0, 0, 0, ($rgb >> 16) & 0xFF, ($rgb >> 8) & 0xFF, $rgb & 0xFF));
}
}

function calc_alpha_diff($R1,$G1,$B1,$R2,$G2,$B2)
{
 $color_diff = max($R1,$R2) - min($R1,$R2) + max($G1,$G2) - min($G1,$G2) + max($B1,$B2) - min($B1,$B2);

 $array  = array
 (
  100 => 510, // 1
  75 => 382, // 0.75
  50 => 256, // 0.5
  25 => 128, // 0.25
  0 => 0 // 0
 );

 // find the nearest value
 foreach($array as $key => $val)
 {
  $delta = abs($val - $color_diff);

  if(!isset($min_delta) || $min_delta > $delta)
  {
   $min_delta = $delta;
   $alpha  = $key;
  }
 }

 return $alpha;
}

$im  = imagecreatetruecolor(24, 24);

foreach($pixels as $p)
{
 if($p[2] != 0)
 {
  imagesetpixel($im, $p[0], $p[1], imagecolorallocatealpha($im, 255, 0, 0, $p[2]));
 }
}

// make image transperant
imagecolortransparent($im, 0);

imagepng($im);
imagedestroy($im);

header('Content-type: image/png');

exit;

This code will provide you a ~ good result. However, not good enough. Please suggest improvements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文