Sloth Armstrong
Sloth Armstrong

Reputation: 1066

How to load blob from image variable in PHP

I have an application that currently loads images into MySQL blobs by passing fopen($fileServerTempName, 'br') into PDO parameter bidings. This works great. I received a new requirement however to edit the photos in memory, while using the imagecreatefrompng function to initially create the image object. Now I am not sure how to get a data stream from the resulting image object to load the modified image back into MySQL. How do I get a data stream from something like this:

$sourceImage = imagecreatefrompng($fileServerTempName);
// do some modifications to $sourceImage

that is in the same format that the fopen would produce? I have tried converting the variable to binary, encoding and decoding, but have had no luck. Any help would be appreciated. If at all possible I would like to avoid creating a temporary copy of the actual modified image on disk.

Thanks.

Upvotes: 1

Views: 584

Answers (1)

Martijn
Martijn

Reputation: 96

ob_start ();
    imagesavealpha($sourceImage, true); // enable transparency
    imagepng ($sourceImage);
    $image_data = ob_get_contents (); 
ob_end_clean ();

You can now save your image back to MySQL.

Upvotes: 1

Related Questions