Skip to content

Instantly share code, notes, and snippets.

@webgyo
Last active December 25, 2018 09:32
Show Gist options
  • Save webgyo/df216c90fadce267ed43d761aa75728b to your computer and use it in GitHub Desktop.
Save webgyo/df216c90fadce267ed43d761aa75728b to your computer and use it in GitHub Desktop.
Image Magickのconvertコマンドでexif削除や回転情報を保持する #php #ImageMagick #convert

iphoneなどでとった写真には位置情報や回転情報があるがexifを削除すると 回転情報が消えて画像の向きがおかしくなるので回転情報だけは保持するようにする

exiftool ・ Image Magick で Exif Orientation 情報を追加・編集する
http://kuteken.hatenablog.com/entry/2013/11/10/180802

orient image orientation
http://www.imagemagick.org/script/command-line-options.php#orient

ImageMagickによる基本的な画像処理
http://www.gi.ce.t.kyoto-u.ac.jp/user/susaki/image/magick_process.html

iPhoneからアップロードしたJPEG写真が横向きになる問題(EXIF, Orientation)
https://qiita.com/RichardImaokaJP/items/385beb77eb39243e50a6

以下の方法はメモリを大量に消費するので注意

PHPでEXIFのOrientationを元に画像を回転 https://qiita.com/uda0922/items/c83c239eb32ed295fca8

// $input_imageには画像情報
$input_image;

// exif消す前にorientationだけ保持
$exif = exif_read_data($input_image);
$orientation = $exif['Orientation'];
$angle = 0;
if ($orientation == 3) {
    $angle = 180;
} elseif ($orientation == 6) {
    $angle = 90;
} elseif ($orientation == 8) {
    $angle = -90;
} else {
    $angle = 0;
}
// exifを削除
exec("convert -strip -rotate {$angle} $input_image $output_image");

-stripで余計やexifを削除している

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment