- 2007-07-20 (Fri) 3:23
- CakePHP関連
まいったね、ちょっと眠い。午前3時・・・今、目黒のネカフェ・・・。
さて、画像アップ処理の続きです。なんかgdgdなコードだけど晒します。明日直したい。
■ 画像アップ処理(users_controller.php)
edit()部分で画像処理部分を以下のように変更。簡単にいうと、エラーチェックして、だめならだめで、OKならファイル形式を見ながら横幅が50px(=PIC_WIDTH定数を/config/app.phpに定義)になるように調整して出力、というもの。
// file upload
if (!empty($this->data['User']['pic']['name']))
{
// tmp file info
$tmp_file = $this->data['User']['pic']['tmp_name'];
$imginfo = getimagesize($tmp_file);
// file error handling (file size / JPG,GIF,PNG)
clearstatcache();
if (filesize($tmp_file)>300000 || ($imginfo[2] < 1 || $imginfo[2] > 3)) { $this->set('file_error',true); return false; }
// set file name
$ext = low(preg_replace("!.*\.!", null, $this->data['User']['pic']['name']));
$filename = sprintf("%05d.%s",$me['User']['id'], $ext);
$upload_path = WWW_ROOT . "pics" . DS;
// set new width, height
$width_old = $imginfo[0];
$height_old = $imginfo[1];
$width_new = PIC_WIDTH;
$height_new = $height_old * ($width_new / $width_old);
// create new file
switch ($imginfo[2]) {
case 2: // jpeg
$jpeg = imagecreatefromjpeg($tmp_file);
$jpeg_new = imagecreatetruecolor($width_new, $height_new);
imagecopyresampled($jpeg_new,$jpeg,0,0,0,0,$width_new,$height_new,$width_old,$height_old);
imagejpeg($jpeg_new, $upload_path . $filename, 100);
break;
case 1: // gif
$gif = imagecreatefromgif($tmp_file);
$gif_new = imagecreatetruecolor($width_new, $height_new);
imagecopyresampled($gif_new,$gif,0,0,0,0,$width_new,$height_new,$width_old,$height_old);
imagegif($gif_new, $upload_path . $filename, 100);
break;
case 3: // png
$png = imagecreatefrompng($tmp_file);
$png_new = imagecreatetruecolor($width_new, $height_new);
imagecopyresampled($png_new,$png,0,0,0,0,$width_new,$height_new,$width_old,$height_old);
imagepng($png_new, $upload_path . $filename, 100);
break;
Default:
break;
}
$this->data['User']['pic'] = $filename;
} else {
$this->data['User']['pic'] = $me['User']['pic'];
}
// file delete
if (empty($this->data['User']['pic']['name']) && file_exists(WWW_ROOT."pics".DS.$me['User']['pic']) && $this->data['User']['pic_del'])
{
unlink(WWW_ROOT."pics".DS.$me['User']['pic']);
$this->data['User']['pic'] = null;
}
ま、テストもしてみてうまく動いているのだけど、いくつかどうにかしたい点が。
- この処理ってモデルの方で吸収したほうがいいの?beforeSave()とかで?
- エラー処理ってこれでOK?なんかだめだったらreturn false;しちゃえばいいの?
- エラー処理ってHTMLヘルパー使うべき?$validateとかモデルに書いたから使えばいいのかな?
- WWW_ROOT.”pics”.DSって書くのが面倒。これってどっかで定数定義しちゃったほうがいいの?
■ 画像表示編(users_controller.php、home.thtml)
で、このようにアップした画像を/users/home/で表示させたいのだが、次のようにやっている。これがとてつもなく頭悪い感じ。なんとかしたい。横幅は50pxで固定なのでいいのだけど、縦幅をこのようにいちいち取得している。
function home ()
{
$this->checkSession();
$this->set('sys_msg', $this->Session->read('sys_msg'));
$this->Session->delete('sys_msg');
$me = $this->User->findById($this->Session->read('my_id'));
$this->set('me', $me);
$imginfo = WWW_ROOT . "pics" . DS . $me['User']['pic'];
$this->set('pic_height', $imginfo[1]);
}
それからhome.thtmlの画像表示部分はこんな感じ。
<img src="/pics/<?= $me['User']['pic']; ?>" width="<?= PIC_WIDTH; ?>" height="<?= $pic_height; ?>" border="0" alt="" />
こ、これはモデルでなんとかできるんだよね、きっと・・・。$me['User']['pic_height']とかでアクセスしたいのだが・・。
ま、でも動作的にはこんなものですかね。思ったより時間かかったな・・・。ここでいったんすべてのコードを見直してみますかね。

↑ テスト完了。画像の縮小、JPEG/GIFでのアップも問題なし。削除もできました。
それができたら次はログイン時の「情報を保存する」をやってみたい。これはCookieを使わないとですね。Cookieへのアクセスの仕方勉強しなくちゃ。
それが終わったら日記の処理・・・次に友達の管理か・・・まだまだ先は長いな・・・。
・・・とりあえず寝るか・・・。
※ CakePHP修業は百式管理人がSNSっぽいものをCakePHPで作ろうとして挫折するまでの日記です。前回までのあらすじはこちらへ。
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.ideaxidea.com/archives/2007/07/iiicakephp_31.html/trackback
- Listed below are links to weblogs that reference
- 画像のアップロード処理をつくる【III】(CakePHP修行 #31) from IDEA*IDEA ~ 百式管理人のライフハックブログ


























