Laravelで使用できるライブラリには、非常に簡単に画像を編集できるライブラリが存在します。
PHP Image Processing
https://github.com/Intervention/image
1,490 forks.
14,351 stars.
18 open issues.
Recent commits:
- Add JPEG XL (JXL) encoder (#1506)* Add JPEG XL (JXL) encoderJXL is wired through the Format, MediaType and FileExtension enums andencoded with Imagick, mirroring the HEIC encoder. GD has no JXL support,so the GD driver reports it as unsupported and throws NotSupportedException.The encoder exposes quality, strip and a lossless flag. Lossless maps to acompression quality of 100, which is how ImageMagick's coder switches libjxlto lossless (distance 0). Adding image/jxl (and image/x-jxl, which libmagicreports for JXL blobs on some builds) to the media type enum also lets theImagick binary decoder read and round-trip .jxl input.CI runs on ubuntu-24.04 because libjxl-dev is not packaged for 22.04. Thatalso needs the libheif encoder plugins (x265, aomenc), since 24.04's libheifmoved its encoders out of libheif-dev and AVIF/HEIC would otherwise fail toencode. ImageMagick 6.9.13 does not build the JXL coder even with libjxl, sothe JXL tests skip when Imagick::queryFormats('JXL') is empty and run on the7.1.2 cells.* Remove lossless option from JXL encoderJXL has no independent lossless setting in ImageMagick. Lossless is just acompression quality of 100 (see coders/jxl.c), so a separate lossless flagwas redundant and wrongly implied quality and lossless could be setindependently. Dropping it also makes JxlEncoder match AvifEncoder andHeicEncoder., GitHub
- Reset page geometry after Imagick rotation (#1505)Imagick's rotateImage() grows the virtual canvas for non-right angles andleaves each frame with a negative page offset (a 480×480 frame rotated 45degrees ends up 680×680-100-100). ImageMagick's animated-AVIF writer(libheif sequences) honours that offset when compositing the sequence,shifting every frame up and left and leaving the bottom/right regiontransparent.Reset each frame's page to +0+0 after rotating, mirroring TrimModifierand CoverModifier which already normalize page geometry. This keeps theImagick driver consistent with the GD and vips drivers, which leave nooffset, and fixes the transparent-corner artifact in animated AVIF., GitHub
- Update github-workflow (#1504), GitHub
- Revert "Switch to php:8.3-cli-alpine for local test runner"This reverts commit 448b7fa472a845029f502439fc8fac6dfc104f76., Oliver Vogel
- Fix link, Oliver Vogel
非常に多機能で、バックエンドにImagemagickやGDを使用することができ、Laravelとも連携することが可能です。
Qiita
intervention/imageをインストール composerでインストールします。 composer requi…
さて、Laravelの自作中のプログラムで、下記のような処理を入れました。
$imageRaw = Storage::get(写真のパス);
$image = \Intervention\Image\Facades\Image::make($imageRaw); // 写真を指定パスから読み込んでInterventionImageオブジェクトを作成
$image->orientate(); // 画像のEXIF情報から正しい画像の向きに修正
$image->save(写真保存パス); // 指定されたパスに画像を保存
画像をそのままアップロードしただけではEXIF内の情報は考慮されず、元データのままの向きで画像が処理されてしまいますが、2行目のorientate()メソッドを使用することで、取得した画像のEXIF情報を取得して、画像を正しい方向に自動で修正してくれます。
ところが、iOSデバイスで撮影した画像については、このorientate()メソッドが働かない問題に当たりました。
こちらが、iPod touchで撮影した元の画像です。
こちらの画像をなにも処理せずにブラウザで表示すると横を向いたままになってしまいます。
さて、アップロード後、orientate()メソッドを実行すると…
補正処理を入れているにもかかわらずこんなふうに横を向いたままになってしまいます。
色々調べたところ、orientate()を実行した際、本来取得されるはずのEXIF情報が、写真によってはうまく取得されないという報告がありました。
GitHub
I'm trying to read my exif data. If I run $image->exif(), it…
こちらのIssueの内容によると、下記のようにして手動でEXIF情報をセットしなおすと解決するとのこと。
$imageRaw = Storage::get(写真のパス);
$image = \Intervention\Image\Facades\Image::make($imageRaw)->setFileInfoFromPath(Storage::path(写真のパス)); // EXIF情報を再セット
$image->orientate();
$image = $image->save(写真保存パス);
早速試してみたところ…
ちゃんと縦を向きました…!!!
iOSの写真の回転処理でお悩みの方は試してみてください。