You are here: Re: do I use fopen or fsockopen to get an image as a stream of bytes? « PHP Programming Language « IT news, forums, messages
Re: do I use fopen or fsockopen to get an image as a stream of bytes?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация