Coverting Hex to Image in PHP

Coverting Hex to Image in PHP?

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

Converting multiple hex color to image

You can do it like this, but hopefully your variables have more sensible names and you can use a loop:

<?php
$im = imagecreate(3,3);
$black = imagecolorallocate($im,0,0,0);
$white = imagecolorallocate($im,0xff,0xff,0xff);
$red = imagecolorallocate($im,0xff,0,0);
$green = imagecolorallocate($im,0,0xff,0);
$blue = imagecolorallocate($im,0,0,0xff);

# First row
imagesetpixel($im,0,0,$black);
imagesetpixel($im,1,0,$black);
imagesetpixel($im,2,0,$black);

# Second row
imagesetpixel($im,0,0,$black);
imagesetpixel($im,1,1,$white);
imagesetpixel($im,2,1,$black);

# Third row
imagesetpixel($im,0,2,$red);
imagesetpixel($im,1,2,$green);
imagesetpixel($im,2,2,$blue);

imagepng($im,"result.png");
?>

Sample Image

Print an hexadecimal image in HTML

Cut off the leading 0x, and then feed the rest to the pack function.

pack('H*', '89504E470D0A1A0A0000000D49484452')

results in (when echo’d) �PNG…, or %89PNG%0D%0A%1A%0A%00%00%00%0DIHDR when URL-encoded, and those are the starting bytes of a PNG image.

You should be able to base64 encode this into a proper data URI now.

Convert Hexadecimal string to image

below code can be use to extract image from hex string value

<?php 
$binary_string2=pack("H*",$hex_string2)
?>
<img src="data:image/jpeg;base64,<?php echo base64_encode($binary_string);?>" alt="IMG DESC" >
<img src="data:image/jpeg;base64,<?php echo base64_encode($binary_string2);?>" alt="IMG DESC">

How to draw, in php, an image from the Hexadecimal code

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

String to Image in PHP

Ok, it seems that it's been converted from a binary form to a hex form, convert it back like this:

<?php
header("Content-Type: image/gif");
//Notice the removed "0x", you can do this with code using substr()
$FlagImage = "47494638396184010401F70031FFFFFFF7F7F7E7E7E7DEDEDEDE1018D6D6D6D62121CECECEC63131BDBDBDBD3139B5B5B5B54242A5A5A59494945A845A52845A52845242946B399C6B319C6B318C5A21945A1094520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002C00000000840104010008FF0001081C48B0A0C18308132A5CC8B0A1C38710234A9C48B1A2C58B18336ADCB8B081838F20438A1C49B2A4C9932853AA5CC9B2A5CB973063CA9C49B3A6CD9B38472610E84000C79F40830A1D4AB4A8D1A348270ED809C041D2A750A34A9D4AB5AAD4A53CAD6ADDCAB5ABD7AF43B136054BB6ACD9B368A78A759AB6ADDBB770E31E5C2BB7AEDDBB78AF32659BB7AFDFBF8021D20D4CB8B0E1BE830F2B5ECC986CE2C690234B46FA78B2E5CB982B56CECCB9B367829B3F8B1E1D3934E9D3A8039B4ECDBAB5DCD5AE63CB2E0B7BB6EDDB556BE3DECDDBA8EEDEC0836BFC2DBCB871C17B8F2B5F4E9138F3E7CB9D439F2E5C3AF5EBBBAD63DF2E5B3BF7EFA9BD83FF1F2F5A3CF9F399CDA35F2F593DFBF78BDDC39F4F583EFDFB8893E3DF8FD93EFFFF6FF907E080670948E0815F1988E0825A29C8E0837A6505E184753948E185445988E1863F69C8E187187908E2884AE947E289B99988E28A5089C8E28B06B908E38C00C848E38B36DEB8628E3A9EC8638F23FE08E487420EB96191465E886492132EC9E4834E3EB96094521E486595035E89E57F5A6EB95F975EDE076698F38D49E67B669EB95E9A6A9EC7669BE3BD09E77772CEB95D9D765E87679ED3EDC9E7737EFE199D8A82F61868A1C61D8A6875842E3AA3A28EF60669A4D9354AE98E965EEA63A69A06C969A7447E0AEA91A28EAAE45E13A4AAEAAAACB6EAEAABB0C6FF2AEBACB4D66AEBADB8E6AAEBAEBCF6EAEBAFC0062B2CAB12EC65C105C826ABECB2CC36EBECB3D0462BEDB4D4566BEDB5D866ABEDB6DC76EBEDB7E086AB6C05C68A6BEEB9E8A6ABEEBAECB6EBEEBBD992CBD3B1F0D66BEFBDF8E6ABEFBEF6CADB14BDFC062CF0C004176CF0B9FE3A00F0C10C37ECF0C310D79BF0C211576CF1C518679CECC41A77ECF1C720DFCB71C824976CF2C9D68E8CF2CA2CB70CB2CA2EC72CF3CC06C34CF3CD38E7DCAECD3AF7ECF3CFF1960BF4D04417DD2CCF4627ADF4CD482FEDF4D327370DF5D454672C75D55867CDF0D55A77EDB5BE5C7F2DF6D8EC864DF6D9687F6B76DA6CB75DEDDA6EC72DF7B270CF6DB7DB75DFADF7D979EFFFEDB7D77DFF2D78D5810F6EB8D3851FAE78D1892FEEB8CF8D3F2E39D3424F6EF9D3915FAE39CA996FEE79C89D7F2EBAC6A18F6E7AC5A59FAE7AC3A9AFEE3AC1ADBF2EFBBEB1CF6E7BBF95DFAE3BC6B5EFEE7BBABDFF2E7CB8C10F6F3CB7C51FAFFCB5C92FEFBCB4CD3F2FFDD1B94F6F7DD9D55FAF3DC2D96FEFBDDADD7F2FBEB6D18FAF7CF9E61B8F7EFAC2AFCFBEEFEEBFAF7BFCF2DB4E7FFDB2DF8FBFEBFAEFAF7AFFFE331D000328BA0112D073063CA0E612A840CB31B081927B20041D27C1092AAE8216341C063328B80D72D06F1EFCA0DE422842BB91B084723B210AF116BE154E4F852E4C1B0C63C8B716D2707933BCA1D872A843C0D9B087C3E32110FFB326C42112EE8746DC5DC21EC0C4263AF189508CA214A748C52A5AF18A58CCA216B7C8C52E7AF18B600CA318C748C63242712F0620801AD7C8C636BAF18D708CA31CE748C73ADAF18E78CCA31EF7C8C73EFAF18F800CA42007C94604A09190884CA42217C9C8463AF291908CE41E0DC993344AF29298CCA42637C9C94E6292924DB1A4274749CA529AF294A84C24281D20CA54BAF295B08CA52C2FB9CA56CEF296B8CCA52E77B9C65AF2F297C00CA63033E9CB611AF398C84C261E8BA9CC663AF399C2642634A749CD6AA2529AD6CCA636B7F9486C72F39BE00CE7240F29CE729AF39C6FF4263AD7C9CE6CAAB39DF08C6732DF29CF7ADA7397F4BCA73EF7E9FFCA7CF2F39F00E5A43F034AD0823A72A0064DA8420389D0853AF4A1776C2844274AD1364AB4A21885E84533CAD1846EB4A32005E847434AD27B8EB4A42885E74953CAD273AEB4A53005E74B634A537792B3A6388DE74C73CA5365EEB4A7401DE64F834A545E0EB5A848BDE55193CAD4572EB5A95035E553A34AD54E4EB5AA58FDE44DB3CA555D5EB5AB605DE457C34AD6418EB5AC68F5E359D3CAD63CAEB5AD70A5E35BE34AD7746EB5AE783DE85DF3CA5755EEB5AF8065E85F034B583ECEB5B06C3D2C62D1AAD8C592B5B18E052B6423CBD5C95216AB96BD2C5533AB59A872B6B34CFD2C68912ADAD112B5B4A6052A6A53CBD3D5B216A7AE7D2D4D632B5B98A3D2B6B62CBD2D6E51AADBDD92B4B7BE05297083CBD1E11217A3C63D2E4593AB5C8D0EB6B975652E74172ADDE97AF4B9D66D6B75B34BD0ED7257A4D8FD6E59BD2BDE7D92B7BC260D2F7ABBBA4A0520E0BDF08DAF7CE74BDFFADAF7BEF8CDAF7EF7CBDFFEFAF7BF000EB080074CE0021BF8C0088E2F03989280063BF8C1108EB084274CE10A5BF8C218CEB08637CCE10E7BF8C3200EB188474CE2129B58C20730D58D0202003B00";
echo hex2bin($FlagImage);

If you don't have at least PHP 5.4 hex2bin doesn't exist, so you'll have to use this instead:

pack("H*", $FlagImage);

How to obtain an array of hexadecimal codes from an image?

There is no ready-made function for it. You can do the following:

function bmp_to_hex($filename) {   
$bmpStr = file_get_contents($filename);
$hexStr = bin2hex($bmpStr);
$hexArray = str_split($hexStr,2);
return $hexArray;
}

The $bmpStr ist a string like "BMF>(..".
$hexStr is a string like "424d460000 .." with the 2-character values.
An array is then created with str_split. The array then starts like this: {[0] => string (2) "42" [1] => string (2) "4d".

Note: Bitmaps can get very large. $hexStr becomes twice as large. The array of these then requires a very huge amount of memory.



Related Topics



Leave a reply



Submit