I'm sending a base64 string from an Android app to a server through a PHP file.
I got this from a tutorial video:
<?php
require 'Init.php';
header('Content-type : bitmap; charset=utf8');
if(isset($_POST['encoded_string'])){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode($encoded_string);
$path = 'ProfileIcons/' .$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
}
?>
It's storing the image in the directory but when you open, it has no image. It's a blank png. Is there something wrong with the code? If so, what should I use? Thanks.
There are several points you should check:
1.Make sure the server has received the base64 string
$encoded_string = $_POST['encoded_string'];
Check the length of $encoded_string, it should have the same length as the android client says.
Make sure the decoding works
$decoded_string = base64_decode($encoded_string);
Check the length of $decoded_string, it should NOT be zero or something odd.
Make sure the file written works
$is_written = fwrite($file, $decoded_string);
$is_written should be the length of the data that has been written to the file, if it is a false, then something is wrong.
Related
Hi i have the following code to decode and save image
$photo = $_REQUEST['image'];
$binary = base64_decode ($photo);
header ('Content-Type: bitmap; charset=utf-8');
$file = fopen ('uploads/'.time().'.png', 'wb');
fwrite($file, $binary);
fclose ($file);
This works fine but i will like to get the name of the image after it is written and store that name as a variable to use in the rest of the code where it will be saved in mysql database. I can write to mysql i just need to get the file name. Thanks
You are already naming the file yourself. Just store it in a variable before you write the file.
$filename = time().'.png';
$file = fopen ('uploads/'.$filename, 'wb');
I am trying to upload an image from JSON code.
But when it is uploaded its data is stored in the folder. But data is lost(the file exist but has 0 bytes) I don't know if the problem is exactly from where. If anyone know please help me.
This is the code
public function decodeImage($imageData)
{
$imageData = $dat->imageData;
$this->decodeImage($imageData);
$filename = "sampletest";
$binary=base64_decode($imageData);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('./'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
}
hi guys ive created a base64 encoded image captured with web cam now i convert the .png to .jpg all works fine but now i get two images on server both .png and .jpg how do i go about deleting the .png or is their a way to convert to jpg without saving .png image to disk thanx here my code
$rawData = $_POST['imgBase64'];
$filteredData = explode(',', $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(1000, 99999999999);
//Create the image
$fp = fopen('user/'.$randomName.'.png', 'w');
fwrite($fp, $unencoded);
//convert image from png to jpg
$image = imagecreatefrompng('user/'.$randomName.'.png');
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
unlink($fp);
ive tried it with
unlink($image);
unlink($_SERVER['DOCUMENT_ROOT'] . "/user/.$randomName.'.png'");
imagedestroy($fp);
imagedestroy($image);
Use the function unlink() but passing the file name to it instead of the file handler.
So from your example it would be:
EDIT: You might need to close the file first:
fclose( $fp );
unlink( 'user/'.$randomName.'.png' );
as far as i understand all you need is:
$data = base64_decode( $_POST['imgBase64']);
// image resource from your string
$image = imagecreatefromstring($data);
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
In my app android, I take photos with android camera and I send these photos to pc through this script PHP :
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
I obtain the photo correctly to my pc, but when I send an other photo, this overwrite the previous photo..because the name ("uploaded_image.jpg") is the same.
It's possibile to assign different names so I can save all the photos ?
You can use uniqid to create a unique file name each time,
$fileName = uniqid().'.jpg';
$file = fopen($fileName , 'wb');
you can use PHP time() function. It will add timestamp with photo name.
$file = fopen(time().'uploaded_image.jpg', 'wb');
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen(time().'uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
I have this simple php file that read the image from my android application:
<?php
$base=$_REQUEST['image'];
$cod=$_REQUEST['cod_anagrafico'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
//binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
print($binary);
$theFile = base64_decode($image_data);
$file = fopen('docs/test2.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=docs/test.jpg>';
?>
But it save my pic with the standard name "test". So i need to save this image with the name of variable "cod". How can i do that? And how can i delete the image when the process end?
Thanks
Change this line and you should be ok
$file = fopen("docs/{$cod}", 'wb');