LoginSignup
1
0

More than 3 years have passed since last update.

php 画像向き修正

Posted at
image.php
$filename = "パス"

if (mime_content_type($filename) == 'image/jpeg') {
          $image = ImageCreateFromJPEG($filename);
        }else {
          $image = ImageCreateFromPNG($filename);
        }


        $exif_datas = @exif_read_data($filename);
        if (isset($exif_datas['Orientation'])) {
          $orientation = $exif_datas['Orientation'];

          //回転角度
          $degrees = 0;
          switch($orientation) {
            case 1:     //回転なし(↑)
              break;
            case 8:     //右に90度(→)
              $degrees = 90;
              break;
            case 3:     //180度回転(↓)
              $degrees = 180;
              break;
            case 6:     //右に270度回転(←)
              $degrees = 270;
              break;
            case 2:     //反転 (↑)
              $mode = IMG_FLIP_HORIZONTAL;
              break;
            case 7:     //反転して右90度(→)
              $degrees = 90;
              $mode = IMG_FLIP_HORIZONTAL;
              break;
            case 4:     //反転して180度なんだけど縦反転と同じ(↓)
              $mode = IMG_FLIP_VERTICAL;
              break;
            case 5:     //反転して270度(←)
              $degrees = 270;
              $mode = IMG_FLIP_HORIZONTAL;
              break;
          }
          //反転(2,7,4,5)
          if (isset($mode)) {
            $image = imageflip($image, $mode);
          }
          //回転(8,3,6,7,5)
          if ($degrees > 0) {
            $image = imagerotate($image, $degrees, 0);
          }

          //保存
          if (mime_content_type($filename) == 'image/jpeg') {
            ImageJPEG($image, $filename);
          }else {
            ImagePNG($image, $filename);
          }
          // //メモリ解放
          imagedestroy($image);
        }
1
0
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
0