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

Php 尝试在上载的文件保存到服务器时调整其大小,php,laravel,laravel-5,image-uploading,Php,Laravel,Laravel 5,Image Uploading,我正在使用Glide从我的一个站点交付图像内容。这是工作得很好,我现在已经建立了一个文件上传,以便管理员可以上传图像到该网站进行后续下载 管理员将上载的一些图像将比我需要的大得多(或者需要在服务器上存储的开销),因此我想缩小它们的大小,最好是在上载例程期间,或者在将它们保存到新位置(存储/应用/图像)之后,如果没有这样做,就缩小它们的大小 因此,我一直在进行干预,例如,由于我对getClientOriginalName/Extension中可用的文件名和路径理解不足,所以没有取得多大成功 谁能给

我正在使用Glide从我的一个站点交付图像内容。这是工作得很好,我现在已经建立了一个文件上传,以便管理员可以上传图像到该网站进行后续下载

管理员将上载的一些图像将比我需要的大得多(或者需要在服务器上存储的开销),因此我想缩小它们的大小,最好是在上载例程期间,或者在将它们保存到新位置(存储/应用/图像)之后,如果没有这样做,就缩小它们的大小

因此,我一直在进行干预,例如,由于我对getClientOriginalName/Extension中可用的文件名和路径理解不足,所以没有取得多大成功

谁能给我看一个很好用的模式吗。理想情况下,我希望包括一些我在其他人的例子中看到的东西,比如

$img=Image::make('foo.jpg')->调整大小(300200)

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

  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: 这段更新的代码将文件输出到/new目录,看起来一切正常,但输出文件具有“零字节”。我错过了什么

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

  • 我不得不将我的应用程序从虚拟机移动到开发机器(iMac)上,以防止路径出现额外的混乱
  • 在执行->保存()之前,图像的路径必须存在
  • 必须在->保存()之前设置path变量
  • 我根本不需要存储::put,因此更大的文件永远不会出现在服务器上。
然后,这最后的代码开始工作

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

非常感谢你们大家。你让我的拉威尔学习曲线不那么可怕

图像上载和调整工作流程如下所示:

  • 将图像从
    tmp
    上传到您的目录
  • 通过设置
    高度、宽度、质量
    来复制该图像,并将其保存在相同或其他目录中
  • 删除原始图像
因此,根据您的代码流:

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

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

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

您可以使用或仅使用命令来调整大小或转换

请注意以下评论:

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>');
  }
公共函数saveUploadPic(请求$Request)
{
$pic=$request->file(“”);
#检查上传是否正确
如果(!$pic->isValid())
{
抛出新异常(“图像未正确上传”);
}
#检查mime类型和扩展名
$ext=$pic->getClientOriginalExtension();
$mime=$pic->getMimeType();
如果(!in_数组($mime,$allowedMimeTypeArray)| |!in_数组($ext,$allowedExtArray))
{
抛出新异常(“此图像不支持”);
}
#检查尺寸
$size=$pic->getClientSize()/1024/1024;
如果($size>$allowedSize)
{
抛出新异常(“图像大小超过支持大小”);
}
########################你有两个选择###################
#1-如果您需要原始图像进行其他处理,请将图像保存在临时位置,并使用随机哈希作为名称
#下面的代码将图像保存在/storage/app/tmp/pics中/
$hash=md5(日期(“YmdHis”).rand(110000));
$pic->storeAs('tmp/pics',$hash'..$ext);
#然后调整或转换它
$img=Image::make(存储路径('app/tmp/pics/'.$hash..$ext))->调整大小(300200);
#保存你想要的新图像
$img->save(“”);
#完成原始图像后,将其删除
存储::删除(存储路径('app/tmp/pics/'.$hash.'.$ext));
#2-或者只使用下面的方法调整大小并保存图像,无需保存在临时位置
$img=Image::make($pic->getRealPath())->调整大小(300200);
$img->save(“”);
}

如果您想使用convert,请参阅。

谢谢Nauman和@Mayank Pandeyz。我已经按照建议尝试了。解决方案让我非常接近。我当前的代码与上面的更新1一致。这给了我一个包含文件的/新文件夹,但是,文件有“零字节”。这就是我的困惑所在。我是使用$file还是file_get_内容($file)或其他什么?如果这有意义,我不知道哪个变量保存实际的文件…@Wittner,你能试试
$new\u image=image::make($file->getRealPath())->resize(300200);
我刚刚试过。文件仍然是零字节。回显$file->getRealPath()为列表中的每个文件提供“/tmp/phpBxgB8a”和类似内容。我还尝试了file\u get\u contents($file),但没有成功:-(@Wittner,检查编辑。要保存该文件,必须对图像对象使用
save
方法。标记为正确,因为尽管其他人有正确的贡献,但我觉得@Nauman Zafar有确切的答案,我遇到的许多其他问题更多地与我对Laravel其他部分的经验不足有关。深深感谢大家不过-我很感激。我刚刚用yor code@Mr.Sun进行了试验,我可能更接近了。我遇到了一些奇怪的错误,但最后一个错误是“无法将图像数据写入路径(/home/vagrant/sites/ckgimages/storage/app/smallpics/)'让我现在认为,我的问题源于这样一个事实:我的本地应用程序运行在一个流浪的盒子上,而Storage::put()将图像放在我的实际Mac驱动器上,$file->storAs()将图像放在Vagrant box上。我必须将应用程序从Vagrant box移动到主驱动器以进行进一步实验…您好,很抱歉延迟。我没有使用MAC man的经验,但在我的linux中运行良好。我很高兴看到您的结果。您的代码让我更仔细地研究了GD以及如何处理我的错误(我是一个老学究,最近把PHP改为OOP),抛出异常是我将更仔细研究的问题。这个应用程序的上传部分不会公开,因为它在管理服务器上的登录后面,但产品本身是公开的
$new_image = Image::make($file)->resize(300,200)->save('/path/to/save');
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>');
  }