|
Posted by Andy Hassall on 11/04/05 22:50
On 4 Nov 2005 12:31:18 -0800, lkrubner@geocities.com wrote:
>Yes, your solution was nearly perfect. I suppose to get it into hex i
>do something like this:
>
>for ($i=0; $i < $width; $i++) {
> for ($r=0; $r < $height; $r++) {
> $rgb = imagecolorat($image, $i, $r);
> $format = "%x";
> $byteCode = sprintf($format, $rgb);
> $imageAsBytesAsString .= $byteCode;
> }
>}
Yep. You probably want zero-padding on the hex, depends on the requirements
for your Postscript format (I have no idea about Postscript).
You've also got the image rotated 90 degrees because of your loops - $i and $r
would be clearer as $x and $y, in which case you'd probably have already
spotted it.
Here's the code expanded to convert images into HTML coloured text (and it
deals with both paletted and true colour images):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>image</title>
<style>
body { font-size: xx-small; }
</style>
</head>
<body>
<?php
$image_data =
file_get_contents("http://static.php.net/www.php.net/images/php.gif");
$image = imagecreatefromstring($image_data);
$width = imagesx($image);
$height = imagesy($image);
echo "<p>the height is $height and the width is $width </p>";
for ($y=0; $y < $height; $y++)
{
for ($x=0; $x < $width; $x++)
{
$colour = imagecolorat($image, $x, $y);
if (imageistruecolor($image))
{
$rgb = sprintf("%06x", $colour);
}
else
{
$colour_rgb = imagecolorsforindex($image, $colour);
$rgb = sprintf("%02x%02x%02x",
$colour_rgb['red'],
$colour_rgb['green'],
$colour_rgb['blue']
);
}
print "<span style='background-color: #$rgb; color: #$rgb;'>+</span>";
}
echo "<br>";
}
?>
</body>
</html>
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Navigation:
[Reply to this message]
|