Reputation: 514
I am using Unisharp file upload for my laravel project. The package working properly. Now what I want is, I want to have array of file type like:
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
of the image URL of Unisharp filemanager. Suppose if I select http://example.com/storage/files/42/lace-up.png
from Unisharp filemanager I want to get file array like above.
I want to have this because I want to resize the images accordingly and store them to different folders.
I have created following function to upload and resize images:
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;
}
}
Is it possible?
Edit
I want to use the following function after getting details of image.
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;
}
}
Upvotes: 0
Views: 1080
Reputation: 3420
If you want to just extract image from the link, you just need to create a small function to handle it.
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();
}
}
As for the case of error
in your array, you basically want file upload errors but it can easily be handled by Exception.
As a sidenote, if you have the request variable at the time of storing image using unisharp, you can access all these details in $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
You can create a listener that whenever an image is stored you can create another copy with all the details to save it to another location.
Upvotes: 0