首页 > 代码库 > 二进制流图片上传类-分享
二进制流图片上传类-分享
就不github了 ………
<?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 $moudle = ‘auto‘;
protected $rootfile = ‘static/uploads‘;
/**
* CONSTRUCT
* ImageBInaryStream constructor.
* @param array $config //CONFIG
* @param string $moudle //FILE NAME
*/
public function __construct($config=[],$moudle="")
{
$path = dirname(realpath($_SERVER[‘SCRIPT_FILENAME‘]));//PATH
$this->config = array_merge($this->config,$config);
if(!empty($moudle)){
$this->moudle = $moudle;
}
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];
$new_file = $this->path.DIRECTORY_SEPARATOR.$this->rootfile.DIRECTORY_SEPARATOR.$this->moudle.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->moudle.‘/‘.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;
}
}
二进制流图片上传类-分享
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。