Muitas vezes é necessário analisar os pixéis de uma imagem no formato hexadecimal. A rotina seguinte percorre a imagem linha a linha e lista a cor de cada pixel no formato hexadecimal.
<?php
$file_img = "img_0.jpg";
$img_val = @getimagesize($file_img);
$w = $img_val[0];
$h = $img_val[1];
$tipo = $img_val['mime'];
$tipo = str_replace("image/", "", $tipo);
if($tipo == "jpeg") $tipo = "jpg" ;
if ($tipo=='jpg') $img = @imagecreatefromjpeg($file_img);
if ($tipo=='gif') $img = @imagecreatefromgif($file_img);
if ($tipo=='png') $img = @imagecreatefrompng($file_img);
$r = $g = $b = 0;
for($y = 0; $y < $h; $y++)
{
echo('<br />');
for($x = 0; $x < $w; $x++) {
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$cor_exa = "#" . str_pad(dechex($r), 2, "0", STR_PAD_LEFT).str_pad(dechex($g), 2, "0", STR_PAD_LEFT).str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
echo($cor_exa);
}
}
?>