【发布时间】:2019-06-27 21:27:57
【问题描述】:
我想使用 PHP GD 为一些 PNG 着色。出于测试目的,我硬编码了红色 (255,0,0),稍后将被动态变量替换。
例如我有这两张图片:
图片一:
图片2:
使用我的代码,只有图像 2 可以正常工作。
但是狗的图像有某种灰色的盒子,不知道这是从哪里来的。
这是我正在使用的代码:
<?php
$im = imagecreatefrompng('dog.png');
imagealphablending($im, false);
imagesavealpha($im, true);
$w = imagesx($im);
$h = imagesy($im);
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$color = imagecolorsforindex($im, imagecolorat($im, $x, $y));
$r = ($color['red'] * 255) / 255;
$g = ($color['green'] * 0) / 255;
$b = ($color['blue'] * 0) / 255;
imagesetpixel($im, $x, $y, imagecolorallocatealpha($im, $r, $g, $b, $color['alpha']));
}
}
imagepng($im, 'result.png');
imagedestroy($im);
为什么它适用于图像 2 而不是图像 1?我只能想到图像 1 上的某种 alpha 蒙版。
希望有人可以帮助我
【问题讨论】: