10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アップロードした写真(画像)が回転して表示されるのを直す Image_Driver

Last updated at Posted at 2013-08-25

http://qiita.com/hiro_y/items/0476bcf39a77ca184009 に書いてあることを FuelPHP でやるときにどうやるのがいいかっていう話。

理屈は一緒です。

<?php

class Image_Gd extends \Fuel\Core\Image_Gd {

	public function _orientation()
	{
		if ( function_exists('exif_read_data'))
		{
			try
			{
				$exif = exif_read_data($this->image_fullpath);
			}
			catch (Exception $e)
			{
				Log::error($e->getMessage());
				return;
			}

			if (isset($exif['Orientation']))
			{
				$orientation = $exif['Orientation'];

				if ($orientation == 2)
				{
					$this->flip('horizontal');
				}
				else if ($orientation == 3)
				{
					$this->rotate(180);
				}
				else if ($orientation == 4)
				{
					$this->flip('vertical');
				}
				else if ($orientation == 5)
				{
					$this->rotate(-90);
					$this->flip('vertical');
				}
				else if ($orientation == 6)
				{
					$this->rotate(90);
				}
				else if ($orientation == 7)
				{
					$this->rotate(90);
					$this->flip('vertical');
				}
				else if ($orientation == 8)
				{
					$this->rotate(-90);
				}
			}
		}
		else
		{
			$this->debug("Exif information is not defined.");
		}
	}
}
<?php

abstract class Image_Driver extends \Fuel\Core\Image_Driver
{
	public function orientation()
	{
		$this->queue('orientation');
		return $this;
	}
}
<?php

class Image extends \Fuel\Core\Image {

	/**
	 * @return  Image_Driver
	 */
	public static function orientation()
	{
		return static::instance()->orientation();
	}

}

あとは fuel/app/bootstrap.php に

<?php

Autoloader::add_classes(array(
	'Image' => APPPATH.'classes/image.php',
	'Image_Driver' => APPPATH.'classes/image/driver.php',
	'Image_Gd' => APPPATH.'classes/image/gd.php'
));

を追加する。

こうすると、preset の action として orientation というのが使えるようになって、画像を回転してくれるようになる。


いろいろ抜けてたので追記しました。

10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?