modules/api/v2/controllers/BrandController.php
/**
     * Creates a new Brand model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new Brand model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new Brand model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new Brand();
        $postData = \Yii::$app->request->post();
        $brandData['Brand'] = $postData;
        $brand_image = UploadedFile::getInstanceByName('image');
        $model->image = $brand_image;
        if ($model->load($brandData) && $model->validate()) {
            if (!empty($brand_image)) {
                $uploadDirPath = Yii::getAlias('@brandImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@brandImageThumbRelativePath');
                $thumbImagePath = '';
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                $ext = $brand_image->extension;
                $fileName = time() . rand(99999, 88888) . '.' . $ext;
                $brand_image->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->image = $fileName;
            }
            if ($model->save()) {
                $brandImage = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
                if (!empty($model->image) && file_exists(Yii::getAlias('@brandImageThumbRelativePath') . '/' . $model->image)) {
                    $brandImage = Yii::$app->request->getHostInfo() . Yii::getAlias('@brandImageThumbAbsolutePath') . '/' . $model->image;
                }
                $model->image = $brandImage;
            }
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new Brand ( ) ; $ postData = \ Yii :: $ app -> request -> post ( ) ; $ brandData [ 'Brand' ] = $ postData ; $ brand_image = UploadedFile :: getInstanceByName ( 'image' ) ; $ model -> image = $ brand_image ; if ( $ model -> load ( $ brandData ) && $ model -> validate ( ) ) { if ( ! empty ( $ brand_image ) ) { $ uploadDirPath = Yii :: getAlias ( '@brandImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@brandImageThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } $ ext = $ brand_image -> extension ; $ fileName = time ( ) . rand ( 99999 , 88888 ) . '.' . $ ext ; $ brand_image -> 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 -> image = $ fileName ; } if ( $ model -> save ( ) ) { $ brandImage = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; if ( ! empty ( $ model -> image ) && file_exists ( Yii :: getAlias ( '@brandImageThumbRelativePath' ) . '/' . $ model -> image ) ) { $ brandImage = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@brandImageThumbAbsolutePath' ) . '/' . $ model -> image ; } $ model -> image = $ brandImage ; } } return $ model ; }
Creates a new Brand model.
Creates a new Brand model .
// Create profile upload directory if not exist // Create profile thumb upload directory if not exist // Upload profile picture // Create thumb of profile picture // Insert profile picture name into database