user2636197 user2636197 - 28 days ago 18
PHP Question

php laravel What is the Stream() function in image intervention

I wonder what the stream() function in image intervention does? http://image.intervention.io/api/stream

Right now I am uploading my images to amazon S3 like this:

public function uploadLargeAndMainImages($file,$adId)
{
$s3 = Storage::disk('s3');
$extension = $file->guessExtension();
$filename = uniqid() . '.' . $extension;

//Create and resize images
$image = Image::make($file)->resize(null, 600, function ($constraint) {
$constraint->aspectRatio();
});
$image->encode($extension);

$imageLarge = Image::make($file)->resize(null, 800, function ($constraint) {
$constraint->aspectRatio();
});
$imageLarge->encode($extension);

// upload image to S3
$s3->put("images/{$adId}/main/" . $filename, (string) $image, 'public');
$s3->put("images/{$adId}/large/" . $filename, (string) $imageLarge, 'public');

// make image entry to DB
$file = File::create([
'a_f_id' => $adId,
'file_name' => $filename,
]);

}

Answer

Its all written in the Intervention Docs you've mentioned above:

The stream() method encodes the image in given format and given image quality and creates new PSR-7 stream based on image data.

It returns a PSR-7 stream as instance of GuzzleHttp\Psr7\Stream.

// encode png image as jpg stream
$stream = Image::make('public/foo.png')->stream('jpg', 60);

Just for hte sake of demonstration you can use the stream() method with S3 like this:

...
$image_normal = Image::make($image)->widen(800, function ($constraint) {
    $constraint->upsize();
});
$image_thumb = Image::make($image)->crop(100,100);
$image_normal = $image_normal->stream();
$image_thumb = $image_thumb->stream();

Storage::disk('s3')->put($path.$file, $image_normal->__toString());
Storage::disk('s3')->put($path.'thumbnails/'.$file, $image_thumb->__toString());

It think you get it!