※2分で読めます
Intervention Imageとは?
Intervention Imageは、オープンソースのPHP画像操作ライブラリで、画像を操作する簡単な方法を提供します。Laravelでの画像処理には欠かせないライブラリです。Composerパッケージを利用することで、サイズ変更や指定したファイルへのデータ保存などを行うことができます。
Laravel9でIntervention Imageを導入する方法
以下の手順に従って、Laravel9にIntervention Imageを導入してください。
1. ComposerでIntervention Imageをインストール
ターミナルを起動して、プロジェクトのルートディレクトリに移動し、
以下のコマンドを実行します。
composer require intervention/image
2. ServiceProviderの登録
app.phpファイルにて、Intervention ImageのServiceProviderを追加します。
'providers' => [
// ...
Intervention\Image\ImageServiceProvider::class,
],
3. Facadeの登録
app.phpファイルにて、Intervention ImageのFacadeを追加します。
'aliases' => [
// ...
'Image' => Intervention\Image\Facades\Image::class,
],
4. 画像のサイズ変更
以下は、画像をリサイズする例です。
$image = Image::make('public/foo.jpg')->resize(300, 200);
5. 画像のサイズ変更して保存
以下は、画像をリサイズして保存する例です。
$image = $request->file('home_slide');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(636,852)->save('upload/home_slide/'.$name_gen);
以上で、Laravel9でのIntervention Imageの導入方法が完了です。
まとめ
LaravelでIntervention Imageを導入する方法を説明しました。この記事を参考にして、簡単にLaravelアプリケーション内で画像処理を行ってください。より詳細な情報については、公式サイトを参照してください。
公式サイト: https://image.intervention.io/v2/introduction/installation