Last active
July 12, 2018 08:29
-
-
Save rmzaki/d1f7e3a7ff812fdbc0cf3ce9cd51cd15 to your computer and use it in GitHub Desktop.
exif情報にorientationが付与された画像(主にスマホで撮影した画像)を正常な向きに補正する。png、jpg対応。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Image extends AppModel | |
{ | |
public function beforeValidate($options = array()) | |
{ | |
// 画像の回転補正 | |
if (isset($this->data[$this->alias]['image']['tmp_name'])) { | |
$file_path = $this->data[$this->alias]['image']['tmp_name']; | |
if (strpos($this->data[$this->alias]['image']['type'], 'jpeg') !== false) { | |
$img_type = 'jpg'; | |
} elseif (strpos($this->data[$this->alias]['image']['type'], 'png') !== false) { | |
$img_type = 'png'; | |
} else { // それ以外は受け付けない | |
return; | |
} | |
// 補正なし | |
$orientation = 1; | |
try { | |
// @つけてるからtry~catchいらないかも | |
$exif = @exif_read_data($file_path, 'EXIF'); | |
if (isset($exif['Orientation'])) { | |
$orientation = $exif['Orientation']; | |
} | |
} catch (Exception $e) { | |
$this->log($e->getMessage()); | |
} | |
$this->imageRotation($file_path, $orientation, $img_type); | |
} | |
return; | |
} | |
/** | |
* Exif 画像の向き 回転/反転による調整 | |
* | |
* @param string $img_path | |
* @param string $orientation | |
* @param string $img_type | |
* @return void | |
* @access private | |
*/ | |
private function imageRotation($img_path, $orientation, $img_type) | |
{ | |
// 存在チェック | |
if (!file_exists($img_path)) { | |
return; | |
} | |
if ($img_type == 'jpg') { | |
// コンテントタイプ | |
header('Content-type: image/jpeg'); | |
// 読み込み | |
$img = imagecreatefromjpeg($img_path); | |
} elseif ($img_type == 'png') { | |
// コンテントタイプ | |
header('Content-Type: image/png'); | |
// 読み込み | |
$img = imagecreatefrompng($img_path); | |
} | |
$degrees = 0; | |
$mode = ''; // 文字列じゃなくて定数が入る | |
switch($orientation) { | |
case 1: // 通常 | |
break; | |
case 2: // 水平反転 | |
$mode = IMG_FLIP_HORIZONTAL; | |
break; | |
case 3: // (反時計回りに)180°回転 | |
$degrees = 180; | |
break; | |
case 4: // 垂直反転 | |
$mode = IMG_FLIP_VERTICAL; | |
break; | |
case 5: // 水平反転、 反時計回りに270°回転 | |
$degrees = 270; | |
$mode = IMG_FLIP_HORIZONTAL; | |
break; | |
case 6: // 反時計回りに270°回転 | |
$degrees = 270; | |
break; | |
case 7: // 垂直反転、 反時計回りに90°回転 | |
$degrees = 90; | |
$mode = IMG_FLIP_HORIZONTAL; | |
break; | |
case 8: // 反時計回りに90°回転 | |
$degrees = 90; | |
break; | |
} | |
// 回転 | |
if ($degrees > 0) { | |
$img = imagerotate($img, $degrees, 0); | |
} | |
// 反転 | |
if (!empty($mode)) { | |
imageflip($img, $mode); | |
} | |
if ($img_type == 'jpg') { | |
// 出力 | |
imagejpeg($img, $img_path); | |
} elseif ($img_type == 'png') { | |
// 出力 | |
imagepng($img, $img_path); | |
} | |
// メモリの解放 | |
imagedestroy($img); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exif情報について | |
http://qiita.com/RichardImaokaJP/items/385beb77eb39243e50a6 | |
sample画像 | |
https://github.com/recurser/exif-orientation-examples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment