Example #1
0
 /**
  * Set image as main for model and model_id.
  * If successful, the browser will be redirected to the 'index' page.
  *
  * @param $id
  * @return mixed
  */
 public function run($id)
 {
     if (Image::setMainImage($id)) {
         \Yii::$app->session->setFlash('success', \Yii::t('app', 'Image successfully set as main'));
     } else {
         \Yii::$app->session->setFlash('error', \Yii::t('app', 'An error occur during set image as main'));
     }
     return $this->controller->redirect(\Yii::$app->request->referrer);
 }
 /**
  * Deletes an existing Image model and image file.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  *
  * @param $id
  * @param null $model_user_id
  * @return Response
  */
 public function run($id, $model_user_id = null)
 {
     if ($this->check_user_id) {
         if ($model_user_id != \Yii::$app->user->id) {
             \Yii::$app->session->setFlash('error', \Yii::t('app', 'You don\'t allow to delete this image'));
             return $this->controller->redirect(\Yii::$app->request->referrer);
         }
     }
     if (Image::deleteImage($id)) {
         \Yii::$app->session->setFlash('success', \Yii::t('app', 'Image successfully deleted'));
     } else {
         \Yii::$app->session->setFlash('error', \Yii::t('app', 'An error occur during image deleting'));
     }
     return $this->controller->redirect(\Yii::$app->request->referrer);
 }
Example #3
0
 /**
  * Make resize on the fly
  *
  * @param $width
  * @param $height
  * @return array|bool|null|ActiveRecord
  */
 public function getResizedImage($width, $height)
 {
     // Get info about owner class
     $class = new \ReflectionClass($this->owner);
     // Check image the thumb image doesn't exist
     $model = Image::find()->where(['model' => $class->getShortName(), 'model_id' => $this->owner->id, 'width' => $width, 'height' => $height])->one();
     if (empty($model)) {
         // Get original model image
         $original_image_model = Image::find()->where(['model' => $class->getShortName(), 'model_id' => $this->owner->id, 'pid' => null])->one();
         $original_image = Yii::getAlias('@frontend/web/upload/') . $original_image_model->dir . '/' . $original_image_model->file_name;
         // Saving crop thumbnail
         $image = yii\imagine\Image::getImagine()->open($original_image);
         $newSizeThumb = new Box($width, $height);
         //            $cropSizeThumb = new Box($width, $height); //frame size of crop
         //            $cropPointThumb = new Point($cropInfo['x'], $cropInfo['y']);
         $sub_dirs = substr(md5(time() + rand()), 0, 2) . '/' . substr(md5(time() + rand()), 0, 2) . '/' . substr(md5(time() + rand()), 0, 2) . '/' . substr(md5(time() + rand()), 0, 2);
         $dir = $this->default_dir . '/' . $sub_dirs;
         $dir_path = Yii::getAlias('@frontend/web/upload/') . $dir;
         if (!is_dir($dir_path)) {
             @mkdir($dir_path, 0777, true);
         }
         $pathinfo = pathinfo($original_image);
         $ext = $pathinfo['extension'];
         $file_name = $this->owner->id . '_' . md5(time() + rand()) . '.' . $ext;
         $pathThumbImage = $dir_path . '/' . $file_name;
         $image_saved = $image->resize($newSizeThumb)->save($pathThumbImage, ['quality' => 100]);
         //            yii\helpers\VarDumper::dump($pathThumbImage, 10, true);
         //            yii\helpers\VarDumper::dump($image_saved, 10, true);
         //            exit();
         // Save new thumb image to DB
         $new_thumb_image = new Image();
         $new_thumb_image->model = $class->getShortName();
         $new_thumb_image->pid = $original_image_model->id;
         $new_thumb_image->model_id = $this->owner->id;
         $new_thumb_image->dir = $dir;
         $new_thumb_image->file_name = $file_name;
         $new_thumb_image->width = $width;
         $new_thumb_image->height = $height;
         if ($image_saved) {
             $new_thumb_image->create_time = date('Y-m-d H:i:s');
             $new_thumb_image->save();
             //                $new_thumb_image_file = $dir_path . '/' . $file_name;
         }
         return !empty($new_thumb_image) ? $new_thumb_image : null;
     } else {
         return false;
     }
 }
Example #4
0
 /**
  * Set main image
  * TODO add transaction saving with true and false return status
  *
  * @param $id
  * @return bool
  */
 public static function setMainImage($id)
 {
     $image = Image::findOne($id);
     // Flush previous main image
     Image::updateAll(['main' => 0], ['model' => $image->model, 'model_id' => $image->model_id]);
     // Set new main image
     $models = Image::find()->where(['model' => $image->model, 'model_id' => $image->model_id])->andWhere(['id' => $image->id])->orWhere(['pid' => $image->id])->all();
     foreach ($models as $model) {
         $model->main = 1;
         $model->save();
     }
     return true;
 }