ThinkPhp图片编码
//————
/**装base64
* @param $image_file
* @return string
*/
function base64EncodeImage($image_file) {
$base64_image = '';
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return $base64_image;
}
/**
* @param $file
* @return string
*/
public function base64Img($file){
$img_dir = $_SERVER['DOCUMENT_ROOT'] . $file;
$path = $this->base64EncodeImage($img_dir);
unlink($img_dir);//删除路径图片
return $path;
}
/**
* @param $arr
* @return string
*/
public function upload_bas64($arr){
$imgarr = [];
$arr = array_filter(explode(",",$arr));
foreach($arr as $k=>$v){
array_push($imgarr,$this->base64Img($v));
}
return implode("--tyb--",$imgarr);
}
用法:
public function txt(){
$this->upload_bas64("/Uploads/44.jpg,/Uploads/7.jpg");
}