I need to convert a .bmp image to a hex array in PHP.
I have tried to create a function for this but it has not worked
function bmp_to_hex($filename) {
$bmp = file_get_contents($filename);
$output = '';
for($i=0; $i < strlen($bmp); $i++) {
$output .= dechex(ord($test{$i}));
}
return $output;
}
Is there a function in PHP for this? If not, how should I do?
Thanks in advance.
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.