1

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.

2
  • 1
    Why store the images themselves in MySQL? Unless you are perofmring some sort of binary search against those blobs, chances are you would be better served storing the files on disk and just storing path references to them in the database. Commented Oct 7, 2014 at 19:21
  • @MikeBrant I couldn't agree with you more, except I don't have the authority to make that decision here. I'm guessing there are reasons behind this I just don't know them. Commented Oct 7, 2014 at 19:26

1 Answer 1

1
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.

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

2 Comments

This worked flawlessly. I will test some more and mark as accepted if this ends up being the best option. Thanks.
Sorry for the delay on marking yours as the correct answer :P

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.