I take a photo with an android application and transform into base64, I would like to know how to save this base64 in the database along with other information that I have on the page ...
This is my code that takes the photos and turns them into base64:
var pictureSource; // picture source
var destinationType; // sets the format of returned value
document.addEventListener("deviceready",onDeviceReady,false);
document.addEventListener("deviceready",onDeviceReady2,false);
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
function onDeviceReady2() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onPhotoDataSuccess(imageData) {
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = "data:img/jpeg;base64," + imageData;
}
function capturePhoto() {
navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
quality: 60,
destinationType: destinationType.DATA_URL,
allowEdit: true,
correctOrientation: true,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 3000,
saveToPhotoAlbum:1
});
}
function onFail(message) {
alert('Failed because: ' + message);
}
This function will call through a button and it will send the data of a form, my location and the image taken (but I am sending the image the wrong way)
function enviar(){
var formula = $('#formCliente').serialize();
var lon = document.getElementById("myDivLo").innerHTML;
var lat = document.getElementById("myDivL").innerHTML;
var img = document.getElementById('smallImage').innerHTML;
$.ajax({
type:'POST',
data:formula + ' &lat=' + lat + "&lon=" + lon+ "&img=" + img ,
url:'http://ip/teste/www/criar_incidente_camera.php',
success: function(data){
if(data == '' || data == 0){
alert('Usuário ou senha incorreto' + data);
window.location = "";
}
if(data == 1){
window.location.href = "mapa_incidente.html";
}
else{
alert('outro' + data);
}
}
});
}
</script>
Inserting values into the database:
$sql = 'INSERT INTO incidente (titulo, descricao, anonimo, tipo,
latitude, longitude, foto)';
$sql .= 'VALUES (:titulo, :descricao, :anonimo, :tipo, :latitude,
:longitude, :foto)';
try {
$recebeConexao = $pdo->prepare($sql);
$recebeConexao->bindParam(':titulo', $_POST['titulo'],
PDO::PARAM_STR);
$recebeConexao->bindParam(':descricao', $_POST['descricao'],
PDO::PARAM_STR);
$recebeConexao->bindParam(':anonimo', $_POST['anonimo'],
PDO::PARAM_STR);
$recebeConexao->bindParam(':tipo', $_POST['tipo'], PDO::PARAM_STR);
$recebeConexao->bindParam(':latitude', $_POST['lat'], PDO::PARAM_STR);
$recebeConexao->bindParam(':longitude', $_POST['lon'],
PDO::PARAM_STR);
$recebeConexao->bindParam(':foto', $_POST['img'], PDO::PARAM_STR);
$recebeConexao->execute();
if($recebeConexao == true){
$cadastro = 1;
}else{
$cadastro = 0;
}
} catch (PDOException $ex) {
echo "Erro inserção";
}
echo (json_encode($cadastro));
I'm using BLOB to store the image ... Thank you!