简体   繁体   中英

Image to base64 then upload

I am working on a Xamarin.Forms PCL app where users can select a photo and upload it. I would like to send the selected photo to the web service so it can be checked for correct format and file size and then uploaded to google photos.

To transfer it to my web service it needs to be a string. I tried using

MediaFile file;

var stream = file.GetStream();
var bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string content = System.Convert.ToBase64String(bytes);

My first problem is I don't know what to initialize file to, to have it correctly converted. Once the user selects an image it is stored in the ram with ImageSource image_source;

Once it uploads it suppose to reach my website which is in PHP and I get the image with $image = $_POST['image_string'];

My second question is how do I convert it back to image to check the file type and the image size?

It is sent to the website using

var values = new Dictionary<string, string>
        {
            {"session", UserData.session },
            {"image", ImageAsBase64().Result.Length.ToString() }
        };

        var content = new FormUrlEncodedContent(values);
        var response = await App.client.PostAsync(WebUtils.URL, content);
        var responseString = await response.Content.ReadAsStringAsync();
        string page_result = responseString;

You can use PHP's GD image library to basically do just about anything you want with an image.

To get an image from a string use this:

 $sourceImg = imagecreatefromstring(file_get_contents($file));

Then to save the image:

 imagejpeg($sourceImg, $targetPath, 60);  //60 is a compression

Here are some links:

imagecreatefromstring():

http://php.net/manual/en/function.imagecreatefromstring.php

GD Library: http://php.net/manual/en/book.image.php

It doesn't make sense to upload an image to the server, than check it's size. Unless if you are going to compress it and reduce the size on the server.

Here is how to check the size of the image:

var ImageStream = file.GetStream();
var bytes = new byte[ImageStream.Length];
//check if image is too big
double ImageSizeInMB = ((double)(bytes.Length) / (1024 * 1024));
//check if image size is bigger than 5 MB
if(ImageSizeInMB > 5)
{
    await DisplayAlert("Error", "Image is too big,reduce resolution and try again", "OK");
    return;
}

After that, you need to ask the user to retake the picture or you can start compressing it and resizing it. I was not able to compress the image after it was taken but I am sure it is possible since you can do it before taking the image using the media plugin options.

Now to convert the image to base64:

await ImageStream.ReadAsync(bytes, 0, (int)ImageStream.Length);
string base64 = System.Convert.ToBase64String(bytes);

That's it, you now have an image taken by your camera and converted to Base64 string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM