SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting a GIF Image to Hexadecimal Format

    hello there
    how can I convert a GIF img to hexadecimal format?
    I tryed to use this :
    PHP Code:
    $imgfile "file.gif";

    $tempfile "$imgfile";

    $tempfile fopen($tempfile'r');
    $data fread($tempfile1024*1024);
    fclose($tempfile);

    $Img bin2hex("$data");

    echo 
    "$Img"
    But its not the right format, please help me A.S.A.P!
    thanx

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you can print each character from $data using the dechex() function,

    http://www.php.net/manual/en/function.dechex.php

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    m0h, try this code
    PHP Code:
    <?php
     
    function pauldump($image,$size) {
       if (
    $fp fopen($image'r')) {
           
    $data fread($fp$size);
           
    fclose($fp);

           for(
    $i=0;$i<strlen($data);$i++)
                
    $hex .= dechex(ord($data[$i]));  
       } else
         
    $hex "File error";
       return 
    $hex;
     }

      echo 
    pauldump('php4.gif',1024*1024);
    ?>

  4. #4
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not working here is what I did:
    PHP Code:
    <?php
    $tempfile 
    "Adidsa4.gif";

    $tempfile fopen($tempfile'r');
    $htmldata fread($tempfile1024*1024);
    fclose($tempfile);

    echo 
    bin2hex("$htmldata");

    echo 
    '<br>';

    function 
    pauldump($image,$size) { 
    if (
    $fp fopen($image'r')) { 
    $data fread($fp$size); 
    fclose($fp); 

    for(
    $i=0;$i<strlen($data);$i++) 
    $hex .= dechex(ord($data[$i])); 
    } else 
    $hex "File error"
    return 
    $hex


    echo 
    pauldump('Adidsa4.gif',1024*1024); 

    ?>
    here is what i got
    my script:
    47494638396148000e00f00100000000ffffff21f90400000002002c0000000048000e0000026c8c8f09c9ed0fa334a0ce8b73ac4bfb3f7116486a9c22a25dc92262f78eafb72a6c1cc40b6ee64cdd0a3e8007224d873c354e30cb8ee2843e732321641585fab053aa8f124ace1cb5eef60b2e6bad1b17faac5617334c67955cec46b9e8ec3b04f63794c5f4554835a5845000003b
    your script:
    474946383961480e0f010000ffffff21f94000202c0000480e0026c8c8f9c9edfa334a0ce8b73ac4bfb3f7116486a9c22a25dc92262f78eafb72a6c1cc4b6ee64cdda3e807224d873c354e30cb8ee2843e732321641585fab053aa8f124ace1cb5eef6b2e6bad1b17faac5617334c67955cec46b9e8ec3b4f63794c5f4554835a58450003b

    both are not the right format!

  5. #5
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got the code that do this in PERL
    here it is:

    -----------------------------------------------
    open(F, "myimage.gif") || die $!;
    binmode(F);
    $s = $hx = "";
    while (read(F, $s, 1) > 0) {
    $hx .= sprintf("%02X", ord($s));
    }
    close F;
    print "The hex-encoded GIF file is: $hx\n";
    -----------------------------------------------

    it can also be done using UNIX Command Pipe

    -----------------------------------------------
    od -v -t x1 myimage.gif | \
    sed -e 's/^........//' | \
    tr 'a-f' 'A-F' | \
    tr -dc '0-9A-F'
    -----------------------------------------------

    but i need it in php :o)

  6. #6
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed! the problem was the a missing zero.

    PHP Code:
     function pauldump($image,$size) { 
       if (
    $fp fopen($image'r')) { 
           
    $data fread($fp$size); 
           
    fclose($fp); 

           for(
    $i=0;$i<strlen($data);$i++) {
               
    $a ord($data[$i]);
               
    $hex .= ($a <= 15) ? "0".dechex($a) : dechex($a);
           }
        } else 
           
    $hex "File error"
        return 
    $hex
     } 

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to use your own error you need to use @:

    PHP Code:
    if ( @ $fp fopen($image'r')) { 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Converting a GIF Image to Hexadecimal Format

    Originally posted by m0h
    But its not the right format, please help me A.S.A.P!
    thanx
    Both scripts return the same, I've checked with an HEX editor.

    what do you mean by right format?

    (Hi seanf )

  9. #9
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Re: Converting a GIF Image to Hexadecimal Format

    Originally posted by Paul S
    Hi seanf
    Hi Paul

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •