open(Yii::getAlias($imageAlias)); $size = $img->getSize(); $width = $size->getWidth(); $height = $size->getHeight(); if($width > $height) { $y = 0; $x = ($width - $height) / 2; $smallestSide = $height; } else { $x = 0; $y = ($height - $width) / 2; $smallestSide = $width; } Image::crop($imageAlias, $smallestSide, $smallestSide,[$x,$y]) ->save(Yii::getAlias($imageAliasSave), ['quality' => 100]); $imagine = new Imagine(); $imagine->open($imageAliasSave) ->resize(new Box($w, $h)) ->save($imageAliasSave, array('flatten' => false)); } private function deleteImages($old_img){ if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){ $rootDir = explode("/", $old_img); $row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/'; $allFiles = scandir($row); $allFiles = array_slice($allFiles, 2); foreach($allFiles as $oldFile){ unlink($row.$oldFile); } } } public function actionDeleteImage(){ $old_img = Yii::$app->request->post('old_img'); if ($old_img) { $this->deleteImages($old_img); } } public function actionDownloadPhoto() { $model = new ImageSizerForm(); if ($model->load(Yii::$app->request->post())) { $this->deleteImages($model->old_img); $model->file = UploadedFile::getInstance($model, 'file'); $md5_file = md5_file($model->file->tempName); $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension); if(!is_dir($imgDir)) { mkdir($imgDir, 0755, true); } $model->file->saveAs($imageOrigAlias); if($model->width && $model->height){ $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension; $this->resizeImg($model->width, $model->height, $imageOrigAlias,$imageAlias); } else { $imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension; } if($model->multi){ $view = $this->renderPartial('@app/components/views/_gallery_item', [ 'item' => ['image'=>$imageLink], ]); return json_encode(['link'=>$imageLink, 'view' =>$view]); } else { return json_encode(['link'=>$imageLink]); } } } /** * @param $mode - int: 0 - fetch from cache, - 1 - put in cache, <2 - delete from cache * @param $data - array * @param $configuration - array * @throws \ErrorException */ protected function parserCacheHandler( $mode, &$data = [], &$configuration = [] ){ switch ( $mode ) { case 0: if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) { $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration')); } else { throw new \ErrorException('Ошибка кеша'); } break; case 1: Yii::$app->getCache()->set('parser_data', json_encode($data), 1800); // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800); break; default: if( Yii::$app->getCache()->exists('parser_data') ) Yii::$app->getCache()->delete('parser_data'); if( Yii::$app->getCache()->exists('parser_configuration') ) Yii::$app->getCache()->delete('parser_configuration'); } } }