猫咪兔 2012-03-11 17:06:26 7625次浏览 11条回复 1 0 0

在Yii中剪辑头像,困扰了很长时间,终于在官网找到了CropZoom。很好用的插件。 插件下载: 需求:Yii 1.1或更高版本 用法:下载cropzoom包,解压后放到extensions目录下。 在view层中和其它widget用法一样: 下面是个例子:视图的名字叫cropzoom.php

<?php
$this->widget('ext.cropzoom.JCropZoom', array(
    'id' => 'cropZoom1',
    //'containerId'=>'crop_container1',
    'callbackUrl'=>$this->createUrl('controllerID/handleCropZoom'),
    'onServerHandled' => 'js:function(response){
        alert(response);
        //if server end give you an json string
        //use the $.parseJSON() to decode it !
    }',
    'image'=>array(
        //note if you didn't give an image source url will use the sample image for testing !                                    //'source'=>'http://a1.att.hudong.com/11/11/01300000334334123287112954864.jpg'
    )
));
?>
<div>
     <a href="javascript:;" onclick="cropZoom1.restore();">restore image</a>
</div>
<div id="examples">
    <div id="crop_container1"></div>
</div>

在控制器中,加入以下的两个方法。

/**
*这个方法是控制器中用来render()到视图的方法,可以根据个人需求去更改它
*/
public function actionCropZoom(){
    if(Yii::app()->request->getIsAjaxRequest()){
        //print_r($_POST);
        //die;
        Yii::import('ext.cropzoom.JCropZoom');
        $saveToFilePath = Yii::getPathOfAlias('webroot.assets').DIRECTORY_SEPARATOR .'cropZoomTest';
        JCropZoom::getHandler()->process($saveToFilePath,true)->process($saveToFilePath.'.jpeg');
        die($saveToFilePath);
    }

    $this->render('cropzoom');
}

/**
* 这个方法是用来处理剪辑图片的方法
*/
public function actionHandleCropZoom(){
    if(Yii::app()->request->getIsAjaxRequest()){
        //print_r($_POST);
        //die;
        Yii::import('ext.cropzoom.JCropZoom');
        $saveToFilePath = Yii::getPathOfAlias('webroot.assets').DIRECTORY_SEPARATOR .'cropZoomTest';
        JCropZoom::getHandler()->process($saveToFilePath,true)->process($saveToFilePath.'.jpeg');
        die('cropped file path : '.$saveToFilePath);
    }
}

有必要的话可以对源码进行更改,来满足个人需求。 希望能够帮到大家~!

  • 回复于 2012-04-26 11:31 举报

    视图中返回的response只是url:cropped file path : /localhost/myapp/assets/cropZoomTest是这样的吗?我该如何保存为用户的头像啊?求帮助。。。

  • 回复于 2012-04-26 14:28 举报

    下面那个函数就是用来处理剪切的图片的。给你个例子

    public function actionHandleCropZoom(){
        //print_r($_POST);
        //die;
        $model = new User();
        $sessionUid = $model->getUid();
        if(Yii::app()->request->getIsAjaxRequest()){			
            $cropImg = 'cropImg';//剪切图片要保存成的名称
            $path = Yii::getPathOfAlias('webroot').'/temp/';	//剪切图片存储路径				
            Yii::import('ext.cropzoom.JCropZoom');//使用JCropZoom扩展
            $saveToFilePath = $path.$cropImg;
            //获取扩展名
            $extName = $this->getExtName($sessionUid);
            JCropZoom::getHandler()->process($saveToFilePath,true)->process($saveToFilePath.$extName);
        }
    }
    

    其中 getExtName($sessionUid); 是获取上传图片的扩展名。根据自己的实际情况,写个函数就行了

  • 回复于 2012-04-27 13:57 举报

    不好用 Chrome 有bug

  • 回复于 2012-05-08 17:48 举报

    mark 或许以后用得着

  • 回复于 2012-05-09 09:57 举报

    学习了,看下怎么用

  • 回复于 2012-05-22 09:54 举报

    Mark,马上需要用到了!收藏之

  • 回复于 2012-05-29 09:46 举报

    有加强啊,赞一下~

  • 回复于 2012-06-04 16:24 举报

    谢谢 留着备用啦

  • 回复于 2012-06-11 14:14 举报

    好东西,一定要好好学习一下。

  • 回复于 2012-06-13 11:16 举报

    顶啊顶!收藏了。可能会用到。

  • 回复于 2015-09-01 09:20 举报

    现在正好在做这个上传头像,没太多思路~~~

您需要登录后才可以回复。登录 | 立即注册