PHP - imagecolorat() 到 rgb 或 hex?

PHP - imagecolorat() to rgb or hex?

我使用这个代码:

for($x=0;$x<$stageWidth;$x++){
    $stageColors[$x] = [];
    for($y=0;$y<$stageHeight;$y++){
        array_push($stageColors[$x],imagecolorat($pngImage,$x,$y));
    }
}

将所有颜色索引存储在一个数组中。

但是我怎样才能将它们作为 CSS 兼容的东西回应出来呢?

例如:

<div style="background:#<?php echo $either_RGBA_OR_HEX; ?>" ></div>

RGBA(或 RGB)或 HEX 并不重要

这是获取 rgb 值的帮助:

<?php
$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

var_dump($r, $g, $b);//shows the individual values that you can use
?>