//获取文件上传信息
$file_upload = $request->file('image');
if ($file_upload) {
if(is_array($file_upload)){
foreach ($file_upload as $value){
if (!$value->isValid()) {
return ['上传图片出错,请重试!'];
}
$img_res[] = img_uplode($value, 'questionPic');
}
foreach ($img_res as $value) {
$img_path[] = $value['path'];
$img_thumb_path[] = $value['thumb_path'];
}
$path = implode('|', $img_path);
$thumb_path = implode('|', $img_thumb_path);
} else {
$img_res = img_uplode($file_upload, 'questionPic');
if (!empty($img_res)) {
$path = $img_res['path'];
$thumb_path = $img_res['thumb_path'];
}
}
}
if (!function_exists('img_uplode')) {
function img_uplode($img, $dir)
{
//获取文件上传的扩展名
$extension = $img->extension();
// echo $extension;exit;
//图片前缀地址
$prefix = date('YmdHis').rand(1000,9999);
//重新拼接缩略图片名称
$pic_thumb_name = $prefix.'_thumb.'.$extension;
//重新拼接原图片名称
$pic_name = $prefix.'.'.$extension;
$img->storeAs('public/'.$dir, $pic_name);
//原图片保存路劲
$save_path = storage_path('app/public/'.$dir.'/'.$pic_name);
//缩略图保存路径
$thumb_save_path = storage_path('app/public/'.$dir.'/'.$pic_thumb_name);
//判断原图片是否存在
if(file_exists($save_path)){
//获取此张上传的图片信息
$img_arr = getimagesize($save_path);
if($img_arr[0]>$img_arr[1]){
//调用缩略图函数、、这里可以设置缩略的宽和高//宽为300,高自适应
mkThumbnail( $save_path, 300, null, $thumb_save_path);
}else{
//调用缩略图函数、、这里可以设置缩略的宽和高//宽为200,高自适应
mkThumbnail( $save_path, null, 300, $thumb_save_path);
}
}
$data = array(
'path' => $pic_name,
'thumb_path' => $pic_thumb_name
);
return $data;
}
}