Save imagejpeg() to s3

2k Views Asked by At

i have a problem with imagejpeg(). I know how how to save/upload image to s3. My problem now. The image was resize using imagejpeg but this was only save to local directory. I want to save it to s3. I'm using laravel. Here's my code. Any help is appreciated. Thanks

s3 config and path.

    $displaypath = '/cover/uploads/csspreview/';

    $s3 = \Storage::disk('s3');

    $img = $s3->get($data['uploaded_file_path']);

    $im = imagecreatefromstring($img);

    $width = imagesx($im);

    $height = imagesy($im);

    $newwidth = '335';

    $newheight = '500';


    $tuecolor = imagecreatetruecolor($newwidth, $newheight);

    imagecopyresampled($tuecolor, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

     // the above code is good.

    $path = './uploads/csspreview/'; // this is a local path only.

    imagejpeg($tuecolor,  $path.'/cover.jpg'); //save image as jpg (It will save to the file but I want it to save in s3)
1

There are 1 best solutions below

2
On

This method works fine. If you check the documentation of imagejpeg you can see it how they got outputs as the image.Link

 $jpg_image = imagecreatefromjpeg('https://s3.amazonaws.com/sample.jpg');
 $imageName = 'imageNameToStoreOnS3.jpg';

 ob_start();
 imagejpeg($jpg_image);
 $image_contents = ob_get_clean();

 $finalResultImageFullPath = 'final_view/'.$imageName; //S3 filename with path
 $finalImage = $s3->put($finalResultImageFullPath, $image_contents, 'public'); //Store on S3