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

Posted 徐锅的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二进制流图片上传类-分享相关的知识,希望对你有一定的参考价值。

 
就不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
;
}

}

以上是关于二进制流图片上传类-分享的主要内容,如果未能解决你的问题,请参考以下文章

七牛上传图片和二进制流方法

JS 上传图片转换成二进制流base64

jsp将图片等文件上传到服务器根目录下,读取二进制流存入mysql?怎么样实现?

C#winform 中上传图片保存到数据库中?

c# 图片转二进制流

umeditor实现ctrl+v粘贴word图片并上传