Base64 image Upload Processing method

Source: Internet
Author: User

Base64 format of foreground image, background processing mode
Process picture information return the corresponding path
Public Function Uploadbaseima ($IMGARR) {
$result = Array ();
Set the path directly under uploads
if ($IMGARR) {
foreach ($imgArr as $k = = $v) {
if ($v) {
Determine if the upload is the original address
$a = Preg_match ('/.* (\.png|\.jpg|\.jpeg|\.gif) $/', $v);
if ($a) {
is stored unchanged, the file path is stored
$b = substr ($v, 36);
$result [$k] = $b;
}else{
No, just upload the picture.
$address = $this->basechangeimg ($v);
$result [$k] = $address;
}
}else {
return Array ();
}
}
return $result;
}
}
Convert Base64 to local image
Public Function basechangeimg ($base)
{
Match the format of the picture
if (Preg_match ('/^ (data:\s*image\/(\w+); base64,)/', $base, $result)) {
$type = $result [2];
$path = '/base '. Date (' Ymd ', Time ()). "/";
$new _file = Public_path (' uploads '). $path;
if (!file_exists ($new _file)) {
Check if there is a folder, if not created, and give the highest permissions
mkdir ($new _file, 0700);
}
$name = Uniqid (). ". {$type} ";
$new _file = $new _file. $name;
if (file_put_contents ($new _file, Base64_decode (Str_replace ($result [1], ', $base)))) {
Return $path. $name;
} else {
return false;
}
}
}

Base64 image Upload Processing method

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.