Image upload method used in the project, Base64 save Local

Source: Internet
Author: User

Generate Health Reports
Public Function Uploadjkbg (Request $r) {
$data = $r->all ();
$JKBG [' jkbg_ctime '] = time ();
$JKBG [' jkbg_aid '] = \auth::user ()->id;
$JKBG [' jkbg_id '] = str_random (6). Mt_rand (100000, 999999);
Demo data
$data [' jkbg_pic '] = Array (0=> $base 64);
Upload image to server return path
if (!empty ($data [' jkbg_pic '])) {
$pic _arr = $this->uploadbaseima ($data [' jkbg_pic ']); This step puts the picture into the parameter is the picture Base64 array format
}else{
$pic _arr = Array ();
}
$JKBG [' jkbg_pic '] = Json_encode ($pic _arr);
Add a report
\db::begintransaction ();
$result = \db::table (' DXJK_JKBG ')->insert ($JKBG);
if (! $result) {
\db::rollback ();
\db::commit ();
return [' Code ' = 1001, ' data ' = [' message ' + ' upload report failed '];
}
Modify Upload Status
$ress = \db::table (' dxjk_admin ')->where (' id ', $JKBG [' Jkbg_aid '])->update ([' admin_status ' = 3]);
\db::commit ();
return [' Code ' = ' + ', ' data ' = [' message ' + ' upload report successful '];

}
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) {
$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;
}
}
}

Image upload method used in the project, Base64 save Local

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.