Question

I have a database that stores images in a MySQL BLOB field. I setup a script that selects and displays the images based on an ID in the URL, and I also made it so if you append ?resize=800x600, it would resize the image (in this case, to 800x600).

The host that I use doesn't have Imagemagick installed and won't let me do it myself, so I need to use PHP's GD library to resize the image.
But I've yet to find a function like Imagick's readImageBlob(), so I can't edit the binary string that I get from the database without first creating a temporary file, editing it, getting the binary string from it, sending it to the browser, and then deleting it (which is waaaay too many steps, especially since this will be getting a few thousand hits when it goes into production).

So my question is, is there any way to replicate readImageBlob with PHP's GD without going through the temporary file solution?

Was it helpful?

Solution

imagecreatefromstring() should do the trick. I think the function example in the manual is almost exactly what you need:

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

Where $data is your binary data string from the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top