【问题标题】:Handling file upload and resizing image with Unisharp Laravel使用 Unisharp Laravel 处理文件上传和调整图像大小
【发布时间】:2020-12-18 01:09:40
【问题描述】:

我正在为我的 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;
  }
}

【问题讨论】:

  • 所以基本上你想提取该链接中附加的文件的详细信息
  • @bhucho 是的,因为我想将图像存储到另一个文件夹。

标签: php laravel file-upload unisharp-file-manager


【解决方案1】:

如果你只想从链接中提取图片,你只需要创建一个小函数来处理它。

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的情况,你基本上想要file upload errors,但它可以很容易地被Exception处理。

附带说明,如果您在使用 unisharp 存储图像时有 request 变量,则可以在 $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

您可以创建一个侦听器,每当存储图像时,您都可以创建另一个包含所有详细信息的副本,以将其保存到另一个位置。

【讨论】:

  • 谢谢,布科。我不是从 Unisharp 上传的。我正在尝试访问已从 Unisharp 上传的图像。
  • 这样您就可以使用 getImageViaLink 访问存储在链接中的图像详细信息,这是什么问题
猜你喜欢
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
相关资源
最近更新 更多