实现头像上传及剪切功能(cakephp+jquery)

送给stone的文章:
此功能的优点就不多说了,具体实现如下:
1、必备:PHP4以上,gd库,jquery,及下载安装jquery.imgareaselect.min.js
2、创建组件jq_imgcrop.php,放在app/controllers/components
如果文件无法下载,用浏览器打开,查看源代码复制到相应文件中。
下载
3、创建helper助手cropimage.php,放在app/views/helpers/
下载
4、在控制器中添加引用
var $helpers = array(..., 'Cropimage') ;
var $components = array(..., 'JqImgcrop');

5、创建视图,使可以上传头像。
function icon()
{
if (!empty($this->data)) {
$uploaded = $this->JqImgcrop->uploadImage($this->data['User']['user_icon'], '/icon/', 'ef_',$this->Auth->user('user_id'));
$this->set('uploaded',$uploaded);
//echo $data['User']['user_icon'];exit;
if (!isset($data['User']['user_icon'])||$data['User']['user_icon']==''){
$suffix = substr(strrchr($this->data['User']['user_icon']['name'], "."), 1);
$user_icon = 'ef_'.$this->Auth->user('user_id').'.'.$suffix;
$this->User->query("UPDATE users SET user_icon = '".$user_icon."' where user_id= ".$this->Auth->user('user_id')."");
}}
$data= $this->User->findByUserId($this->Auth->user('user_id'), array("user_icon"));
$this->set('user_icon',isset($data['User']['user_icon'])?$data['User']['user_icon']:"user.jpg");
}

6、上传后跳转到的控制器:
function crop()
{
if (!empty($this->data)) {
$this->JqImgcrop->cropImage(48, $this->data['User']['x1'], $this->data['User']['y1'], $this->data['User']['x2'], $this->data['User']['y2'], $this->data['User']['w'], $this->data['User']['h'], $this->data['User']['imagePath'], $this->data['User']['imagePath']) ;
}
$this->redirect('/users/setting');
}

参考:

http://www.zhuyinghao.com/?p=220

Comments (10)

valentina十月 16th, 2009 at 19:14

朱老师,什么时候推出cakephp与flex结合的课程啊?

admin十月 20th, 2009 at 08:28

暂时没有出flex的教程打算,你可以参考一些flex的书籍,flex与php结合并不是太好,需要借助中间件。

遥远的期待十月 26th, 2009 at 01:24

感谢豪哥的cake教程,希望再多出一些,越详细越好哦~~

chenjing十一月 13th, 2009 at 07:26

多谢,豪哥的视频资料

星光十一月 17th, 2009 at 19:48

朱老师的课程太棒了

zhangqi十二月 19th, 2009 at 20:39

感谢豪哥的cake教程,希望你能把phpnow于cakephp1.2结合在改详细一点。对于我们初学者来说。还是有些不能理解。再多出一些,越详细越好哦…

memory三月 7th, 2010 at 23:57

能问下朱老师多大了吗?

aaron三月 15th, 2010 at 01:24

这个上传头像 功能,按照你的这个的话。怎么浏览呀。我是初学者。感觉怎么少了很多哦。

丕子八月 10th, 2010 at 10:27

没有个demo么?

admin八月 10th, 2010 at 10:53

很久不做CAKE了,抱歉

Leave a comment

Your comment