handling a json base64 encode image from iphone app with php - php

I am working on a php REST API that will be used with an iPhone app. It is another developer who develops the app.
From that app it is possible to take a image and upload it to the webserver. I will recieve the images from the iphone as base64 with json, but I am unsure how to process them with a PHP script.
The images will be send like:
{
"image1":"Base64Data",
"image2":"Base64Data",
"image3":"Base64Data",
"image4":"Base64Data"
}
will it be something like:
$image1 = json_decode($jsonData);
Is it possible to get the same / a like data from the base64 string as from $_FILES[] upload? When images a upload the normal way from the website, they are being handled with a cropper and thumbnail generator. I want the base64 images to be handled the same way.

your json which looks like this
{
"image1":"Base64Data",
"image2":"Base64Data",
"image3":"Base64Data",
"image4":"Base64Data"
}
will not be something like:
$image1 = json_decode($jsonData);
more like this
$json = json_decode($jsonData, true);
$image1 = $json['image1'];
There will be no values similar to $_FILES except the data of the file.
you can easily do
$imagedata = base64_decode($image1);
file_put_contents("file.jpg", $imagedata);

You can decode Base64 encoded, with this: base64_decode($yourstring)
Also, you can decode the Json and after the Base 64 encoded.

Related

Laravel decode base64 image and hashName and store public folder

How I can store decoded image to laravel public image path and hash image name ?
I tried like this, but didnt work and doesn't get any error message;
if (!empty($request->image)) {
$file = base64_decode($request->image)->hashName();
base64_decode($request->image)->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
This code works fine but I use API and via curl sending image then needed base64 decoding:
if ($request->hasFile('image')) {
$file = $request->file('image')->hashName();
$request->image->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
What you suggest save image and send image url to api and downloads image too path or send base64 image encode and in api decode, like I'm trying at the moment?
You can't call functions on the output of base64_decode, as it returns as string.
I would suggest you post the name of the file along with the base 64 encoded content, which can then be saved using laravel's storage helpers:
Storage::disk('local')->put('image.png', base64_decode($request->image))

Laravel File Storage: How to store (decoded) base64 image?

How to store base64 image using the Laravel's filesytem (File Storage) methods?
For example, I can decode base64 image like this:
base64_decode($encoded_image);
but all of the Laravel's methods for storing files can accept either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance.
So I guess I'd have to convert base64 image (or decoded base64 image) to Illuminate\Http\File or Illuminate\Http\UploadedFile, but how?
Just use put to store the encoded contents:
Storage::put('file.jpg', $encoded_image);
All it's doing is wrapping file_put_contents.
Then to read it back out:
$data = base64_decode(Storage::get('file.jpg'));
Which, you guess it, is wrapping file_get_contents.
You can upload your base64 Image using laravel File Storage like this
$base64_image = $request->input('base64_image'); // your base64 encoded
#list($type, $file_data) = explode(';', $base64_image);
#list(, $file_data) = explode(',', $file_data);
$imageName = str_random(10).'.'.'png';
Storage::disk('local')->put($imageName, base64_decode($file_data));
Hope it will help you

Postman upload file on API using raw

How do you upload a file on postman using raw mode.
I used a json entry like this:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"#/User/images.jpg"
}
the problem was the # in php was deprecated and now recommended to use curlFile, how to do it in a json form?
Thanks.
Since you are sending the image as part of the JSON, you would need to encode it. Base64 encoding is a good choice. On the server side, you will need to decode it as well.
See this answer to How to convert an image to base64 encoding?:
$imagedata = file_get_contents("/path/to/image.jpg");
$base64 = base64_encode($imagedata);
Once you have the data, you can add it to the JSON:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"TWFuIGlzIGRpc3R..."
}
Also see Base64 Reference

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.

PHP create image file using imagecreatefromstring, without knowing the format of the image

I'm developing a laravel RESTful app that accepts image strings from users and must store them.
images are encoded and sent to my app. I know that I have to Receive and decode image like this:
$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
but I dont know how to save them to files, without knowing the format of the image?is there any way to find out the image extension, so that I can use functions like:
imagepng
You can use getimagesizefromstring();
$size = getimagesizefromstring($imageData);
if ($size['mime'])
return $size['mime'];

Categories