1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel5.7のバリデーションでJPEG画像が画像として認識されない現象

Posted at

はじめに

古いLaravel(5.7)で画像のバリデーションを行った際に、何故かJPEG画像だけが画像として認識されない(エラーで弾かれてしまう)現象がありました。
バリデーションの内容は下記です。実にシンプル。

PNGやGifは問題無いのに、なぜ?

MediaRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MediaRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'file' => 'required|image',
        ];
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'file' => '画像ファイル',
        ];
    }
}

要因

composer update を行った後から発生したので、何かのパッケージが悪さしているのは明白です。
バージョンを戻した上で、改めてひとつずつパッケージを確認していきました。

すると、symfony/mime をアップデートした際に発生する事がわかりました。

$ composer update symfony/mime --dry-run
- Updating symfony/mime (v5.1.2) to symfony/mime (v5.3.0)

どうやら、symfonyがv5.2以降で拡張子の順序を変更したため、
Laravelのバリデーション時にJPEG画像のmimetypeが一致しなくなり、エラーとして弾かれてしまうようになったとのこと。:joy:

/vendor/symfony/mime/MimeTypes.php(v5.1.2)
'image/jpeg' => ['jpeg', 'jpg', 'jpe'],
/vendor/symfony/mime/MimeTypes.php(v5.3.0)
'image/jpeg' => ['jpg', 'jpeg', 'jpe'],

解決方法

手動でパッチを当てる事で解決できるそうですが、私の場合は symfony/mime のバージョンを5.1に固定する事で難を逃れる事としました。

composer.json
"require": {
    "symfony/mime": "5.1.*",
},

おわりに

迂闊に composer update などしてはいけませんね。:sweat_smile:
古いLaravelをお使いの方はお気を付けください。

参考

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?