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

jackjoesmith picture jackjoesmith · Nov 11, 2015 · Viewed 7.3k times · Source

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:

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

Update I 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:

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?

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

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

Answer

ajtrichards picture ajtrichards · Jan 16, 2016

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

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

$filename = 'aUniqueFilename.png';

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