Geting byte content & uInt32 encoding [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alainlavanchy
Forum Newbie
Posts: 4
Joined: Tue Sep 04, 2007 8:31 am

Geting byte content & uInt32 encoding [solved]

Post by alainlavanchy »

Hi all there

Have search for the following problem, but could find nothing that worked.

I have the following problem:
I have to detect color profiles which are embedded in different image file types. I have no big problem to find the beginning of the embedded data. And I know the architecture of the embedded profile.

Bytes 0..3 - Profile size - Encoded as UInt32Number
Bytes 16..19 - Colour space

I can get out the Colour space, but not the profile size. Which I need to extract the whole profile. I've tried the following:

Code: Select all

<?php
$filename = "./images/test.jpeg";
$fcontent = fopen($filename, "rb");
$fdata = fgets($fcontent, filesize($filename));
fclose($fcontent);

$profile_start = strpos($fdata,"ICC_PROFILE") //Detects the tag defining the start of the embedded content
$profile_start = $profile_start+11; //To start after the tag

$size = substr($fdata,$profile_start,4);
$colour_space = substr($fdata,$profile_start+16,4);

echo $size;
echo $colour_space;
?>
If I open the file in a Hex-Editor I have no problem to search for the positions and then just transform the Hex-Code into Decimal. But I couldn't fix it in in PHP.
Last edited by alainlavanchy on Thu Sep 06, 2007 9:25 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you need unpack() to convert binary data to native php types.
alainlavanchy
Forum Newbie
Posts: 4
Joined: Tue Sep 04, 2007 8:31 am

Post by alainlavanchy »

Thanks Weirdan

Works great with unpack.
I can get the encoded length of the profile as well as the number of tags in the tag table.

But the next problem is unfortunately not far away.

The tag table starts at byte 128 and is 4+12n bytes long, n being the number of tags. The first 4 bytes define the numbers of tags. After each tag uses 12bytes. I have the following code:

Code: Select all

<?php 
$filename = "./images/test.jpeg"; 
$fcontent = fopen($filename, "rb"); 
$fdata = fgets($fcontent, filesize($filename)); 
fclose($fcontent); 

$tag_array = unpack("N", substr($fdata,128,4));  //Getting the number of tags
$tag_count = $tag_array[1];

$tag_table_length = 4 + 12*$tag_count;  //In my example 124

$tag_table = substr($fdata,128,$tag_table_length);
?>
I can read out of $tag_table itself again the number of tags using the same method. But it seems that the string ends after for bytes. The bytes 4 to 7 should contain the first tag description. But all I get is null. Also tried not to calculate $tag_table_length and set

Code: Select all

$tag_table_length = '124';
I even tried to perform first to convert the binary string into a hex string using bin2hex. And then to scan the hex string for the tags I need. But also the hex string represented only the first 4 bytes.

I'm quite lost as substr should be binary safe.

Any help is very much appreciated. - Thanks
Last edited by alainlavanchy on Thu Sep 06, 2007 9:21 am, edited 1 time in total.
alainlavanchy
Forum Newbie
Posts: 4
Joined: Tue Sep 04, 2007 8:31 am

String length - Filesize

Post by alainlavanchy »

Did some testing to check the length of my string and the file.

Code: Select all

<?php 
$filename = "./images/test.jpeg"; 
$fcontent = fopen($filename, "rb"); 
$fdata = fgets($fcontent, filesize($filename)); 
fclose($fcontent); 

echo "Filesize: ";
echo filesize($filename); // It says 557164

echo "<br> String length: ";
echo strlen($fdata); // And this says 132
?>
Why is there this difference? Tested it localy on a XAMPP with PHP Version 5.2.3 so it should be a timeout or memory problem. Shouldn't it?
alainlavanchy
Forum Newbie
Posts: 4
Joined: Tue Sep 04, 2007 8:31 am

Post by alainlavanchy »

It was the fgets(), so it only read a line. With fread() it works.
Post Reply