phpで使いそうな画像処理のクラス2

前回のコードが時間がないままフレームワークの中で使っていたものを汎用的にしたので
最悪だったのですが
今回はまだマシかと思います。
とりあえず直しただけ多分動く

一つ目は戒めとして残しておきます。

<?php
class ImageChanger{
      const FILE_MAX_LENGTH = 750;
      const COMPRESSION = 60;

      private $target_file;
      private $width;
      private $height;
      private $type;

      function __construct($target){
            $this->target_file = $target;
      }

      /* public method */
      public function getFile(){ return $this->target_file; }

      //画像の縮小
      public function createImage(){ //->bool
            if(file_exists($this->target_file) == false){
                  return false;
            }
            $this->getImageInformation();
            $size = $this->imageSize(self::FILE_MAX_LENGTH);
            //型作り
            $new_image = imagecreatetruecolor($size['width'], $size['height']);

            switch($this->type){
            case 'image/gif':
                  $base_image = imagecreatefromgif($this->target_file);
                  break;
            case 'image/png':
                  $base_image = imagecreatefrompng($this->target_file);
                  break;
            case 'image/jpeg':
            case 'image/jpg':
                  $base_image = imagecreatefromjpeg($this->target_file);
                  break;
            default:
                  return false;

            }
            //型に合わせて作成
            imagecopyresampled($new_image, $base_image, 0, 0, 0, 0, $size['width'], $size['height'],  $this->width, $this->height);
            ImageDestroy($base_image);

            //元データの削除
            if(file_exists($this->target_file)){
                  unlink($this->target_file);
                  $this->target_file = "";
            }
            $file_path = $this->getNewImagePath();

            imagejpeg($new_image, $file_path, self::COMPRESSION);
            ImageDestroy($new_image);

            $this->target_file = $file_path;
            return true;
      }

      //画像の向きを元に戻す
      public function rotateImage(){ //->bool
            if(file_exists($this->target_file) == false){
                  return false;
            }
            $this->getImageInformation();

            if($this->type != "image/jpeg" && $this->type != "image/jpg"){
                  return true;
            }
            //型作り
            $new_image = imagecreatetruecolor($this->width, $this->height);

            $exif = $this->exifInformation($this->target_file);
            if($exif == null){
                  return false;
            }

            $base_image = imagecreatefromjpeg($this->target_file);
            if(isset($exif['IFD0']['Orientation'])){
                  //回転処理
                  $rotate_infos = $this->imageRotation($exif['IFD0']['Orientation']);

                  //反転
                  if(!empty($rotate_infos['mode'])){
                        $base_image = imageflip($base_image, $rotate_infos['mode']);
                  }

                  //回転
                  if($rotate_infos['degrees'] > 0){
                        $base_image = imagerotate($base_image, $rotate_infos['degrees'], 0);
                  }
            }

            //元データの削除
            if(file_exists($this->target_file)){
                  unlink($this->target_file);
                  $this->target_file = "";
            }
            $file_path = $this->getNewImagePath();

            imagejpeg($base_image, $file_path, self::COMPRESSION);
            ImageDestroy($base_image);

            $this->target_file = $file_path;
            return true;
      }


      /* private method */
      //同じ縦横比のまま長いほうが指定した数値になるように縦横を返す
      private function imageSize($max_length){ // -> [int, int]
            if($this->width < $max_length || $this->height < $max_length){
                  $w = $this->width;
                  $h = $this->height;
            }elseif($this->width > $this->height){
                  $ratio = $this->height / $this->width;
                  $w = $max_length;
                  $h = round($w * $ratio);
            }elseif($this->width < $this->height){
                  $ratio = $this->width / $this->height;
                  $h = $max_length;
                  $w = round($h * $ratio);
            }else{
                  $w = $max_length;
                  $h = $max_length;
            }
            $size = ['width' => $w, 'height' => $h];
            return $size;
      }

      private function getNewImagePath(){ // -> string
            $name = str_replace('.', '', microtime(true));
            $filepath = 'tmp/'. $name. '.jpeg';
            return $filepath;
      }

      //file 情報取得
      private function getImageInformation(){ //->void
            $infos = getimagesize($this->target_file);
            $this->width = $infos[0];
            $this->height = $infos[1];
            $this->type = $infos['mime'];
      }


      //exif情報取得
      private function exifInformation(){
            if(file_exists($this->target_file) == false) return null;
            $exif = exif_read_data($this->target_file, 0, true);
            if($exif === false){
                  return null;
            }else{
                  return $exif;
            }
      }

      //イメージの回転修正
      //ファイルのパスをもらって回転の処理に必要な$mode, $degreesを返す
      private function imageRotation($exif){ //-> [string, int]
            if(isset($exif) == false){
                  return ["", 0];
            }
            $degrees = 0;
            $mode = '';

            switch($exif){
            case 1:
                  break;
            case 2:
                  $mode = 'IMG_FLIP_HORIZONTAL';
                  break;
            case 3:
                  $degrees = 180;
                  break;
            case 4:
                  $mode = 'IMG_FLIP_VERTICAL';
                  break;
            case 5:
                  $degrees = 90;
                  $mode = 'IMG_FLIP_HORIZONTAL';
                  break;
            case 6:
                  //$degrees = 90;
                  $degrees = 270;
                  break;
            case 7:
                  $degrees = 90;
                  $mode = 'IMG_FLIP_VERTICAL';
                  break;
            case 8:
                  $degrees = 270;
                  break;
            }

            return ['mode' => $mode, 'degrees' => $degrees];
      }

}