PR

[CakePHP] 画像ファイルを読み込んで表示する

前回、画像ファイルを保存しました。

pc.casey.jp » PHP|画像を縮小して保存する :
http://pc.casey.jp/archives/2486

今回は読み込んで、そのまま出力てみます。(ちなみに imagejpeg の引数を調整すると出力時の画質などを調整できる)存在しないファイルの場合cakeErrorの404を出すようになっています。

スポンサードリンク

	function index(){
		// test data
		$file = WWW_ROOT . "output.jpg";
		$this->_displayJpg($file);
	}

	function _displayJpg($file){
		Configure::write('debug', 0);

		if($img = @imagecreatefromjpeg($file)){
			$this->layout = false;
			$this->autoRender = false;
			header('Content-type: image/jpeg');
			imagejpeg($img, null, 100);
			imagedestroy($img);
		}else{
			$this->cakeError('error404');
		}
	}

参考文献

コメント