Upload image to php webservice and store it in MSSQL database - php

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.

Related

How to store QR code as image file in to Mysql Database

I`m implementing a simple application using Laravel.
just wondering, when I send qr code in email text, does qr code need to be stored in database first to Specify file pass for the image??
If that answer is yes, is there any way that I`m able to store qr code without using form tag?
I don't think you need to store the actual QR code.
A QR code is merely a way of representing a string of characters. Often people will put a URL into the QR code.
You can probably just store the source data into your db, and generate the QR from the data.
If the data is a URL, the device consuming the QR should be able to link to the url which will bring it back to your application. You could put parameters on the end of the URL to allow your app to retrieve the data from your db for that user.
You could even use a signed URL so that the end user cannot change it.
Here is an article that I found that may help. It's not laravel specific, but will help with the QR code understanding.
https://www.kerneldev.com/2018/09/07/qr-codes-in-laravel-complete-guide/
You can do it converting image to base64 and then store it as text.
for more information how to encode visit http://php.net/manual/en/function.base64-encode.php and for decode http://php.net/manual/en/function.base64-decode.php
example encode:
$file_encoded = base64_encode(file_get_contents($file)); //this is stringed data. save this in database.
example decode:
$file_encoded = base64_decode ($file_encoded); //this will be file.
You could also store the image as a BLOB, which has less overhead as a base64 encoded image and would not be indexed as a searchable string.
Even better might be to just store links to binaries in your database as opposed to the data itself.
$data = new ModelName();
$path = '/img/';
if(!\File::exists(public_path($path))) {
\File::makeDirectory(public_path($path));
}
$file_path = $path . time() . '.png';
$image = \QrCode::format('png')
->merge('img/t.jpg', 0.1, true)
->size(200)->errorCorrection('H')
->generate('A simple example of QR code!', $file_path)
$data->file = $file_path;
$data->save();
I hope this will help you, it works fine for me.

Encode image in base64 format and store in mysql database , not woring for all type of images with php and laravel

I have upload one image and want to store it in base64 encoded format in database. for some images it is not working to store in database.
In my local system it is working for all images but on live server it not stores some of images in base64 format in mysql database . my code to store image in database is below,
$handle = $_FILES["file_name"]["tmp_name"];
$fileObj = $req->files->get('file_name');
$filename = $fileObj->getClientOriginalName();
$fileInfo = pathinfo($filename);
$fileExt = $fileInfo['extension'];
$file1 = $req->files->get('file_name');
$mime = $file1->getMimeType();
$fileName = base64_encode(file_get_contents($handle));
$File = new File();
$File->file_name = $fileName;
$File->save();
from above code if I chane below line then it is working
$fileName = (file_get_contents($handle));
but I want to store it in base64 encoded format in mysql database
in my data table this field's type is "long blob".
If you base64 the image, the column should be longtext. Blob are for binary.
But think again before storing files to database.
Storing image to database is a bad practice because whenever your app layer (in your case, php) retrieve the images, it will increase your memory usage and if you are retrieving list, your server can easily out of memory if multiple users are using the same function.
Storing data as base64 make it worse because the data size is bigger (8/6)
The best practice is to store the file in a file server and only the necessary meta or link in the database.
Storing image details instead saving a blob is better. You can store all images on another server or the same server in a folder and store the path , name of that image in mysql. To retrieve it you can simple get the file from the path stored in mysql.

how to convert base64 encoded image multi file formate in server side PHP

Hi i'm sending encoded base image string from android side and in server side i'm decoding the encoded image string and storing to folder, while saving to database i'm storing .JPG format.
But the issue is if i upload .PNG image from android side it is storing in .JPG format but image is not visible in server side folder, and if i upload .JPG format it is visible in server side folder. Please help me out this problem.
PHP Code
if(isset($_REQUEST['image1']))
{
$profile_picture = $_REQUEST['image1'];
$target_path = "./upload/"."file"."_".time().".jpg";
// **I want store multiple file forame** //
$upload_img= "file"."_".time().".jpg";
file_put_contents($target_path, base64_decode($profile_picture));
}
else
{
$upload_img = "image.png";
}
You can send file extension(.jpg, .png) in webservice from android side and retrieve it in your back-end PHP side then store it in variable and use it dynamically.

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 to save and display image into database without using move uploaded or copy functions in php

How to save and display image into database without using move uploaded or copy functions in php ?
Is it possible to do that without saving the images into directories and saving them to database with some datatype for that particular field ?
use blob datatype it is direct save image in the database.
here one example of PHP MySQL BLOB.hope it is help you.
Maybe you could try to get the base64 encoded image and save it to your DB.
$image_as_string = base64_encode( file_get_contents( 'image.jpg' ) )
// now save dat string to your database.
Hope it helps.

Categories