I am saving images into a database with Doctrine, mapped with this:
/** @Column(type="blob") **/
protected $data;
Everything seems to be OK. I can persist the image data in the database this way:
$largeImage = new ImageData();
$handle = fopen($imagePath, "r");
$bytes = fread($handle, filesize($imagePath));
$largeImage->setData(base64_encode($bytes));
fclose($handle);
$entityManager->persist($largeImage);
$entityManager->flush();
OK. The data is saved, but when I need to read it, I can't.
var_dump($image->getData());
// outputs resource(1) of type (stream)
So, I tried this:
$fp = fopen('image.jpg', 'w');
fwrite($fp, base64_decode(stream_get_contents($image->getData())));
fclose($fp);
And the contents of the file is not from an image, so the image is not rendered by the Windows photo viewer.
I solved it by doing this:
$image = stream_get_contents($image->getData());
$hex = substr($image, 1);
$image = pack("H*", $hex);
echo $image;
my $image content is a hex string started with x, if yours starts with 0x, do: substr($image, 2);