Create server to receive jpgs as json
Sometimes you might need to create a small server that receives pictures as a json file. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to create a server that receives pictures as json file.
This strategy assumes that you have have php extensions installed for your web server.
First, we need to create a new php file in your web server directory. Second, we need to receive json as a string and decode the json. Third, create an file handler and save the json file as a picture.
The first thing you need to do is create a php file in the web server directory such as receiver.php.
Then you need to receive the json file as a string and decode it:
1 $string = @file_get_contents('php://input'); 2 $json = json_decode($string, true);
Then you will need create a file handler and save the data as jpg.
1 $file = 'uploads/filename'; 2 $file_info = new finfo(FILEINFO_MIME); 3 4 $mime_type = $file_info->buffer($json['image']); 5 file_put_contents($file, base64_decode($json['image']));
So, this is how a final code for receiving picture as json will look like:
1 buffer($json['image']); 10 file_put_contents($file, base64_decode($json['image'])); 11 echo "sucess saving image"; 12 exit; 13 ?>