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
Related
I am developing a REST API to an Android App. I want to save a base64 image as a jpeg image that sending from the App. I wrote a code to that with laravel intervention and tested with postman. No Problem. But when data is sending from the Android App, They said me they got 500 error with a message Image intervention - Image source not readable.
After that I saw their JSON contains backslashes and \ns. So I have tried to escape them with stripslashes, stripcslashes, utf8_encode , utf8_decode but no luck.
After I have left the laravel intervention and start to try with imagecreatefromstring (gd) library. Also no luck with imagecreatefromstring(): Data is not in a recognized format message.
Here is my try.
<?php
$json_string = '{"id_city":2,"image":"UklGRpQEAABXRUJQVlA4IIgEAAAQGgCdASpyAHMAPxF+tlMsKCUiqxLrCYAiCWkRQAHDhsYwTe19\nO7uR07OS4zITzPgo\/k\/\/0J5nUNHFl64IOAxaccVoK1IldwGQzh87cuQK\/Qv6ruo6EARQuRqZXsI1\nT1a85ouI4ZYwKmvLwgnuBPnn\/HRLOOjc4GJZ2WzZhF5g+XbewWBxrDfIJEcLkLPeY8ebLZwDLcjY\nSUrgaaxHaTX1+xU8bcKt59iNx\/IMFYnpdDJvKcJwFW9rBRy\/U5DjRlo5ZQU7dKgqlxXmdu5MGOh2\n43RLSSEWILgAANfz4R4KgwnZHkZWDlHwIRGB+lbtcZ4Y4mWUjleS5JFM3KmrDaJ419MI2ZJJ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mBAbU9rl1JPZY2HJmfZI5EZAXTH2Idi9zSzSjcP4cT+\nTWBl1kfl\/v+cAFGUJj9vx2dhvpq7hhFBzZ9NmBHCdE8qEEpz7JkHCgVIR6SSOZoEg9CMS\/NEGm9w\nUDgX+FGlCdz94\/3XNLPbe\/kEpZ\/5C+QJJVAV27kOIl9IC8If9YGAAA==\n"}';
$data = json_decode($json_string);
var_dump($data->image);
// After copy and pasted the var_dump result to the postman it works
$base64_decoded = base64_decode($data->image);
imagecreatefromstring($base64_decoded);
?>
Your Base64'ed data is actually a .webp image, but the imagecreatefromstring() function doesn't detect this encoding.
Change:
imagecreatefromstring(
To:
imagecreatefromwebp(
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
I want to send base64 decoded data to php file from ajax, but my half of the data get truncated, not able to send entire data through ajax to php file.
I am sending this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAgAElEQ…/Hv59f3b//PQ+Q/3vcf35r/vmLQP0RPc/m74Zy4dP//wBHqel8UTBFDwAAAABJRU5ErkJggg==
and converting the base64 image to normal image like jpg
Any Help ?
Just a guess. Don't hurt me Stackoverflow gods. :-) Use JSON, not urlencoding. Make an object literal and then put your data inside. Send the data your PHP file and use json_decode.
var image {image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAgAElEQ…/Hv59f3b//PQ+Q/3vcf35r/vmLQP0RPc/m74Zy4dP//wBHqel8UTBFDwAAAABJRU5ErkJggg=="};
I am using an API where I can send a document to something like dropbox. According to the documentation, the file which is sent needs to be BASE64 encoded data.
As such, I am trying something like this
$b64Doc = chunk_split(base64_encode($this->pdfdoc));
Where $this->pdfdoc is the path to my PDF document.
At the moment, the file is being sent over but it seems invalid (displays nothing).
Am I correctly converting my PDF to BASE64 encoded data?
Thanks
base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file
$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
base64_encode() will encode whatever string you pass to it. If the value you pass is the file name, all you are going to get is an encoded filename, not the contents of the file.
You'll probably want to do file_get_contents($this->pdfdoc) or something first.
Convert base64 to pdf and save to server path.
// Real date format (xxx-xx-xx)
$toDay = date("Y-m-d");
// we give the file a random name
$name = "archive_".$toDay."_XXXXX_.pdf";
// a route is created, (it must already be created in its repository(pdf)).
$rute = "pdf/".$name;
// decode base64
$pdf_b64 = base64_decode($base_64);
// you record the file in existing folder
if(file_put_contents($rute, $pdf_b64)){
//just to force download by the browser
header("Content-type: application/pdf");
//print base64 decoded
echo $pdf_b64;
}
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.