Hex to image using Imagick


Some software will encode and send images in hex format. This is not very useful for storing. The solution is to use php pack method to convert the data into a binary string. Then using Imagick you can easily read in the result of the pack and set compression, image format, and dimensions before rendering.

// Pack the hex code
$data = pack('H*', $hex);

// Read the data as blob into imagick
$img = new Imagick();
$blob = $img->getImageBlob();

header("Pragma: public");
header("Cache-Control: maxage=" + 7200);
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $this->cacheLength));
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header("Content-Type: image/jpeg");
echo $blob;