php script get 25 image per second from android - php

I try to send 25 image/second to server. In my android app, i set it to send image every 40 milisecond, so every 1 second i will have 25 image was sent. But now with my php scrip, it cannot get continuosly image, it just get 1 image and i had to refresh website. This is my php script
<?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);
$path = "publib_html/";
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
move_uploaded_file($tmp_name,$path.$name);
?>
How can i fix it to get continuesly 25 image per second or do you have which blog write about it?
Thank in advance

Related

Can not upload and display image in webpage with php code

I am facing some problem when I try to upload and display an image with the following php code:
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
?>
The image is uploaded and located in the same folder which has the php script.
Now, if I delete all of the code and replace with just 1 line
<?php
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
The image which has been uploaded, is displayed in the webpage ok.
Then, I combine both of it (uploading and displaying the image).
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
Uet this does not display the image, and it seems that the image has not been uploaded yet.
What is the problem and how can I solve it? I would like to upload the image and show the image on the webpage.
There is no image when you refresh the page because in that case $base=$_REQUEST['image']; will be empty (when you refresh the page you are not providing it with img upload data.)
So basically it overrides the uploaded_image.jpg with empty data.
Just check if you have provided a value for the image. In your case:
if(!empty($_REQUEST['image'])) {
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
}
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";

How to send base64 image to server - PHP

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.

How to save many images through a script php

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……';
?>

how to save the image with name posted in the request

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');

Uploading to mySQL and using SimpleImage PHP?

I'm hoping this is an easy one, I'm using mySQL and php to upload an image as a BLOB type using this code:
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
This is all working fine, inserting to the database and everything. I then want to use SimpleImage: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ to perform some resize and compression work on the image before uploading it. I can't see how to combine say:
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['userfile']['tmp_name']);
$image->resizeToWidth(150);
$image->output();
With my existing code, I think what I want to do is get $content to become $image, but I've tried for a while and can't find a way of doing it. Any help much appreciated.
Happy Christmases to those who like Christmas and TIA to all.
You are outputting the resized image to the browser (::output()) but you're not saving it. If you want to store it inside the database, you need to change the temporary file first, e.g. by using the ::save() function of SimpleImage.
Next to that, you write you want to resize the picture in the browser before uploading. That can not be done with PHP, but only with javascript and browsers who support that. Additionally the upload handling on the PHP side might differ then. But I'm not really sure if you really meant it that the image is resized before uploading.
Another Idea I had is using an output buffer:
if (isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$image = new SimpleImage();
$image->load($_FILES['userfile']['tmp_name']);
$image->resizeToWidth(150);
ob_start();
$image->output();
$content = ob_get_clean();
$content = addslashes($content);
...

Categories