Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing uploaded image in Laravel and store in S3 is not working

I am trying to upload a couple of resized images to S3, but somehow all images have the same size. Storing them locally in different sizes gives no problems. What am I missing?

public function uploadFileToS3(Request $request) {
    $image = Image::make($request->file('image'))->encode('jpg', 75);
    $s3 = Storage::disk('s3');

    $image_file_name = $this->generateName($request->name) . '.jpg';
    $file_path = '/' . config('folder') . '/' . $request->name . '/';

    $s3->put($file_path.'original_'.$image_file_name, $image, 'public');
    $s3->put($file_path.'medium_'.$image_file_name, $image->fit(300, 300), 'public');
    $s3->put($file_path.'thumb_'.$image_file_name, $image->fit(100, 100), 'public');

    return json_encode(array(
        'filename' => $image_file_name
    ));
}

All versions are stored successfully in S3, only all in the same size

like image 384
Alvin Bakker Avatar asked Oct 07 '15 19:10

Alvin Bakker


1 Answers

I have two possible solutions.

Try to do the image manipulation completely before attempting to store them:

$s3->put($file_path.'original_'.$image_file_name, $image, 'public');
$image->fit(300, 300);
$s3->put($file_path.'medium_'.$image_file_name, $image, 'public');
$image->fit(100, 100);
$s3->put($file_path.'thumb_'.$image_file_name, $image, 'public');

Try to cast the image to a string, the actual output file contents should work fine:

$s3->put($file_path.'original_'.$image_file_name, (string) $image, 'public');
like image 55
Lance Pioch Avatar answered Oct 05 '22 00:10

Lance Pioch