二进制流图片上传类-分享

 
------------------类 Class ………
 
<?php

//================ ImageBinaryStream ==========================


/**
* Class ImageBInaryStream
*/
Class
ImageBinaryStream
{
/**
* @var
array
*/
private $config
= [
'path'=>''
,
'width'=>0
,
'height'=>0
,
'type'
=>[],
'size'=>1024*
5
];

/**
* @var
string
*/
private $errorsize= 'Img Size Wrongful'
;
/**
* @var
string
*/
private $errorType= 'Img Type Wrongful'
;
/**
* @var
string
*/
private $errorwidth= 'Img Width Wrongful'
;
/**
* @var
string
*/
private $errorheight = 'Img Height Wrongful'
;

protected $module = 'auto';

protected $rootfile = 'static/uploads';

/**
* CONSTRUCT
* ImageBInaryStream constructor.
* @param
array $config //CONFIG
* @param
string $module //FILE NAME
*/
public function __construct($config=[],$module=""
)
{
$path = dirname(realpath($_SERVER['SCRIPT_FILENAME']));
//PATH

$this->config = array_merge($this->config,$config);

if(!empty($module)){
$this->module = $module
;
}

if(empty($this->config['path'])){
$this->config['path'] = $path
;
}

}

/**
* @return
\think\response\Json
*/
public function
run()
{
header('Content-type:text/html;charset=utf-8'
);
$base64_image_content = $_POST['img'
];
$vali = $this->validate($base64_image_content
);
if(!$vali['code'
]){
return json($vali,400
);
}
$save =$this->save($base64_image_content,$vali['data'
]);
if(!$save['code'
]){
return json($save,400
);
}
return json($save,200
);
}

/**
* @param
$base64_image_content
* @param
$vali
* @return
array
*/
protected function save($base64_image_content,$vali
)
{
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result
)){
$type = $result[2] == 'jpeg'? 'jpg':$result[2
];
$new_file = $this->path.DIRECTORY_SEPARATOR.$this->rootfile.DIRECTORY_SEPARATOR.$this->module.DS.date('Ymd',time())."/"
;
if(!file_exists($new_file
))
{
//Check to see if there is a folder, if not created, and given the maximum permissions
mkdir
($new_file, 0700,true
);
}
$new_file = $this->rootfile.'/'.$this->module.'/'.date('Ymd',time()).'/'.time().".{$type}"
;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content
))))
{
$fileAttr = $vali
;
$filesize = $fileAttr['size'].'kb'
;
$data = ["url"=>$new_file,"width"=> $fileAttr['width'],"height"=> $fileAttr['height'],'size'=>$filesize
];
return $this->msg_array(1,'success',$data
);
}else
{
return $this->msg_array(0,$this->NotFound
,[]);
}
}
return $this->msg_array(0,'Base64Data Error'
,[]);
}

/**
* @return
array
*/
protected function validate($base64_image_content
)
{
if(!$base64_image_content
){
return $this->msg_array(0,"Not Image"
);
};
$fileAttr = getimagesize($base64_image_content);
//ATTR
preg_match
('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result);
//IMG BASE64
$filesize = round(strlen(base64_decode(str_replace($result[1], '', $base64_image_content)))/1024);
//KB
$data = ["width"=> $fileAttr[1],"height"=> $fileAttr[0],'size'=>$filesize];
// fileData
if($this->WH_validate([$this->width,$this->height
])){
if($fileAttr[0] != $this->height
){
return $this->msg_array(0,$this->errorheight
);
}
if($fileAttr[0] != $this->width
){
return $this->msg_array(0,$this->errorwidth
);
}
}

if($filesize > $this->size){
return $this->msg_array(0,$this->errorsize
);
}

preg_match('/^[a-z]+[\/]([a-z]+)/','image/jpeg',$result);
if(!in_array($result[1],$this->type
)){
return $this->msg_array(0,$this->errorType
);
}

return $this->msg_array(1,"success",$data);
}

/**
* @param
array $arr
* @return
bool
*/
private function WH_validate($arr
=[])
{
foreach ($arr as $key
){
if(empty($key
)){
return false
;
}
}
return true
;
}

/**
* @param
$name
* @return
mixed
*/
public function __get($name
)
{
//
TODO: Implement __get() method.
return
$this->config[$name
];
}

/**
* @param
$name
* @param
$value
* @return
mixed
*/
public function __set($name, $value
)
{
//
TODO: Implement __set() method.
return
$this->config[$name]=$value
;
}

/**
* @param
$name
* @return
bool
*/
public function __isset($name
)
{
//
TODO: Implement __isset() method.
return isset
($this->config[$name
]);
}

/**
* @param
int $code
* @param
string $msg
* @param
array $data
* @param
string $url
* @param
int $wait
* @return
array
*/
private function msg_array($code = 1,$msg="",$data=[],$url="",$wait= 3
)
{
$result
= [
'code' => $code
,
'msg' => $msg
,
'data' => $data
,
'url' => $url
,
'wait' => $wait
,
];
return $result
;
}

}
 
-------------------------------appliy:
 
$module = 'brand';
import('ImageBinaryStream',EXTEND_PATH,'PHP'
);
$image = new \ImageBinaryStream(config('base64imageupload'),$module
);
return $image
->run();
posted @ 2017-06-24 12:11  徐锅  阅读(176)  评论(0编辑  收藏  举报