-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBase64.php
42 lines (31 loc) · 970 Bytes
/
Base64.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php namespace Octobro\API\Classes;
use System\Models\File;
class Base64 extends File {
public static function base64ToFile($string)
{
$mimeType = substr(array_get(explode(';', $string), 0), 5);
switch($mimeType) {
case 'image/jpeg':
$fileExt = '.jpg';
break;
case 'image/png':
$fileExt = '.png';
break;
case 'image/gif':
$fileExt = '.gif';
break;
case 'application/xlsx':
$fileExt = '.xlsx';
break;
default:
$fileExt = '';
}
$data = base64_decode(array_last(explode(',', $string)));
$filePath = temp_path(time() . rand() . $fileExt);
file_put_contents($filePath, $data);
$file = new self;
$file = $file->fromFile($filePath);
unlink($filePath);
return $file;
}
}