how to image upload and crop and save in yii

Bipin Chandra Tripathi picture Bipin Chandra Tripathi · May 14, 2012 · Viewed 8.3k times · Source

i am trying to upload image in yii and then crop the uploaded image and then save it. what i have achieved till now is i can upload in on form submit but i dont understand to crop it or use jcrop for that can anyone send some usefull tutorials for that what i am doing in my controller is

public function actionCreate()
{
    $model=new Userprofile;
    if(isset($_POST['Userprofile']))
    {
    $model->attributes=$_POST['Userprofile'];
    $model->profilePic=CUploadedFile::getInstance($model,'profilePic');
    $ext = $model->profilePic->getExtensionName();
    $imagename=date('dmy').time().rand();
    $pp=$model->profilePic;
    $model->profilePic=$imagename.'.'.$ext;
    if($model->save())
        mkdir('profilepics/'.$model->id,0777);
        $pp->saveAs('profilepics/'.$model->id.'/'.$imagename.'.'.$ext);
        $this->redirect(array('view','id'=>$model->id));
    }
    $this->render('create',array('model'=>$model,));
}

Answer

sl4mmer picture sl4mmer · May 15, 2012

You can use jquery plugin imageAreaSelect to get coordinates for cropping at client-side. Here is live example http://odyniec.net/projects/imgareaselect/.

Then pass coordinates to model`s beforeSave and crop it using Imagick::cropImage() (instead of Imagick you can use GD or any other library you want)