get image size from binary data

Is there some way to get the dimensions of an image, given the binary
data of the image, without having to write it to a temporary file?

It seems that getimagesize() will only take a filename, but since I have
to download the image from a remote URL with fsockopen(), I have it
stored as a binary string.

I've had a cursory glance at the data, but predictably the size is not
stored in decimal format anywhere...

--
cb
Christoph Burschka [ Di, 26 Juni 2007 09:31 ] [ ID #1748839 ]

Re: get image size from binary data

On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
aachen.de> wrote:
> Is there some way to get the dimensions of an image, given the binary
> data of the image, without having to write it to a temporary file?
>
> It seems that getimagesize() will only take a filename, but since I have
> to download the image from a remote URL with fsockopen(), I have it
> stored as a binary string.
>
> I've had a cursory glance at the data, but predictably the size is not
> stored in decimal format anywhere...
>
> --
> cb

If the image format is supported by your GD Library, you can use the
following:

<?php

$binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
images/logo.gif');

$im = imagecreatefromstring($binary_data);

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

print "width: $width\n";
print "height: $height\n";

?>
james.gauth [ Di, 26 Juni 2007 10:38 ] [ ID #1748840 ]

Re: get image size from binary data

james.gauth [at] googlemail.com wrote:
> On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
> aachen.de> wrote:
>> Is there some way to get the dimensions of an image, given the binary
>> data of the image, without having to write it to a temporary file?
>>
>> It seems that getimagesize() will only take a filename, but since I have
>> to download the image from a remote URL with fsockopen(), I have it
>> stored as a binary string.
>>
>> I've had a cursory glance at the data, but predictably the size is not
>> stored in decimal format anywhere...
>>
>> --
>> cb
>
> If the image format is supported by your GD Library, you can use the
> following:
>
> <?php
>
> $binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
> images/logo.gif');
>
> $im = imagecreatefromstring($binary_data);
>
> $width = imagesx($im);
> $height = imagesy($im);
>
> print "width: $width\n";
> print "height: $height\n";
>
> ?>
>

Someone correct me if I am wrong, but whenever you open an image/file to
read (even if you want to read the headers) you are storing it to some
temporary buffer/space on your machine!
Maybe the GD solution above is the best!

Armand
Armand B [ Di, 26 Juni 2007 22:50 ] [ ID #1748855 ]

Re: get image size from binary data

On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
aachen.de> wrote:
> Is there some way to get the dimensions of an image, given the binary
> data of the image, without having to write it to a temporary file?
>
> It seems that getimagesize() will only take a filename, but since I have
> to download the image from a remote URL with fsockopen(), I have it
> stored as a binary string.
>
> I've had a cursory glance at the data, but predictably the size is not
> stored in decimal format anywhere...
>
> --
> cb

If the image format is supported by your GD Library, you can use the
following:

<?php

$binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
images/logo.gif');

$im = imagecreatefromstring($binary_data);

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

print "width: $width\n";
print "height: $height\n";

?>
james.gauth [ Di, 26 Juni 2007 10:38 ] [ ID #1748889 ]

Re: get image size from binary data

james.gauth [at] googlemail.com wrote:
> On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
> aachen.de> wrote:
>> Is there some way to get the dimensions of an image, given the binary
>> data of the image, without having to write it to a temporary file?
>>
>> It seems that getimagesize() will only take a filename, but since I have
>> to download the image from a remote URL with fsockopen(), I have it
>> stored as a binary string.
>>
>> I've had a cursory glance at the data, but predictably the size is not
>> stored in decimal format anywhere...
>>
>> --
>> cb
>
> If the image format is supported by your GD Library, you can use the
> following:
>
> <?php
>
> $binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
> images/logo.gif');
>
> $im = imagecreatefromstring($binary_data);
>
> $width = imagesx($im);
> $height = imagesy($im);
>
> print "width: $width\n";
> print "height: $height\n";
>
> ?>
>

Someone correct me if I am wrong, but whenever you open an image/file to
read (even if you want to read the headers) you are storing it to some
temporary buffer/space on your machine!
Maybe the GD solution above is the best!

Armand
Armand B [ Di, 26 Juni 2007 22:50 ] [ ID #1748943 ]

Re: get image size from binary data

Armand Brahaj wrote:
> james.gauth [at] googlemail.com wrote:
>> On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
>> aachen.de> wrote:
>>> Is there some way to get the dimensions of an image, given the binary
>>> data of the image, without having to write it to a temporary file?
>>>
>>> It seems that getimagesize() will only take a filename, but since I have
>>> to download the image from a remote URL with fsockopen(), I have it
>>> stored as a binary string.
>>>
>>> I've had a cursory glance at the data, but predictably the size is not
>>> stored in decimal format anywhere...
>>>
>>> --
>>> cb
>>
>> If the image format is supported by your GD Library, you can use the
>> following:
>>
>> <?php
>>
>> $binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
>> images/logo.gif');
>>
>> $im = imagecreatefromstring($binary_data);
>>
>> $width = imagesx($im);
>> $height = imagesy($im);
>>
>> print "width: $width\n";
>> print "height: $height\n";
>>
>> ?>
>>
>
> Someone correct me if I am wrong, but whenever you open an image/file to
> read (even if you want to read the headers) you are storing it to some
> temporary buffer/space on your machine!
> Maybe the GD solution above is the best!
>
> Armand

Yeah, so I'd be downloading the image data, saving it to a temporary
file, reading the temp file into the buffer and getting the header. Not
exactly efficient.

Thanks for imagecreatefromstring(); that was exactly the function I was
looking for!

--
cb
Christoph Burschka [ Mi, 27 Juni 2007 10:26 ] [ ID #1751301 ]

Re: get image size from binary data

> Yeah, so I'd be downloading the image data, saving it to a temporary
> file, reading the temp file into the buffer and getting the header. Not
> exactly efficient.
>
> Thanks for imagecreatefromstring(); that was exactly the function I was
> looking for!

How else do you expect a system to know details of files on other systems
without downloading them?

I have a book beside me how many pages has it got, what is the author and
what is the title? This is basically what you are wanting your script to do.
Peter [ Mi, 27 Juni 2007 18:43 ] [ ID #1751303 ]

Re: get image size from binary data

Armand Brahaj wrote:
> james.gauth [at] googlemail.com wrote:
>> On 26 Jun, 08:31, Christoph Burschka <christoph.bursc... [at] rwth-
>> aachen.de> wrote:
>>> Is there some way to get the dimensions of an image, given the binary
>>> data of the image, without having to write it to a temporary file?
>>>
>>> It seems that getimagesize() will only take a filename, but since I have
>>> to download the image from a remote URL with fsockopen(), I have it
>>> stored as a binary string.
>>>
>>> I've had a cursory glance at the data, but predictably the size is not
>>> stored in decimal format anywhere...
>>>
>>> --
>>> cb
>>
>> If the image format is supported by your GD Library, you can use the
>> following:
>>
>> <?php
>>
>> $binary_data = file_get_contents('http://www.google.co.uk/intl/en_uk/
>> images/logo.gif');
>>
>> $im = imagecreatefromstring($binary_data);
>>
>> $width = imagesx($im);
>> $height = imagesy($im);
>>
>> print "width: $width\n";
>> print "height: $height\n";
>>
>> ?>
>>
>
> Someone correct me if I am wrong, but whenever you open an image/file to
> read (even if you want to read the headers) you are storing it to some
> temporary buffer/space on your machine!
> Maybe the GD solution above is the best!
>
> Armand

Yeah, so I'd be downloading the image data, saving it to a temporary
file, reading the temp file into the buffer and getting the header. Not
exactly efficient.

Thanks for imagecreatefromstring(); that was exactly the function I was
looking for!

--
cb
Christoph Burschka [ Mi, 27 Juni 2007 10:26 ] [ ID #1751323 ]

Re: get image size from binary data

> Yeah, so I'd be downloading the image data, saving it to a temporary
> file, reading the temp file into the buffer and getting the header. Not
> exactly efficient.
>
> Thanks for imagecreatefromstring(); that was exactly the function I was
> looking for!

How else do you expect a system to know details of files on other systems
without downloading them?

I have a book beside me how many pages has it got, what is the author and
what is the title? This is basically what you are wanting your script to do.
Peter [ Mi, 27 Juni 2007 18:43 ] [ ID #1751355 ]

Re: get image size from binary data

peter schrieb:
>>Yeah, so I'd be downloading the image data, saving it to a temporary
>>file, reading the temp file into the buffer and getting the header. Not
>>exactly efficient.
>>
>>Thanks for imagecreatefromstring(); that was exactly the function I was
>>looking for!
>
>
> How else do you expect a system to know details of files on other systems
> without downloading them?
>
> I have a book beside me how many pages has it got, what is the author and
> what is the title? This is basically what you are wanting your script to do.
>
>

To complete the analogy, you open the book and count the pages. You do
not xerox the book page by page, then open the copy. ;)

It doesn't matter if fsockopen(url) -> imagecreatefromstring() ->
imagesx() uses temporary files (a process I can't influence anyway),
fsockopen() -> fwrite() -> getimagesize() is likely to use one more.

Even if they are equally efficient, the first method handles it in the
background without making me juggle files in my own code, removing a
frequent source of bugs.

--
cb
Christoph Burschka [ Do, 28 Juni 2007 11:32 ] [ ID #1753672 ]

Re: get image size from binary data

peter schrieb:
>>Yeah, so I'd be downloading the image data, saving it to a temporary
>>file, reading the temp file into the buffer and getting the header. Not
>>exactly efficient.
>>
>>Thanks for imagecreatefromstring(); that was exactly the function I was
>>looking for!
>
>
> How else do you expect a system to know details of files on other systems
> without downloading them?
>
> I have a book beside me how many pages has it got, what is the author and
> what is the title? This is basically what you are wanting your script to do.
>
>

To complete the analogy, you open the book and count the pages. You do
not xerox the book page by page, then open the copy. ;)

It doesn't matter if fsockopen(url) -> imagecreatefromstring() ->
imagesx() uses temporary files (a process I can't influence anyway),
fsockopen() -> fwrite() -> getimagesize() is likely to use one more.

Even if they are equally efficient, the first method handles it in the
background without making me juggle files in my own code, removing a
frequent source of bugs.

--
cb
Christoph Burschka [ Do, 28 Juni 2007 11:32 ] [ ID #1753699 ]
PHP » alt.php » get image size from binary data

Vorheriges Thema: Newbie question = how to send variable form one page to the other
Nächstes Thema: PHP parsing script