Hi, I've just started messing with PHP and Apache, and knocked up the below quick script as a quirky idea. It's meant to read the hex colours of each pixel of a PNG file, and then print them as the colour for the background of a table division in a html page.
It does result in a grid of coloured cubes but my problem is the colour is wrong compared to the source image viewed enlarged in, say, the Gimp.
I suspect the problem lies in the int->hex code I copied from the PHP Manual, with the bitshifting and masking.
PHP Code:
<html>
<head>
<title>Pixel Picker</title>
</head>
<body>
<?php
$file = "/usr/share/icons/crystalsvg/32x32/apps/kmenu.png";
$image = ImageCreateFromPng($file);
$size = GetImageSize($file);
$width = $size[0];
$height = $size[1];
echo "<table>";
for ($i = 0; $i < $height; $i++) {
echo "<tr>" ;
for ($j = 0; $j < $width; $j++) {
$rgb = ImageColorAt($image, $j, $i);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
echo "<td bgcolor=#";
echo base_convert($r, 10, 16);
echo base_convert($g, 10, 16);
echo base_convert($b, 10, 16);
echo "\">  </td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Just wondering if anyone had a quick solution. Plus pointing out any bad coding habits would be appreciated.
