How to upload filepond image to MySql with PHP? - php

I have included the javascript in my project and FilePond works great on the FrontEnd, as you can see below:
Here is the code in my HTML:
When I submit the form this is what I get for filepond:
Code:
https://snipsave.com/user/viktorgavrilovic/snippet/TpgWPYaBBmWe1N44YO/
Now, how can I save the image to a folder, let's say: C:/temp/?
I know that I have to decode because the data is base64 encoded, here is my PHP so far, with decoded format:
$filepond = $_POST["filepond"];
$json_decoded = json_decode($filepond[0], true);
var_dump($json_decoded);
and this is what I get from the decoded format:
Code:
https://snipsave.com/user/viktorgavrilovic/snippet/u3Th8eqrzvmq6IAWAI/
Could you please tell me what should I do next in order to save the image in a folder and in a MySql DB? Thank you very much!

Related

Converting HTML output to BLOB image [duplicate]

This question already has answers here:
Convert HTML to image in php
(2 answers)
Closed 3 years ago.
I want to make a some step of saving data into DB as BLOB data type. I need convert data to BLOB in the DB. I have the data that are considered as HTML output in the some file. The selected programming language is PHP with Zend Framework 1 and database is MySQL.
I tried to save HTML page as valid BLOB image in DB, but it is still not working. I can't find any solutions about it and I don't know how to do. When I got a content from URL and saved it into DB as BLOB by using function file_get_contents($url), that is worked to me fine.
Here is a code that saves the content of the HTML page in the file described below into DB as BLOB.
$file = "C:\test.html";
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
$dbModel = new MyTable($this->db);
$dbRowSet = $dbModel->find(1);
$dbRow = $dbRowSet->current();
$dbRow->map = $content;
$dbRow->save();
Here is a preview of saved HTML page in DB, but there I can't show any BLOB image.
There is shown BLOB image. I had the URL page and tried to save it into DB.
I expected that the result of saving data (HTML page) into DB should be that DB contains valid BLOB images.
Thank you for your help.
You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:
$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
$url="http://google.com";
$output_path="test.png";
shell_exec("$path $url $output_path");
One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.
Credit: #Tom
Convert HTML to image in php
After this you just need file_get_contents() and save blob in the database.

Upload image to php webservice and store it in MSSQL database

I am trying to upload image to php web service and store it in MS SQL database. My post data is in json format which has base64 encoded image data.
Base 64 encode and insert sample
$base=base64_encode(file_get_contents("C:\sample\images.jpg"));
$CommandText = "INSERT INTO [M_IMAGES] ([IMAGE_DATA]) VALUES(?) " ;
$ImageStream = imagecreatefromstring(base64_decode($this->imageData));
$RowsAffected = (int)$objDBManager->Exe*emphasized text*cuteNonQuery($CommandText,array(array($ImageStream,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
SQLSRV_SQLTYPE_VARBINARY('max'))));
Here $this->imageData contanins base64 encoded image data and IMAGE_DATA datatype is image. I am able to upload and retrieve image successfully in normal upload
Normal upload
$ImagePath = $_FILES[$ImageCtrlName]['tmp_name'];
$ImageStream = fopen($ImagePath, "r");
$RowsAffected = (int)$objDBManager->ExecuteNonQuery($CommandText,
array(array(&$ImageStream,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
SQLSRV_SQLTYPE_VARBINARY('max'))));
fclose($ImageStream);
I am getting error sqlsrv_query(): supplied resource is not a valid stream resource
Can anyone point out what is wrong here. Or is there a better way to do it. Help me as I am new to PHP.
The best solution is to save in the DB only the url of the image and save the image in your server.
When you want to print it you only have to search the url in the DB.
Trying to save the image in a DB is a wrong solution.
From what I can understand from your question, your problem stems from trying to store the image on to MSSQL as it's binary form, which is not allowed. What I can do, is advice you to store the image in it's base64 form, which is a string of alphanumeric characters. There is no need to try and store the image as an image as the base64 encoded image, is usually serve-able directly to the client.
TL;DR Just store the image as base64.

saving blob file in oracle using php

please tell me how can i insert pdf or doc files into oracle blob field .
this is the code which i use for varchr data type and this is fine but how can i save the files into blob data type please help me !!!
if(isset($_POST['elm1'])) {
$pdata=$_POST['elm1'];
$profile_name=$_POST['profilename'];
$profile_id=$_POST['profileid'];
$query = "insert into prepaid_profiles values('$profile_id' , '$profile_name','$pdata')";
$result = oci_parse($dbc,$query);
oci_execute($result);
oci_close($dbc);
When user uploads file, save it in server, show only link to it. When saved text from editor, you save only link (with rest of text). File is still on server.
But if you really need to make your database huge and slowly, read uploaded file with file_get_contents, that convert it to base64 and save to database long string (no idea why it's better).
TinyMCE don't need file content in <textarea>.

How do I create a file from submitted raw base64 data?

I am trying to reconstruct a WAV/MP3 file from raw bases64 encoded data submitted in in a form. I understand th the steps should be something like:
1. Create new file
2. use file_put_content onto the new file with the decoded data
Is this correct? And what is the best/easiest way to do so?

Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.

Categories