Coverting Hex to Image in PHP?

前端 未结 2 899
别那么骄傲
别那么骄傲 2020-12-02 02:13

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to s

相关标签:
2条回答
  • 2020-12-02 02:42

    Convert the HEX string to binary:

    $binary = pack("H*", $hex);
    

    pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.

    Write it to disk:

    file_put_contents("file.png", $binary);
    
    0 讨论(0)
  • 2020-12-02 02:57

    Suppose you have received a hex string in a page where you want to convert this hex to real image. Please check this code snippet will help you or not.

    <?php  
    
        $hexpic=".......................
        .....................";
    
        # convert the hex string to binary
    
        $data = pack("H" . strlen($hexpic), $hexpic);
    
        #write the binary string into an image file
    
        file_put_contents("sample.png", $data);
    ?>
    
    0 讨论(0)
提交回复
热议问题