2

我有问题imagejpeg()。我知道如何将图像保存/上传到 s3。我现在的问题。使用imagejpeg调整图像大小,但这仅保存到本地目录。我想把它保存到s3。我正在使用laravel。这是我的代码。任何帮助表示赞赏。谢谢

s3 配置和路径。

    $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)
4

1 回答 1

3

这种方法效果很好。如果您查看文档,imagejpeg您可以看到它们是如何将输出作为图像的。关联

 $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
于 2018-09-02T14:42:29.637 回答