-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImage.model.php
More file actions
51 lines (42 loc) · 1.46 KB
/
Image.model.php
File metadata and controls
51 lines (42 loc) · 1.46 KB
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
43
44
45
46
47
48
49
50
51
<?php
/* Generated by neoan3-cli */
namespace Neoan3\Model;
use Neoan3\Apps\Db;
use Neoan3\Apps\DbException;
use Neoan3\Apps\Ops;
class ImageModel extends IndexModel {
static function byId($id){
return IndexModel::first(Db::easy('image.*',['id'=>'$'.$id]));
}
static function undeletedById($id){
$img = IndexModel::first(Db::easy('image.*',['id'=>'$'.$id,'^delete_date']));
return $img;
}
static function saveFromBase64($b64String,$userId){
try{
preg_match('/data:([a-z\/]+);base64,/',$b64String,$matches);
$newImageRaw = substr($b64String,strlen($matches[0]));
if(!is_dir(path.'/asset/upload/')){
mkdir(path.'/asset/upload/',0755);
}
$directory = path.'/asset/upload/'.$userId;
$fileType = explode('/',$matches[1]);
if(!is_dir($directory)){
mkdir($directory,0755);
}
$path = '/'.Ops::randomString(22).'.'.end($fileType);
file_put_contents($directory.$path,base64_decode($newImageRaw));
$newId = Db::uuid()->uuid;
$newImage = [
'id'=>'$'.$newId,
'format'=>$matches[1],
'path'=>'/asset/upload/'.$userId.$path,
'inserter_user_id'=>'$'.$userId
];
Db::image($newImage);
} catch(DbException $e){
return false;
}
return $newId;
}
}