user2288650
user2288650

Reputation: 450

Modifying Image using PHP and sending it to browser

I would appreciate your thoughts on this method.

I have a page that uploads a pic given by user and displays it after saving it in a database now that's very simple so, I wanted users to be able to modify these images as they wish, hence I decided to add filters to these images using php. Now before these images are uploaded to the database I want the user to see a preview of what the image looked like after adding the filter.

The solution I came up was to send the image to php file using a form and AJAX using POST, do all necessary checks if file was safe. create a new image and add filter and then convert it to base 64 and send it back to browser and display it using a DATA URL

something like:

   <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">  

Is this a good idea or there is something I should worry about?

Upvotes: 0

Views: 80

Answers (2)

Alleo Indong
Alleo Indong

Reputation: 327

hello we have a library for that actually, we make use of GD library to be able to achieve the correct image output that we would like to have, you can check more about our image library here.. by the way, our library does not only contain image library.. it has a bunch of useful libraries that you can actually use on your projects and we opensource it http://eden.openovate.com/documentation/core/images

Upvotes: 0

Brad
Brad

Reputation: 163272

Base-64 encoding adds about 33% overhead. For this reason, it is usually preferable to serve the image in binary form.

You can do this with a PHP script easily.

header('Content-Type: image/png');
echo $yourImageData;

Then in your HTML:

<img src="yourPHPScript.php?id=12345" />

Upvotes: 1

Related Questions