Laravelで使用できるライブラリには、非常に簡単に画像を編集できるライブラリが存在します。
PHP Image Processing
https://github.com/Intervention/image
1,494 forks.
14,370 stars.
22 open issues.
Recent commits:
- Refine coding standard (#1525), GitHub
- Merge branch 'develop', Oliver Vogel
- Keep the ICC profile of a stripped Imagick image in PNG output (#1524)Imagick::stripImage() does not remove the meta data from the propertycache. It sets a "png:exclude-chunk" artifact instead, which tells thePNG encoder to skip the chunks that would carry it.That artifact stays on the wand and the PNG encoder skips every profileas soon as the text chunks are excluded (coders/png.c, the profile loopis guarded by "exclude_tEXt == false || exclude_zTXt == false"). So theprofile that StripMetaModifier reads back and re-applies never reachesan encoded PNG, and neither does a resolution set afterwards.The artifact outlives the modifier, so this is not limited to callingthe modifier directly. Encoding an image as JPEG with strip enabled andthen as PNG drops the profile of the PNG as well.Drop the artifact and clear the property cache instead, so the meta datais really gone for every encoder. The sweep keeps "png:" and "jpeg:"properties, which are encoding hints that the PNG encoder skips anyway.Imagick::getImageProperties() cannot be the only source for that sweep,because it silently skips every property whose name starts with "[",which a PNG text chunk is able to produce. The PNG encoder has no suchrestriction and would write those back out. The property names reportedby the "%[*]" format complete the list. That format builds a string ofevery property and its value, so it runs last, once the properties thatcan be enumerated are already gone.A stripped PNG now carries the cHRM, bKGD and pHYs chunks that theartifact used to suppress, and is a little larger for it., GitHub
- Remove unused code, Oliver Vogel
- Remove test, 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の写真の回転処理でお悩みの方は試してみてください。