I'm trying to upload image file from my android app to a server. This is my php file that is processing incoming images from my website and iOS app to be used in move_uploaded_file function:
<?php include '../../../init.php';
$post_id = $_GET['post_id'];
$image_temp = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$image_ext = strtolower(end(explode('.', $image_name)));
upload_page_image($image_temp, $image_ext, $post_id);
Now, in android I came up to the point when I have Base64 encoded string:
params.put("image",imageToString(bitmap));
Then, I bring this to my PHP file like this:
<$php
....
$image = base64_decode(_POST['image']);
...
But now, how do I brake it in to $_FILES['image']['temp_name'] and $_FILES['image']['name']? All the other examples I went through are using file_put_contents. I need it to be move_uploaded_files.
Any help is appreciated.
Thanks
this worked for me enter link description here
You must submit image using Multipart Form Data for use move_uploaded_files in php
Ok, that was the most difficult stuff I ever went through. The problem is that when I use php function file_put_contents(), it first makes everything easy on android app side, just convert bitmap to Base64 and pass it to php file. But... one very important thing got lost in this process.... it's that $exif['Orientation'] when using exif_read_data. This parameter is not there any more.
Now, when going back to android app and trying to get image orientation before sending the file to php, the standard function such as below:
ExifInterface exifReader = new ExifInterface(mFilePath);
exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
.. or similar doesn't work. It always returns 0. ALWAYS
Solution came from using Glide https://github.com/bumptech/glide library and get current image orientation using:
InputStream is = getActivity().getContentResolver().openInputStream(originalUri);
int orientation = new ImageHeaderParser(is).getOrientation();
Once you get orientation, you pass image as Base64 and orientation number and then process upload and do orientation change and so on.
Related
I'm trying to use php to save a base64 image on my server.
But every time I run my code, I get this error:
PHP Warning: file_get_contents(data:image/gif;base64R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P goOXMv8 fhw/v739/f 8PD98fH/8mJl fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8 QePLl38MGBr8JCP zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/ MjMUcAN8zM/9wcM8ZGcATEL QePdZWf/29uc/P9cmJu9MTDImIN /r7 /vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo Pj/ pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP 8vH9QUK vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5MLypoG8wKOuwsP/g4P/Q0IcwKEswKMl8aJ9fX2xjdOtGRs/Pz Dg4GImIP8gIH0sKEAwKKmTiKZ8aB/f39Wsl LFt8dgUE9PT5x5aHBwcP AgP WltdgYMyZfyywz78AAAAAAAD///8AAP9mZv///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA in my-file.php
I POST the base64 image via AJAX to my PHP page and this what I have in my PHP page:
$base64_string = $_POST['logo'];
$output_file = 'image.jpg';
file_put_contents('../logos/'.$output_file, file_get_contents($base64_string));
When I look inside the logos folder, I see the image.jpg being created but it has no content.
when I look at the error that PHP throws out, the base64 image seems to be a lot shorter than the actual base64 that I am POSTing!
Can someone please advice on this issue?
I also changed my server's post_max_size to 120M but I still get the same error.
EDIT:
Here's the base64 image that I am sending to my PHP page:
data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+f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xnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==
Of course this is not working.
It's because you try to read a file at path $base64_string.
$base64_string = $_POST['logo'];
$output_file = 'image.jpg';
file_put_contents('../logos/'.$output_file, $base64_string);
Will work.
I'm developing a laravel RESTful app that accepts image strings from users and must store them.
images are encoded and sent to my app. I know that I have to Receive and decode image like this:
$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
but I dont know how to save them to files, without knowing the format of the image?is there any way to find out the image extension, so that I can use functions like:
imagepng
You can use getimagesizefromstring();
$size = getimagesizefromstring($imageData);
if ($size['mime'])
return $size['mime'];
I am using plupload to upload file in my php based website, with large file uploading the file becomes a file named 'blob' without any suffix. I know this is a binary file that contains the raw data, question is how to retrieve the data and save it back as an image file, say .png/.jpg or etc? I tried:
$imageString = file_get_contents($blogPath);
$image = imagecreatefromstring($imageString);
But it gives me some 'Data is not in recognized format...' error, any thoughts? Thanks in advance.
Your call to imagecreatefromstring() should work just fine if your file_get_contents() is working. Use var_dump($imageString) to verify. Did you mean to name your variable $blobPath instead of $blogPath?
You don't need to load this image though. Just rename the file.
rename($blobPath, 'new/path/here.jpg');
http://php.net/manual/en/function.rename.php
I am storing the uploaded image files for late use, like attaching them to posts or products(my site is e-commerce CMS). I figured that my image file didn't get fully uploaded to the server, the image before upload is 6mb, but the blob file is just 192kb, so my best guess is that what get uploaded is just a chunk instead of the whole package, and yet that brought up another question: how should I take all the pieces and assemble them as one complete image file? As mentioned earlier, I am using plupload for js plugin and php as backend, the backend php code to handle uploading goes like this:
move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFolder . $_FILES["file"]["name"]);
Instead of doing that you should do this to display image to the browser
<img src="data:image/jpeg;base64,'.base64_encode( $row['blob_image'] ).'"/>
I'm not sure what imagecreatefromsting does or how it encodes the image.
I looked at the documentation for that function; you're missing:
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data); <--- this operation
I'm wondering if there is someone who can help me with this, i'm sending an image from my windows phone c# to php and i want to save the image.
The data connection works and i can receive the POST value
see: http://posttestserver.com/data/2014/01/23/19.41.40516059397 for the information the server receives.
i now want to save this base64 encoded RGBA string to a png or jpg (i would prefer png but i dont need the alpha value).
this is the code i have server side
//getimagecontents
$data = base64_decode(file_get_contents("php://input"));
//create an image from string
$im = imagecreatefromstring($data);
if ($im !== false) {
//add an header for the image
header('Content-Type: image/png');
//save the image
imagepng($im, 'upload/test.png');
imagedestroy($im);
echo "it worked!";
}
Hopefully someone can help me with this coz it's keeping me up all night :(
Thanks in advance :)
Make sure that you are receiving the data. If yes. then try once without base64_decode and upload directory is created in place that is where your image will be saved. If it is not created, you can not save image.
You should have received warnings in both cases indicating the problem. If this your dev environment, turn the warnings reporting on in php.ini or your script.
Another thing, why are you sending header for image when only you intend to save it to file system.
The image i was sending was too big for the server to handle...
As it was send as a RAW file...
converted it to a png and now works like a charm
I'm writing the PHP/webserver side of an image upload for an iOS app. Using an html file I can upload an image to my script just fine. I've even written a Perl LWP script to post an image with no problems.
When the iOS app uploads an image it fails when I call is_uploaded_file. Sending back the $_FILES var in the json response show us what we expect for a file upload. Also I do a file_exists on the tmp_name and that fails as well.
Is there anything else I can look at in the request to determine what is going wrong? I'd like to be able to indicate what's wrong with the post request.
Here's the block of code where it stop processing the image upload:
//Check for valid upload
if(!is_uploaded_file($_FILES['photo']['tmp_name'])) {
$this->errors['upload_2'] = $photoErrorString;
$this->errors['files'] = $_FILES;
$this->errors['image_uploaded'] = file_exists($_FILES['photo']['tmp_name']);
error_log("uploadPhoto: upload_2");
return false;
}
As far as i know you cannot upload a image or photo from iOS app directly using PHP scripts.
You have to send 64 bit encode from the iOS app to the script responsible for the file upload as a simple POST. Then that script will firstly decode your photo's string and then PHP script will create the image from the string only to upload.
Code will be something like:
$filedb = $path_to_dir.$file_name_of_the_photo;
$profile_pic = $_POST['profile_pic'];
$profile_pic= base64_decode($profile_pic);
if(($img = #imagecreatefromstring($profile_pic)) != FALSE)
{
if(imagepng($img,$filedb))//will create image and save it to the path from the filedb with the name in variable file_name_of_the_photo
{
imagedestroy($img);// distroy the string of the image after successful upload
// do other updates to database if required
}
else //cannot create imagepng Error
{
//do what required
}
}
Hope this will work.
Linked below is a really nice NSData base64 category.
If you're unfamiliar with categories in Obj-C, they're basically stock objects that we know and love with some added methods (in this case, base64 encoding/decoding).
This class is really simple to drop into an existing project.
Enjoy.
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html