modules/api/v2/controllers/ProductImageController.php
/**
     * Updates an existing ProductImage model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
Updates an existing ProductImage model.
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
Updates an existing ProductImage model . If update is successful the browser will be redirected to the ' view ' page . @param integer $id @return mixed @throws NotFoundHttpException if the model cannot be found
public function actionUpdate()
    {
        $model = new ProductImage();
        $postData = Yii::$app->request->post();
        $productImage['ProductImage'] = $postData;
        $images = UploadedFile::getInstancesByName('images');
        $model->images = $images;
        $model->scenario = 'update_api';
        $arrayImage = [];
        if ($model->load($productImage) && $model->validate()) {
            if (!empty($images)) {
                foreach ($images as $img) {
                    $modelImage = new ProductImage();
                    $uploadDirPath = Yii::getAlias('@productImageRelativePath');
                    $uploadThumbDirPath = Yii::getAlias('@productImageThumbRelativePath');
                    $thumbImagePath = '';
                    if (!is_dir($uploadDirPath)) {
                        mkdir($uploadDirPath, 0777);
                    }
                    if (!is_dir($uploadThumbDirPath)) {
                        mkdir($uploadThumbDirPath, 0777);
                    }
                    $fileName = time() . rand(99999, 88888) . '.' . $img->extension;
                    $img->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']]);
                    $modelImage->product_id = $productImage['ProductImage']['product_id'];
                    $modelImage->name = $fileName;
                    $modelImage->save(false);
                    $arrayImage[] = $modelImage;
                }
            }
            if (!empty($arrayImage)) {
                $thumbImagePath = Yii::getAlias('@productImageThumbAbsolutePath');
                $thumbImagePathRelative = Yii::getAlias('@productImageThumbRelativePath');
                foreach ($arrayImage as $images) {
                    if (!empty($images->name) && file_exists($thumbImagePathRelative . "/" . $images->name)) {
                        $images->name = Yii::$app->request->getHostInfo() . $thumbImagePath . '/' . $images->name;
                    } else {
                        $images->name = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
                    }
                }
                $model = $arrayImage;
            }
        }
        return $model;
    }
public function actionUpdate ( ) { $ model = new ProductImage ( ) ; $ postData = Yii :: $ app -> request -> post ( ) ; $ productImage [ 'ProductImage' ] = $ postData ; $ images = UploadedFile :: getInstancesByName ( 'images' ) ; $ model -> images = $ images ; $ model -> scenario = 'update_api' ; $ arrayImage = [ ] ; if ( $ model -> load ( $ productImage ) && $ model -> validate ( ) ) { if ( ! empty ( $ images ) ) { foreach ( $ images as $ img ) { $ modelImage = new ProductImage ( ) ; $ uploadDirPath = Yii :: getAlias ( '@productImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@productImageThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } $ fileName = time ( ) . rand ( 99999 , 88888 ) . '.' . $ img -> extension ; $ img -> 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' ] ] ) ; $ modelImage -> product_id = $ productImage [ 'ProductImage' ] [ 'product_id' ] ; $ modelImage -> name = $ fileName ; $ modelImage -> save ( false ) ; $ arrayImage [ ] = $ modelImage ; } } if ( ! empty ( $ arrayImage ) ) { $ thumbImagePath = Yii :: getAlias ( '@productImageThumbAbsolutePath' ) ; $ thumbImagePathRelative = Yii :: getAlias ( '@productImageThumbRelativePath' ) ; foreach ( $ arrayImage as $ images ) { if ( ! empty ( $ images -> name ) && file_exists ( $ thumbImagePathRelative . " / " . $ images -> name ) ) { $ images -> name = Yii :: $ app -> request -> getHostInfo ( ) . $ thumbImagePath . '/' . $ images -> name ; } else { $ images -> name = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; } } $ model = $ arrayImage ; } } return $ model ; }
Updates an existing ProductImage model.
Updates an existing ProductImage model .
// Create product upload directory if not exist // Create product thumb upload directory if not exist // Upload product picture // Create thumb of product picture // Insert product picture name into database