During Week 5, I am working on the camera feature. I have always been interested in trying to make a camera and upload features. I have gotten a grasp of how to upload files up to the database after researching and testing multiple times. Youtube tutorials are very useful when it comes to learning ionic, SQL and angular.
A default camera by Capacitor plugin.
I would love to make a custom camera with UI and features developed only if we had time, since I don't think there will be enough time for us to work on this now time-consuming feature.
It took me a few days to make this work. I used base64 encoding and something similar to imagecreatefromstring. But these codes aren't working for some reasons, so I had to find different methods but produce the same result.
Snippet on uploading the photos from camera to Database
if($_SERVER['REQUEST_METHOD'] === 'POST'){
// $datenow = date('Y-m-d');
$data = json_decode(file_get_contents("php://input"));
// $image = base64_decode($data->postname);
// $image = base64_decode($testimg);
// $img = imagecreatefromstring($image);
$target_dir = "uploads/";
// $target_file = basename($_FILES["dataurl"]["name"]);
$target_file = "img_".$data->usersid."_".uniqid().".png";
// move_uploaded_file($img, $target_path)
// if ($img !== false) {
// header('Content-Type: image/png');
// imagepng($img, $target_path);
// imagedestroy($img);
// }
$target_path = $target_dir . $target_file;
$server_ip = "https://student.amphibistudio.sg/10187403A/POP/db/";
$full_target_path = $server_ip . $target_path;
$imagedata = $data->postname;
$imagedata = str_replace('data:image/png;base64,', '', $imagedata);
$imagedata = str_replace('data:image/jpeg;base64,', '', $imagedata);
$imagedata = str_replace('data:image/jpg;base64,', '', $imagedata);
$imagedata = str_replace(' ', '+', $imagedata);
$sql = $conn->query("INSERT INTO POP_Uploads (usersid, postid, postname, posttype, postdate, posturl, postdesc) VALUES ('".$data->usersid."', '".$data->postid."', '$target_file', 'post', now(), '$full_target_path', '$imagedata')");
if($sql){
$imagedata = base64_decode($imagedata);
file_put_contents($target_path, $imagedata);
//$data->id = $conn->insert_id;
exit(json_encode($data));
}else{
exit(json_encode(array('status' => 'error')));
}
}
Comments