Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading Image from src of img tag to php

Tags:

javascript

php

I just want to ask if there is a way to upload an image from src of <img> tag . The src of the img tag was not from the server and look like this one:

<img src="data:image/png;base64,iVBOR...52Eysdsadsa===" />

This was the product of the approach I learned from this site.

Thank you in advance.

Let me know if you want me to explaine this dipper. Thank you

like image 399
Jenerson Jose Avatar asked Sep 27 '13 09:09

Jenerson Jose


1 Answers

You can upload base64 encoded string(src part) to the server and save it as it is or convert it into an image and save it as a file.

Uploading base64 encoded image:

Get the src attribute of the image and post it to the server.

var base64image = $('#blah').attr('src');

Converting base64 encoded string to image

$img = $_POST['base64image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = 'image.png';
$success = file_put_contents($file, $data);

Or simply save the uploaded content into your database and later use the below code to render the image on your page:

echo '<img src="'. $img .'" />';
like image 162
Sunil Manheri Avatar answered Oct 21 '22 09:10

Sunil Manheri