Tuesday, September 8, 2009

convert image to hex, hex to image

This is how you can produce a code that is in fact a picture.
(This code is a complete tool, copy it to a file, call it 'somehow.php' and produce your pictures as hexcode).


<!--// ***Begin of File*** //-->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<input type="file" name="thefile"><input type="submit">
</form>
<?php
$rh = fopen ($_FILES['thefile']['tmp_name'], "r");
$pb = fread($rh, 8192);
fclose($rh);
$pc = bin2hex($pb);
$pd = wordwrap($pc, 76, "\".
\n \"", 1);
echo "\$hexpic=\""."$pd"."\"\n;";
?>
<!--// ***End of File*** //-->


Copy the result in your site code somewhere. For to show the code as a picture you can use something like what dirk (at) camindo de wrote ...

<?php
$hexpic=".......................
.....................";
$data = pack("H" . strlen($hexpic), $hexpic);
//or
//$data = pack("H*", $hexpic);
header("Content-Type: image/png");
// maybe your is jpeg / gif / png
header("Last-Modified: " . date("r", filectime($_SERVER['SCRIPT_FILENAME'])));
header("Content-Length: " . strlen($data));
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=test2.png ");
echo $data;
?>


have fun!

No comments: