469,898 Members | 1,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

do I use fopen or fsockopen to get an image as a stream of bytes?


Suppose I need to get an image as a stream of bytes. I want to store
this in a variable and then embed it in some Postscript code. In my
Postscript code, the image might look like this:
{<
ffffffffffffffffffffffffffffffffffffffffffffffffff ff
ff000000000000000000000000000000000000ffffffffffff ff
ff00efefefefefefefefefefefefefefefef0000ffffffffff ff
ff00efefefefefefefefefefefefefefefef00ce00ffffffff ff
ff00efefefefefefefefefefefefefefefef00cece00ffffff ff
ff00efefefefefefefefefefefefefefefef00cecece00ffff ff
ff00efefefefefefefefefefefefefefefef00cececece00ff ff
ff00efefefefefefefefefefefefefefefef00000000000000 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efef000000ef000000ef000000ef0000ef0000efefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efef000000ef00000000ef00000000ef000000efefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efef0000ef00000000000000ef000000ef0000efefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff00efefefefefefefefefefefefefefefefefefefefefef00 ff
ff000000000000000000000000000000000000000000000000 ff
ffffffffffffffffffffffffffffffffffffffffffffffffff ff
}

image
How do I get an image like that, as a stream of bytes? Which function
do I use?

I know include() sort of works, but there is no way to capture what it
brings in.

Nov 4 '05 #1
13 4286
On 4 Nov 2005 09:42:55 -0800, lk******@geocities.com wrote:
How do I get an image like that, as a stream of bytes? Which function
do I use?


Where are you getting it from?
What format is the image in?

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object

http://uk.php.net/manual/en/function.imagesx.php
http://uk.php.net/manual/en/function.imagesy.php
http://uk.php.net/manual/en/function.imagecolorat.php

- loop over all the pixels in the image, converting to whatever your
representation is (looks like hex representation of brightness level in a
greyscale image?)
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 4 '05 #2

Andy Hassall wrote:
On 4 Nov 2005 09:42:55 -0800, lk******@geocities.com wrote:
How do I get an image like that, as a stream of bytes? Which function
do I use?
Where are you getting it from?
What format is the image in?

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object


Well, I'm groping in the dark here, but I think that
imagecreatefromstring would give me an actual image, whereas what I
want is an actual string, and I don't know how to get a string from an
image file.

The thing with Postscript is that it is a plain text file. And to
represent an image in Postscript, I think that image needs to be
represented as a plain text listing of all its bytes.

I'm currently working at a manufacturing company that would like to
capture images (of a product) off of its website everytime it makes a
sale on the website, and then have that image and some text combined
into a Postscript file that prints out in the shipping department. The
image is suppose to help people see what product needs to be shipped.

I just wrote this code:
<?php

$image =
file_get_contents("http://www.mycompany.com/product_images/composite_standard_sm/318_1_SM_100.jpg");

$string = <<<EOF
%!
% Sample of printing text

/Times-Roman findfont % Get the basic font
20 scalefont % Scale the font to 20 points
setfont % Make it the current font

newpath % Start a new path
72 72 moveto % Lower left corner of text at (72, 72)
(14k White Gold 2 mm Classic Engagement Ring with Heart Shaped Side
Stones .80 ct. tw.) show % Typeset "Hello, world!"

100 200 translate
26 34 scale
26 34 8 [26 0 0 -34 0 34]
{<
$image}

image

showpage

EOF;

echo $string;
?>
Then I went to look at it in my browser. The image bytes show up as a
mess. It doesn't matter what character encoding I choose, it still
looks like a mess. IE asks me to download the file, which I do, and
then I open it in my text editor, and it still looks like a mess, with
strange Russian letters showing up.

The thing is, it doesn't look like plain text. Somehow I was expecting
a lot of 00089000. That kind of stuff. Not foreign letters.

So I'm left thinking that I'm not getting the raw bytes, or they are
getting interpreted somehow, along the way, whereas what I need is the
raw description of that image, because that is what Postscript needs.

Nov 4 '05 #3
On 4 Nov 2005 10:05:06 -0800, lk******@geocities.com wrote:
Andy Hassall wrote:
On 4 Nov 2005 09:42:55 -0800, lk******@geocities.com wrote:
>How do I get an image like that, as a stream of bytes? Which function
>do I use?


Where are you getting it from?
What format is the image in?

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object


Well, I'm groping in the dark here, but I think that
imagecreatefromstring would give me an actual image, whereas what I
want is an actual string, and I don't know how to get a string from an
image file.


That was why I posted the rest of my post, you've stopped reading too early.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 4 '05 #4

lk******@geocities.com wrote:
Suppose I need to get an image as a stream of bytes. I want to store
this in a variable and then embed it in some Postscript code. In my
Postscript code, the image might look like this:


The easiest way is probably to copy the file to a temporary location,
then use ImageMagick to convert it to a .ps file.

Nov 4 '05 #5

Andy Hassall wrote:
On 4 Nov 2005 09:42:55 -0800, lk******@geocities.com wrote:
How do I get an image like that, as a stream of bytes? Which function
do I use?


Where are you getting it from?
What format is the image in?

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object

http://uk.php.net/manual/en/function.imagesx.php
http://uk.php.net/manual/en/function.imagesy.php
http://uk.php.net/manual/en/function...lorat.phplysis tool


I guess, to some extent, I wish there was a character encoding called
"binary". So I could tell the browser to show me the raw binary bytes
of the image.

I notice that IE renders the image as an image, when I echo just the
image and nothing else.

Nov 4 '05 #6

Chung Leong wrote:
lk******@geocities.com wrote:
Suppose I need to get an image as a stream of bytes. I want to store
this in a variable and then embed it in some Postscript code. In my
Postscript code, the image might look like this:


The easiest way is probably to copy the file to a temporary location,
then use ImageMagick to convert it to a .ps file.


I don't see that as being the easiest way of doing things. I'd have to
compile ImageMagick for our HP Unix server, and I've never compiled
anything much of anything. And if it worked, it wouldn't solve my main
problem, which is how to mix text and images together and send that to
a printer without having to open the file in application.

Nov 4 '05 #7

Andy Hassall wrote:
On 4 Nov 2005 10:05:06 -0800, lk******@geocities.com wrote:

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object


Well, I'm groping in the dark here, but I think that
imagecreatefromstring would give me an actual image, whereas what I
want is an actual string, and I don't know how to get a string from an
image file.


That was why I posted the rest of my post, you've stopped reading too early.


Apologies, I should have looked up those functions to see exactly what
you were suggesting.

I'll try something like this:

$myFileAsString = file_get_contents("myFile.jpg");
$image = imagecreatefromstring($myFileAsString);

$width = imagesx($image);
$height = imagesy($image);

for ($i=0; $i < $width; $i++) {
for ($r=0; $r < height; $r++) {
$rgb = imagecolorat($image, $width, $height)
echo $rgb;
}
}

Nov 4 '05 #8

Andy Hassall wrote:
On 4 Nov 2005 09:42:55 -0800, lk******@geocities.com wrote:
How do I get an image like that, as a stream of bytes? Which function
do I use?


Where are you getting it from?
What format is the image in?

You probably want a sequence of:

http://uk.php.net/file_get_contents - get the image from a file or URL

http://uk.php.net/imagecreatefromstring - create a GD image object

http://uk.php.net/manual/en/function.imagesx.php
http://uk.php.net/manual/en/function.imagesy.php
http://uk.php.net/manual/en/function.imagecolorat.php


That's awesome. I tried the following and I got a long list of numbers
8 digits long.

$myFileAsString =
file_get_contents("http://www.mycompany.com/product_images/composite_standard_sm/318_1_SM_100.jpg");

$image = imagecreatefromstring($myFileAsString);

$width = imagesx($image);
$height = imagesy($image);

echo "<p>the height is $height and the width is $width </p>";
for ($i=0; $i < $width; $i++) {
for ($r=0; $r < $height; $r++) {
$rgb = imagecolorat($image, $i, $r);
echo $rgb;
echo "<br>";
}
}

which gave me something like this:

16645629
16711422
16777215
16711422
16711422
16645629
16711422
16645629
16185078
15790320
15329771
15066599
14803427
14737634
14737634
14737634
14606048
14737634
15132392
15658736
16053494
16316666
16645629
16711422

but a lot more of that.

Nov 4 '05 #9
On 4 Nov 2005 11:42:47 -0800, lk******@geocities.com wrote:
That's awesome. I tried the following and I got a long list of numbers
8 digits long.

$myFileAsString =
file_get_contents("http://www.mycompany.com/product_images/composite_standard_sm/318_1_SM_100.jpg");

$image = imagecreatefromstring($myFileAsString);

$width = imagesx($image);
$height = imagesy($image);

echo "<p>the height is $height and the width is $width </p>";
for ($i=0; $i < $width; $i++) {
for ($r=0; $r < $height; $r++) {
$rgb = imagecolorat($image, $i, $r);
echo $rgb;
echo "<br>";
}
}

which gave me something like this:

16645629
16711422 [snip]but a lot more of that.


Good, that's what you asked for. All you need to do now is read the
imagecolorat manual page a bit more closely and convert it into the hex
representation you wanted (sprintf and %x is useful here), and you're done.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 4 '05 #10

Andy Hassall wrote:
On 4 Nov 2005 11:42:47 -0800, lk******@geocities.com wrote:
echo "<p>the height is $height and the width is $width </p>";
for ($i=0; $i < $width; $i++) {
for ($r=0; $r < $height; $r++) {
$rgb = imagecolorat($image, $i, $r);
echo $rgb;
echo "<br>";
}
}

which gave me something like this:

16645629
16711422

[snip]
but a lot more of that.


Good, that's what you asked for. All you need to do now is read the
imagecolorat manual page a bit more closely and convert it into the hex
representation you wanted (sprintf and %x is useful here), and you're done.


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;
}
}

Nov 4 '05 #11
On 4 Nov 2005 12:31:18 -0800, lk******@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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 4 '05 #12

Andy Hassall wrote:
On 4 Nov 2005 12:31:18 -0800, lk******@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):


When you say true color, I assume you mean 24 bit and 32 bit colors,
where each pixels has an "r" and a "g" and "b" value?

"paletted colour" would be what? Anything less that 24 bit? Or
something like a Gif, where the image creates chooses the palette of
colors that will be allowed into an image?

Nov 4 '05 #13
On 4 Nov 2005 14:02:03 -0800, lk******@geocities.com wrote:
When you say true color, I assume you mean 24 bit and 32 bit colors,
where each pixels has an "r" and a "g" and "b" value?
Yes.
"paletted colour" would be what? Anything less that 24 bit?
No.
Or something like a Gif, where the image creates chooses the palette of
colors that will be allowed into an image?


Yes.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 4 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Shaoyong Wang | last post: by
2 posts views Thread by Colin Bell | last post: by
3 posts views Thread by Ming | last post: by
12 posts views Thread by Bill | last post: by
2 posts views Thread by Loopy | last post: by
9 posts views Thread by fipaj1992 | last post: by
5 posts views Thread by Brian | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.