1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.