2015-11-11 41 views
2

我将图像保存到s3和我的数据库的s3路径。当我需要显示图像时,我正在调用路径。所以现在我在将图像保存到s3之前无法重新调整图像大小。我得到这个错误信息:如何在存储到s3之前使用干预来调整图像大小?

Command (getRealPath) is not available for driver (Gd). 

这是我的控制器是什么样子

public function up(Request $request) { 

     $user = $request->user(); 
     $image= $request->file('images'); 

      if(!empty(($image))){ 
      $files = Input::file('images'); 
      foreach($files as $file) { 
      if(!empty($file)){ 



      $ext = $file->getClientOriginalExtension(); 

      $media = ($user->media->where('category','profile')->first()); 
      if($media == null){ 
       $media = new Media(); 
       $media->category='profile'; 
      }  
       $this->saveMedia($media, $user,$file); 
      } 
     } 
      return Redirect::back()->with('message','Your profile has been updated'); 
     } 
    } 
    private function saveMedia($media, $user, $file){ 

     $ext = $file->getClientOriginalExtension(); 
     $key = strtotime('now') . '.' . $ext;   
     $id = $user->id; 
     $url = 'https://s3-us-west-2.amazonaws.com/makersbrand/' . $id . '/' . $media->category . '/'; 
     $media->user_id = $user->id; 
     $media->path = $url . $key; 

     $this->fillMedia($media,$user,$file, $key); 
     $media->save(); 

    } 
    private function fillMedia($media, $user, $file, $key) 
    { 
     $file = Image::make($file)->resize(200,200); 
     $s3 = AWS::createClient('s3'); 
     $result = $s3->putObject(array(
      'Bucket' => self::$_BUCKET_NAME, 
      'Key' => $user->id . '/'. $media->category .'/'. $key, 
      'SourceFile' => $file->getRealPath(), 
      'Metadata' => array(
      'Owner' => $user->first_name .' ' . $user->last_name 
      ) 
      )); 
    } 

更新 我不认为它击中getClientOriginalExtension错误之前我的图像,即使得到适当的调整做。当我在调整大小后执行var_dump时,我得到以下文本:

object(Intervention\Image\Image)#238 (9) { ["driver":protected]=> 
object(Intervention\Image\Gd\Driver)#237 (2) { ["decoder"]=> 
object(Intervention\Image\Gd\Decoder)#241 (1) { 
["data":"Intervention\Image\AbstractDecoder":private]=> NULL } 
["encoder"]=> object(Intervention\Image\Gd\Encoder)#242 (4) { ["result"]=> 
NULL ["image"]=> NULL ["format"]=> NULL ["quality"]=> NULL } } 
["core":protected]=> resource(280) of type (gd) ["backups":protected]=> 
array(0) { } ["encoded"]=> string(0) "" ["mime"]=> string(10) "image/jpeg" 
["dirname"]=> string(26) "/Applications/MAMP/tmp/php" ["basename"]=> 
string(9) "phpVGzVk0" ["extension"]=> NULL ["filename"]=> string(9) 
"phpVGzVk0" } 

结果为空。格式为空。图像是空的。我在这里做错了什么?

更新 我将Image :: make移动到了我的saveMedia函数中。现在,我得到

Command (getRealPath) is not available for driver (Gd). 

回答

1

好像你正在使用(GD)不支持特定方法(getClientOriginalExtension)在你的情况下,唯一的解决办法是使用一个PHP函数来获取从文件名的扩展名的驱动程序:

$ ext = pathinfo($ filePath,PATHINFO_EXTENSION);

+0

我只需将$ :: ext行下面的Image :: make行移动即可。但是现在我得到了相同的错误,除了它的getRealPath。我用这个将图像存储到亚马逊S3。但是,我甚至没有在调整它的大小后在我的$文件中看到一个路径对象。 – joejoeso

2

为了得到这个工作,我在我的Laravel 5代码库中使用了下面的代码;

$imageFile = \Image::make($uploadedFile)->resize(600, 600)->stream(); 
$imageFile = $imageFile->__toString(); 

$filename = 'aUniqueFilename.png'; 

$s3 = \Storage::disk('s3'); 
$s3->put('/'.$filename, $imageFile, 'public'); 
+0

一些解释将不胜感激。你用'$ imageFile - > __ toString()'做什么? –

0

主要错误在这里 - 你传递的文件,而不是一个错误的对象(Intervention\Image\Image)。

在你的情况应该工作是这样的:

private function fillMedia($media, $user, $file, $key) 
{ 
    $image = Image::make($file)->resize(200,200); 
    $s3 = AWS::createClient('s3'); 
    $result = $s3->putObject(array(
     'Bucket' => self::$_BUCKET_NAME, 
     'Key' => $user->id . '/'. $media->category .'/'. $key, 

     // use 'Body' option to put resized image content instead of 'SourceFile' 
     // fyi, your resized image wasn't saved to the original file 
     'Body' => $image->__toString(), 
     'Metadata' => array(
      'Owner' => $user->first_name .' ' . $user->last_name 
     ) 
    )); 
} 
3

按类\干预\图片\图片此错误的原因不支持方法getRealPath()

解决方案:创建一个包装类实施getRealPath()

use \Intervention\Image\Image as InterventionImage; 

class ImageFile 
{ 
    /** 
    * Intervention image instance. 
    * 
    * @var \Intervention\Image\Image 
    */ 
    private $image; 

    function __construct(InterventionImage $image) 
    { 
     $this->image = $image; 
    } 

    function getRealPath() 
    { 
     return $this->image->basePath(); 
    } 

} 

用法:

$image = new ImageFile(InterventionImage::make($file->path())->fit(300, 200)->save()); 

$s3Key = Storage::disk('s3')->putFileAs('my_s3_image_folder', $image, 'my_image_file_name.jpg', 'public'); 

// Save your s3 Key to your database or whatever... 
相关问题