modules/api/v1/controllers/UserController.php
/**
     * use to update profile picture
     * @return User
     * @throws NotFoundHttpException
     * @throws \yii\base\Exception
     */
use to update profile picture
@return User
@throws NotFoundHttpException
@throws \yii\base\Exception
use to update profile picture @return User @throws NotFoundHttpException @throws \ yii \ base \ Exception
public function actionUpdateProfilePicture($id)
    {
        $model = User::findOne($id);
        $model->scenario = User::PROFILE_PICTURE_UPDATE;
        if (!$model instanceof User) {
            throw new NotFoundHttpException('User doesn\'t exist.');
        }
        $old_image = $model->profile_picture;
        $postData = \Yii::$app->request->post();
        $data['User'] = $postData;
        $model->profile_picture = UploadedFile::getInstanceByName('profile_picture');
        if ($model->load($data) && $model->validate()) {
            $uploadDirPath = Yii::getAlias('@profilePictureRelativePath');
            $uploadThumbDirPath = Yii::getAlias('@profilePictureThumbRelativePath');
            $thumbImagePath = '';
            if ($model->profile_picture instanceof UploadedFile) {
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (file_exists($uploadDirPath . '/' . $old_image) && !empty($old_image)) {
                    unlink($uploadDirPath . '/' . $old_image);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                if (file_exists($uploadThumbDirPath . '/' . $old_image) && !empty($old_image)) {
                    unlink($uploadThumbDirPath . '/' . $old_image);
                }
                $ext = $model->profile_picture->extension;
                $fileName = pathinfo($model->profile_picture->name, PATHINFO_FILENAME);
                $fileName = $fileName . '_' . time() . '.' . $ext;
                $model->profile_picture->saveAs($uploadDirPath . '/' . $fileName);
                $actualImagePath = $uploadDirPath . '/' . $fileName;
                $thumbImagePath = $uploadThumbDirPath . '/' . $fileName;
                Image::thumbnail($actualImagePath, Yii::$app->params['profile_picture_thumb_width'], Yii::$app->params['profile_picture_thumb_height'])->save($thumbImagePath, ['quality' => Yii::$app->params['profile_picture_thumb_quality']]);
                $model->profile_picture = $fileName;
            }
        }
        if ($model->save()) {
            $uploadThumbDirPath = Yii::getAlias('@profilePictureThumbRelativePath');
            $thumbImagePath = $uploadThumbDirPath . '/' . $model->profile_picture;
            $showProfilePicture = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
            if (!empty($model->profile_picture) && file_exists($thumbImagePath)) {
                $showProfilePicture = Yii::$app->request->getHostInfo() . Yii::getAlias('@profilePictureThumbAbsolutePath') . '/' . $model->profile_picture;
            }
            $model->profile_picture = $showProfilePicture;
        }
        return $model;
    }
public function actionUpdateProfilePicture ( $ id ) { $ model = User :: findOne ( $ id ) ; $ model -> scenario = User :: PROFILE_PICTURE_UPDATE ; if ( ! $ model instanceof User ) { throw new NotFoundHttpException ( 'User doesn\'t exist.' ) ; } $ old_image = $ model -> profile_picture ; $ postData = \ Yii :: $ app -> request -> post ( ) ; $ data [ 'User' ] = $ postData ; $ model -> profile_picture = UploadedFile :: getInstanceByName ( 'profile_picture' ) ; if ( $ model -> load ( $ data ) && $ model -> validate ( ) ) { $ uploadDirPath = Yii :: getAlias ( '@profilePictureRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@profilePictureThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( $ model -> profile_picture instanceof UploadedFile ) { if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( file_exists ( $ uploadDirPath . '/' . $ old_image ) && ! empty ( $ old_image ) ) { unlink ( $ uploadDirPath . '/' . $ old_image ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } if ( file_exists ( $ uploadThumbDirPath . '/' . $ old_image ) && ! empty ( $ old_image ) ) { unlink ( $ uploadThumbDirPath . '/' . $ old_image ) ; } $ ext = $ model -> profile_picture -> extension ; $ fileName = pathinfo ( $ model -> profile_picture -> name , PATHINFO_FILENAME ) ; $ fileName = $ fileName . '_' . time ( ) . '.' . $ ext ; $ model -> profile_picture -> saveAs ( $ uploadDirPath . '/' . $ fileName ) ; $ actualImagePath = $ uploadDirPath . '/' . $ fileName ; $ thumbImagePath = $ uploadThumbDirPath . '/' . $ fileName ; Image :: thumbnail ( $ actualImagePath , Yii :: $ app -> params [ 'profile_picture_thumb_width' ] , Yii :: $ app -> params [ 'profile_picture_thumb_height' ] ) -> save ( $ thumbImagePath , [ 'quality' => Yii :: $ app -> params [ 'profile_picture_thumb_quality' ] ] ) ; $ model -> profile_picture = $ fileName ; } } if ( $ model -> save ( ) ) { $ uploadThumbDirPath = Yii :: getAlias ( '@profilePictureThumbRelativePath' ) ; $ thumbImagePath = $ uploadThumbDirPath . '/' . $ model -> profile_picture ; $ showProfilePicture = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; if ( ! empty ( $ model -> profile_picture ) && file_exists ( $ thumbImagePath ) ) { $ showProfilePicture = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@profilePictureThumbAbsolutePath' ) . '/' . $ model -> profile_picture ; } $ model -> profile_picture = $ showProfilePicture ; } return $ model ; }
use to update profile picture
 @return User
 @throws NotFoundHttpException
 @throws \yii\base\Exception
use to update profile picture @return User @throws NotFoundHttpException @throws \ yii \ base \ Exception
// Profile picture upload // Create profile upload directory if not exist //unlink real image if update // Create profile thumb upload directory if not exist //unlink thumb image if update // Upload profile picture // Create thumb of profile picture // Insert profile picture name into database