前回、画像ファイルを保存しました。
pc.casey.jp » PHP|画像を縮小して保存する :
https://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');
}
}
参考文献
- エラーハンドリング(Error Handling) :: CakePHPによる作業の定石 :: マニュアル :: 1.2コレクション :: The Cookbook :
http://book.cakephp.org/ja/compare/154/Error-Handling - [CakePHP] 画像の圧縮比を変えて出力する – Sometime PHP :
multiburst.net


コメント