約 5 分で読めます。
最近自分のWordPressにアップロードした画像が回転してしまっていたので、直した記事。
結論から言うと、こちらの記事を参考にさせていただき、EXIF情報を使うように修正しただけ。
[WordPress] スマホで縦向き撮影した写真が勝手に横向く(回転する)問題 | IT女子のお気に入りフォルダ
スマホで縦向きに撮影した写真(JPEG画像)をWordPressにアップロードしたら、勝手に回転して横向きにな
こちらがfunctions.phpに書いたコード。
switch ($orientation)からの部分がキモで、EXIF情報に基づき回転させて画像を保存しています。
↑で紹介したURLに載っているコードそのまんま…
function rotate_uploaded_image_file($file) {
if ($file['type'] == 'image/jpeg') {
$image = wp_get_image_editor($file['file']);
if (!is_wp_error($image)) {
$exif = exif_read_data($file['file']);
$orientation = $exif['Orientation'];
if (!empty($orientation)) {
switch ($orientation) {
case 8:
$image->rotate(90);
break;
case 3:
$image->rotate(180);
break;
case 6:
$image->rotate(-90);
break;
}
}
$image->save($file['file']);
}
}
return $file;
}
add_action('wp_handle_upload', 'rotate_uploaded_image_file');
一応公式ページを貼っておきます。

PHP: exif_read_data - Manual
PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites i...
また、exif_read_data()を使うためにexif拡張を入れる必要がありました。
これはこちらにコミットがありますので、参考にしてください。
Added exif extension for correct orientation of image file. · kurofuku/wordpress-container@eae9ddf
Contribute to kurofuku/wordpress-container development by creating an account on GitHub.
ちなみにこの問題に出くわして最初にググったときにいろいろな記事で紹介されていたこちらのプラグインは、自分の環境との組合せが悪かったのか、意味がありませんでした。

Image Rotation Repair
The Image Rotation Repair plugin simply fixes image orientation based on EXIF data. This is primarily a patch for mis-or...
終わりに
いかがでしたか。
最初は都度回転させていましたが、さすがに毎回やるのは面倒すぎるので対応しました!


Comments