Mobile End HTML5 Image Upload Method "better compatible with Android iOS and micro-letter" _android

Source: Internet
Author: User
Tags base64

The method of uploading before the mobile end, some friends Test said that the micro-letter support is not very good, and some Andro also do not support, in fact, I have another method, but the example has not been sorted out, and contact me a lot of friends need, so the first hair out, and make a simple explanation, I will not do a demo.

<!doctype html>  

This is also the picture into a base64 to send, the individual does not recommend to use JS to change the size of the picture, it is recommended to cut the zoom or PHP to do it.

This.maxwidth = Options.maxwidth | | ; 
This.maxheight = Options.maxheight | | ; 
This.maxsize = Options.maxsize | | 3 * 1024 * 1024; 
This.input = Options.input; 
This.mime = {' png ': ' image/png ', ' jpg ': ' image/jpeg ', ' jpeg ': ' image/jpeg ', ' bmp ': ' Image/bmp '};

This part is to upload the image configuration, should be able to understand, you can change

$.ajax { 
URL: ' {: U (' upload ')} ', 
data:{str:base64,type:this.filetype}, 
type: ' Post ', 
dataType: ' JSON ', 
success:function (i) { 
alert (i.info); 
}

This part is uploaded after the AJAX send Base64 code to PHP,
Base64 code with a picture of the description string, so you have to use the regular remove, and then Base64 decoding saved to the picture file. and return to the address can be

The content of upload.php

$STR = $_post[' str '];
    $type = $_post[' type '];
 
    Switch ($type) {case
      ' image/png ':
        $ext = '. png ';
        break;
      Case ' image/jpeg ';
        $ext = '. jpeg ';
        break;
      Case ' image/jpeg ':
        $ext = '. jpg ';
        break;
      Case ' image/bmp ':
        $ext = '. bmp ';
        break;
      Default:
        $ext = '. jpg ';
    }
    $file _path= './uploads/'. Date (' Ymd '). /'. Time (). $ext;
    if (!file_exists (dirname ($file _path))) {
      mkdir (dirname ($file _path), 0777,true);
    $img _content = str_replace (' data: '. $type. ') Base64, ', ', $str);
    $img _content = Base64_decode ($img _content);
    $result =file_put_contents ($file _path, $img _content);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.