[英]Laravel unisharp/filemanager and unisharp/ckeditor file/image browser/upload shows 404 not found
[英]Handling file upload and resizing image with Unisharp Laravel
我正在为我的 laravel 项目使用 Unisharp 文件上传。 包工作正常。 现在我想要的是,我想要文件类型数组,例如:
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
Unisharp 文件管理器的图像 URL。 假设如果我从 Unisharp 文件管理器中选择http://example.com/storage/files/42/lace-up.png
我想获得像上面那样的文件数组。
我想要这个是因为我想相应地调整图像大小并将它们存储到不同的文件夹中。
我创建了以下函数来上传和调整图像大小:
function uploadImage($file, $dir, $thumb_dimension=null){
$path = public_path().'/uploads/'.$dir;
if(!File::exists($path)){
File::makeDirectory($path, 0777, true, true);
}
$file_name = ucfirst($dir).'-'.date('Ymdhis').rand(0,999).".".$file->getClientOriginalExtension();
$success = $file->move($path, $file_name);
if($success){
$file_path = $path.'/'.$file_name;
if($thumb_dimension){
list($width,$height) = explode('x',$thumb_dimension);
Image::make($file_path)->resize($width,$height, function($const){
$const->aspectRatio();
})->save($path.'/Thumb-'.$file_name);
}
return $file_name;
} else {
return null;
}
}
是否可以?
编辑
获取图像的详细信息后,我想使用以下功能。
function uploadImage($file, $dir, $thumb_dimension=null){
$path = public_path().'/uploads/'.$dir;
if(!File::exists($path)){
File::makeDirectory($path, 0777, true, true);
}
$file_name = ucfirst($dir).'-'.date('Ymdhis').rand(0,999).".".$file->getClientOriginalExtension();
$success = $file->move($path, $file_name);
if($success){
$file_path = $path.'/'.$file_name;
if($thumb_dimension){
list($width,$height) = explode('x',$thumb_dimension);
Image::make($file_path)->resize($width,$height, function($const){
$const->aspectRatio();
})->save($path.'/Thumb-'.$file_name);
}
return $file_name;
} else {
return null;
}
}
如果你只想从链接中提取图像,你只需要创建一个小函数来处理它。
public function getImageViaLink($link){
try {
$info = pathinfo($link);
$image = file_get_contents($link);
$arr['basename'] = $info['basename'];
$file_info = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer($image);
$arr['size'] = strlen($image);
$arr['mime_type'] = $mime_type;
$path = public_path() .'/'. $arr['basename'];
// You can save contents of link in your file directly or store it in tmp
file_put_contents($path, $image);
$arr['path'] = $path;
return $arr;
}
catch (Exception $e) {
echo $e->getMessage();
}
}
至于你的数组error
的情况,你基本上想要 文件上传错误,但它可以很容易地被异常处理。
作为旁注,如果您在使用 unisharp 存储图像时有请求变量,则可以在 $request 中访问所有这些详细信息。
// dd() of a request containing image file.
array:2 [▼
"_token" => "e7v7ZxaKoIFGIYOscCAFwsoB8olw8lrNjwx8Azyi"
"attachment_1_0" => UploadedFile {#229 ▼
-test: false
-originalName: "db.png"
-mimeType: "image/png"
-size: 86110
-error: 0
path: "C:\xampp\tmp"
filename: "phpF4F4.tmp"
basename: "phpF4F4.tmp"
pathname: "C:\xampp\tmp\phpF4F4.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\phpF4F4.tmp"
... and many more
您可以创建一个侦听器,无论何时存储图像,您都可以创建另一个包含所有详细信息的副本以将其保存到另一个位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.