php – Laravel and Intervention/Image: Uploading photos to a AWS S3 bucket results in 0 byte files-ThrowExceptions

Exception or error:

I’m using http://image.intervention.io/ with Laravel to resize images before storing them in an AWS S3 bucket. Here is the applicable code:

$extension = $image->extension();
$realPath = $image->getRealPath();
$resizedImage = Image::make($realPath) // ->resize(255, 255); // resize or not, make() seems to always return an empty Image
$file_path = "posts/$post->id/images/image-$post->id-num-$pictureCount.$extension/"; 
$path = Storage::disk('s3')->put($file_path, $resizedImage->__toString(), ['visibility' => 'public']);

When debugging (breakpoint on the line $path is set) this I can see that $realPath holds a value like /private/var/folders/23/jl2dytp168g9g9s5j51w2m780000gn/T/php9047Fh – and going there I can see the image that I’m trying to make()/resize().

The $resizedImage object has the following fields:

[encoded] => 
[mime] => image/jpeg
[dirname] => /private/var/folders/23/jl2dytp168g9g9s5j51w2m780000gn/T
[basename] => phpTNRdXe
[extension] => 
[filename] => phpTNRdXe

I’m guessing that the encoded property should hold something like the base 64 image data?

When checking the image in AWS S3 bucket, it is always getting uploaded as a image with 0 bytes. What am I doing wrong here?

Intervention is configured to use GD if it makes any difference

How to solve:

Laravel File class is a subclass of Symfony File class which in turn subclasses \ SplFileInfo

The file object can be passed directly in Image::make. Image::make gets the realPath for \SplFileInfo objects.

Image::make returns an instance of Image and $resizedImage->__toString() returns the value of its encoded field. encoded starts out as an empty string.

The image should be encoded before accessing its encoded value.

$resizedImage->encode();

Then,

$path = Storage::disk('s3')->put($file_path, $resizedImage->__toString(), ['visibility' => 'public']);

Leave a Reply

Your email address will not be published. Required fields are marked *