1
0

More than 1 year has passed since last update.

Laravel 圧縮した画像をS3にアップロード

Last updated at Posted at 2022-07-20

パッケージインストール
$ composer require intervention/image

フォームから画像を送信

use Illuminate\Http\UploadedFile;

public static function s3UploadResize ($file, $width=null, $height=null)
{
    if (!file_exists($tmp_file_dir = storage_path('app/tmp/'))) {
        mkdir($tmp_file_dir, 0777, true);
    }

    $filename = date('YmdHis').$file->getClientOriginalName();
    $path = '/user';

    // 圧縮した画像を一時フォルダに保存
    $tmpPath = storage_path('app/tmp/') . $filename;
    $image = \Image::make($file)
                    ->resize($width, $height, function ($constraint) {
                        $constraint->aspectRatio();
                    })
                    ->save($tmpPath);
    $file = new UploadedFile($tmpPath, $filename);
    $path = $file->storeAs($path, $filename, 's3');
    \Storage::delete('tmp/'.$filename);
    return $path;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0