Laravel9でIntervention Imageが動かない

WEB開発
この記事は約3分で読めます。
スポンサーリンク

#composer require intervention/image
このような形でcomposerでlaravel9にインストールすると現在バージョン3が自動的にインストールされる。
現在見受けられる記事の多くはバージョン2に関して書いてあるもので、
それを見て記載するとエラーを吐いてしまう
use Intervention\Image\Facades\Image; が見つからないということが多々あるだろう

回避する方法は、
# composer require intervention/image:^2 のような形でバージョンを2にするか、
バージョン3の書き方をすればよい。

公式に使用方法が書かれている
https://image.intervention.io/v3/introduction/installation

私は今回バージョン3をインストールし使用方法に従い次のように利用した
以下はgdライブラリを用い、$imageの画像データを読み込み、横を150pxまで縮小する書き方だ

use Intervention\Image\ImageManager;
     
$im = ImageManager::gd()->read($image);
$im->scaleDown(width: 150);

また、バージョン3にはimage-laravelといった専用のパッケージも存在している。
しかしlaravel9では他のパッケージのバージョン問題で入れることができなかった。
おそらく最新のlaravelでなら利用できるのだろう

# composer require intervention/image-laravel
./composer.json has been updated
Running composer update intervention/image-laravel
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires intervention/image-laravel * -> satisfiable by intervention/image-laravel[dev-main, 0.0.1, 0.0.2, 0.0.3, 1.0.0, 1.0.1, 9999999-dev].
    - intervention/image-laravel[dev-main, 0.0.1, ..., 0.0.3, 1.0.0, ..., 1.0.1] require illuminate/support ^10.0 -> found illuminate/support[v10.0.0, ..., 10.x-dev] but these were not loaded, likely because it conflicts with another require.

You can also try re-running composer require with an explicit version constraint, e.g. "composer require intervention/image-laravel:*" to figure out if any version is installable, or "composer require intervention/image-laravel:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

コメント

タイトルとURLをコピーしました