Question

My problem is rather straight-forward:

  1. Retrieve an image from a MySQL database (currently stored as binary data in a blob column)
  2. Rotate that image 90 degrees (using PHP's imagerotate)
  3. Store the image back in the database with that rotation changes applied.

I'm having trouble finding functions that will let me save the image as a datastream (not to the file system) since these images are not allowed to touch the web server (hence, why they are in the db). Any advice is welcome.

Thanks

Was it helpful?

Solution

Are you converting the image to another type before rotating? You said you're using tiffs, GD can only read the headers of tiff images, you'll need to convert it first. Probably with ImageMagick

Most likely you will want to save the files to a temp directory:

  • Query database for file
  • Save to temp directory (you can protect the files by setting permissions if necessary)
  • Rotate image
  • Insert back into database
  • Delete temp file

OTHER TIPS

SELECT the data from your DB. Pass the data into imagecreatefromstring()

You will now have an image resource that you can call imagerotate on.

To save it back to the DB you will need to output it using imagepng() / imagejpeg() or the equivalent function for the image type you're using. These functions output to browser or file so you can use output buffering to capture a string to save back to the DB.

ob_start();
imagepng($resource);
$img_data = ob_get_contents();
ob_end_clean();

$img_data can now be saved to the DB.

This is only a rough outline but I hope I've explained the idea.

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