Resizing uploaded image in Laravel and saving in S3 does not work

I am trying to upload a couple of modified images to S3, but somehow all the images are the same size. Storing them locally in different sizes does not pose a problem. 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 one size

+4
source share
2 answers

I have two possible solutions.

Try to fully process the images before trying to save 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 applying the image to the line, the actual contents of the output file should work fine:

$s3->put($file_path.'original_'.$image_file_name, (string) $image, 'public');
+7
source

... , , tmp, . , , , , , Laravel.

    $folder = $_SERVER['DOCUMENT_ROOT'] . '/tmp/';
    $image_file_name = $this->generateName($request->name) . '.jpg';

    // save original
    $image->save($folder . 'original_' . $image_file_name);
    $s3->put($file_path.'original_'.$image_file_name, fopen($folder.'original_'.$image_file_name, 'r+'), 'public');

    // generate thumb
    $image = $image->fit(100, 100);
    $image->save($folder . 'thumb_' . $image_file_name);
    $s3->put($file_path.'thumb_'.$image_file_name, fopen($folder.'thumb_'.$image_file_name, 'r+'), 'public');
0

Source: https://habr.com/ru/post/1610638/


All Articles