/** * Upload and apply new avatar * @return bool * @throws CException */ public static function uploadPhoto($userId) { // folder for uploaded files $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['temp']; $photosFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . Yii::app()->params['folders']['userPhotos']; $photosFolder .= $userId . DIRECTORY_SEPARATOR; if (!is_writable($tempFolder)) { throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder); } // Upload to temp folder $uploader = new FileUploader(Yii::app()->params['uploader']['allowedFileExtensions'], Yii::app()->params['uploader']['sizeLimit']); $result = $uploader->handleUpload($tempFolder); if (!isset($result['error'])) { // Move file to target folder and make thumbs $imageHandler = new CImageHandler(); $imageHandler->load($tempFolder . $result['filename']); $imageHandler->save($photosFolder . $result['filename']); $imageHandler->cropAndScaleFromCenter(300, 300); $imageHandler->save($photosFolder . self::THUMB_PREFIX_SMALL . $result['filename']); $model = new Photos(); $model->owner_id = $userId; $model->file_name = $result['filename']; $model->save(); return array('originalSrc' => $model->getOriginal(), 'thumbSrc' => $model->getSmallThumbnail(), 'photoId' => $model->id); } else { throw new CException($result['error']); } }