public function Upload(){
$file = request()->file('file');
$filepath=$this->publicimgurl."paint";
$info = $file->move($filepath);
if($info){
$filename = '/www/wwwroot/image/paint/' . $info->getSaveName();
$type = mb_substr($filename,strripos($filename,'.')); //后面的类型
//设置文件路径
$ml = date('Ymd', time());//目录名称
$url = '/www/wwwroot/image/paint/'.$ml.'/thumb/';//存储的路径
//判断目录是否存在
if(!file_exists($url)){
mkdir($url,0777,true);//创建文件夹
}
$imgname ='thumb_'.time().mt_rand(1111,9999);//创建图片名称
$needCreatImaName = $url.$imgname.$type;
$oldPath = 'paint/'.$info->getSaveName();;
$newPath = 'paint/'.$ml.'/thumb/'.$imgname.$type;
$this->creatImageThumb($filename,$needCreatImaName);
$data['code'] = 200;
$data['msg'] = $oldPath;
$data['thumb'] = $newPath;
}else{
$data['code'] = 0;
$data['msg'] = $file->getError();
}
return json($data);
}
/*
* $filename:原文件名
*
* */
private function creatImageThumb($filename,$needCreatImaName){
$info = getimagesize($filename);
$urls = $needCreatImaName;
//获取图片的后缀
$type = image_type_to_extension($info[2], false);
//拼接图片资源句柄函数
$func = 'imagecreatefrom'.$type;
//创建图片资源句柄
$image = $func($filename);
$imageSize = $this->getImageSize($info);
$image_thumb = imagecreatetruecolor($imageSize['width'], $imageSize['height']);
imagecopyresampled($image_thumb, $image, 0, 0,0,0,
$imageSize['imageSize'], $imageSize['imageSize'],$imageSize['imageSize1'],$imageSize['imageSize1']);
//销毁缓存的图片,释放内存
imagedestroy($image);
//图片输出函数拼接
$outFunc = 'image'.$type;
//生成图片
$outFunc($image_thumb,$urls);
}
//单文件上传
private function getImageSize($info){
//自定义需求
//918x1018
if($info[0]>$info[1]){
//横图片
if($info[0]>918){
$data['height'] = intval($info[1]/($info[0]/918));
$data['width'] = 918;
}else{
if($info[1]>1018){
$data['width'] = intval($info[0]/($info[1]/1018));
$data['height'] = 1018;
}else{
$data['height'] = $info[1];
$data['width'] = $info[0];
}
}
$data['imageSize'] = $data['width'];
$data['imageSize1'] = $info[0];
}else{
//竖图片
if($info[1]>1018){
$data['width'] = intval($info[0]/($info[1]/1018));
$data['height'] = 1018;
}else{
if($info[0]>918){
$data['height'] = intval($info[1]/($info[0]/918));
$data['width'] = 918;
}else{
$data['height'] = $info[1];
$data['width'] = $info[0];
}
}
$data['imageSize'] = $data['height'];
$data['imageSize1'] = $info[1];
}
return $data;
}
02-26
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交