laravel 在存储到 s3 之前如何使用干预调整图像大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33660956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:40:36  来源:igfitidea点击:

How do I resize image using intervention before storing to s3?

phplaravelamazon-s3laravel-5intervention

提问by Hymanjoesmith

I'm saving the image to s3 and the s3 path to my database. I'm calling the path when I need to show the image. So right now I'm having trouble resizing that image before saving it to s3. I get this error message:

我将图像保存到 s3 并将 s3 路径保存到我的数据库。当我需要显示图像时,我正在调用路径。所以现在我在将其保存到 s3 之前无法调整该图像的大小。我收到此错误消息:

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

this is what my controller looks like

这就是我的控制器的样子

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
            )
            ));
    }

UpdateI don't think my images even get resized properly before it hits the getClientOriginalExtension error. When I do var_dump after resizing, I get this text:

更新我认为我的图像在遇到 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" }

Result is null. Format is null. image is Null. What am I doing wrong here?

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

updateI moved Image::make to my saveMedia function. Now I get

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

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

回答by ajtrichards

To get this working I used the following code within my Laravel 5 codebase;

为了让它工作,我在我的 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');

回答by roliroli

This error cause by class \Intervention\Image\Image doesn't support method getRealPath()

由类 \Intervention\Image\Image 引起的此错误不支持方法 getRealPath()

Solution: create a wrapper class to implement 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();
    }

}

Usage:

用法:

$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...

回答by Dracony

It seems like the driver you are using (gd) does not support a particular method (getClientOriginalExtension) the only solution in your case is to use a php function to get the extension from file name:

您使用的驱动程序 (gd) 似乎不支持特定方法 (getClientOriginalExtension),您的情况的唯一解决方案是使用 php 函数从文件名中获取扩展名:

$ext = pathinfo($filePath, PATHINFO_EXTENSION);

$ext = pathinfo($filePath, PATHINFO_EXTENSION);

回答by Leonid Shumakov

The main error here - you're passing a wrong object (Intervention\Image\Image) instead of File.

这里的主要错误 - 您传递了错误的对象 ( Intervention\Image\Image) 而不是 File。

In your case should work something like this:

在你的情况下应该是这样的:

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
        )
    ));
}