EXIFにオリエンテーション(回転)が設定されている画像をアップロードすると、Chromeはそのオリエンテーションを尊重して表示します。 これを回避するためにこんなコンポーネントを作りました:
class FixOrientationComponent extends Component { public function autoRotate(\Imagick $image): \Imagick { switch ($image->getImageOrientation()) { case \Imagick::ORIENTATION_TOPLEFT: break; case \Imagick::ORIENTATION_TOPRIGHT: $image->flopImage(); break; case \Imagick::ORIENTATION_BOTTOMRIGHT: $image->rotateImage("#000", 180); break; case \Imagick::ORIENTATION_BOTTOMLEFT: $image->flopImage(); $image->rotateImage("#000", 180); break; case \Imagick::ORIENTATION_LEFTTOP: $image->flopImage(); $image->rotateImage("#000", -90); break; case \Imagick::ORIENTATION_RIGHTTOP: $image->rotateImage("#000", 90); break; case \Imagick::ORIENTATION_RIGHTBOTTOM: $image->flopImage(); $image->rotateImage("#000", 90); break; case \Imagick::ORIENTATION_LEFTBOTTOM: $image->rotateImage("#000", -90); break; default: // Invalid orientation break; } $image->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT); return $image; } }
使い方ですが、こんな感じです:
$image = new \Imagick("/tmp/gazou.jpg"); // Fix orientation info so displays correctly in all browsers $image = $this->loadComponent('FixOrientation')->autoRotate($image);
ほぼこちらの内容通りです!