尝试在上传的文件保存到服务器时调整其大小

时间:2017-06-12 09:44:12

标签: php laravel laravel-5 image-uploading

我正在使用Glide从我的某个网站提供图片内容。这个工作正常,我现在已经构建了一个文件上传,以便管理员可以将图像上传到网站以供后续下载。

管理员上传的一些图片将比我需要的大得多(或者想要在服务器上存储的开销),所以我想缩小它们,最好是在上传例程中或者在它们失败之后,已保存到新位置(存储/应用/图像)

所以,由于我对getClientOriginalName / Extension等提供的文件名和路径的理解不足,我一直在干预干预,但没有太大的成功。

有没有人能告诉我一个适合这种情况的模式。理想情况下,我喜欢把我在其他人的例子中看到过的东西包括在内......

$img = Image::make('foo.jpg')->resize(300, 200);

...在我的代码中的正确位置

  foreach($files as $file) {
    $fileExtension = $file->getClientOriginalExtension();
    $fileMimeType = $file->getMimeType();
    if(in_array($fileExtension, $allowableExtensions)) {
      if(in_array($fileMimeType, $allowableMimes)) {
        array_push($dbFileList, $file->getClientOriginalName());
        $newImage = '/images/' . $propertyCode . '/' . $file->getClientOriginalName();
        Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
      }else{
        $errorMessage = 'At least one file was not an image, check your results...';
      }
    }else{
      $errorMessage = 'At least one file was not an image, check your results...';
    }
  }

更新1:

Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
$img = Image::make($file);
Storage::put('/images/new/' . $file->getClientOriginalName(), $img);

这个更新的代码将文件输出到/ new目录,所有看起来都很好,但输出文件有'零字节'。我错过了什么?

更新2:最终代码

最终答案(在使用贡献者提供的正确代码之后)是:

  • 我不得不将我的应用程序从虚拟框移动到开发机器(iMac),以防止与路径混淆
  • 在进行 - > save()之前,图像的路径必须存在
  • 路径变量必须在 - > save()之前设置
  • 我根本不需要Storage :: put,因此较大的文件永远不会在服务器上结束。

然后这个最终的代码开始起作用了。

$path = storage_path('app/smallpics/')."/".$file->getClientOriginalName();
$img = Image::make($file)->resize(300,200)->save($path);

非常感谢你们所有人。你让我的Laravel学习曲线不那么糟糕!!

3 个答案:

答案 0 :(得分:2)

您可以使用Intervention来操作图像(调整大小等)

$new_image = Image::make($file)->resize(300,200)->save('/path/to/save');

答案 1 :(得分:0)

图片上传和调整工作流程如下:

  • 将图片从tmp上传到您的目录。
  • 通过设置height, width, quality制作该图像的副本,并将其保存在相同或其他目录中。
  • 删除原始图片。

根据您的代码流程:

Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));

在此代码之后,放置图像压缩代码,然后删除原始图像。

答案 2 :(得分:0)

您可以使用Intervention或仅使用imagemagick convert command line命令进行调整大小或转换。

注意评论:

public function saveUploadPic(Request $request)
  {
    $pic = $request->file('<NAME_OF_FILE_INPUT_IN_HTML_FORM>');

    #check for upload correctly
    if(!$pic->isValid())
    {
      throw new Exception("IMAGE NOT UPLOADED CORRECTLY");
    }

    #check for mime type and extention
    $ext = $pic->getClientOriginalExtension();
    $mime = $pic->getMimeType();
    if(!in_array($mime, $allowedMimeTypeArray) || !in_array($ext, $allowedExtArray))
    {
      throw new Exception("This Image Not Support");
    }
    #check for size
    $size = $pic->getClientSize() / 1024 / 1024;
    if($size > $allowedSize)
    {
      throw new Exception("Size Of Image Is More Than Support Size");
    }

    ########################YOU HAVE TWO OPTION HERE###################
    #1- save image in a temporary location with random hash for name if u need orginal image for other process
          #below code save image in <LARAVEL_APP_PATH>/storage/app/tmp/pics/
          $hash = md5(date("YmdHis").rand(1,10000));
          $pic->storeAs('tmp/pics', $hash.'.'.$ext);

          #Then resize or convert it
          $img = Image::make(storage_path('app/tmp/pics/'.$hash.'.'.$ext))->resize(300, 200);
          #save new image whatever u want 
          $img->save('<PATH_TO_SAVE_IMAGE>');
          #after u finish with orginal image delete it
          Storage::delete(storage_path('app/tmp/pics/'.$hash.'.'.$ext);

    #2- Or just use below for resize and save image witout need to save in temporary location
          $img = Image::make($pic->getRealPath())->resize(300,200);
          $img->save('<PATH_TO_SAVE_IMAGE>');
  }

如果您想使用转换,请参阅this link

相关问题