From ab6d9045406e5d61ea9238cb1e39079c37457cd4 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 3 Feb 2016 15:40:26 +0200 Subject: [PATCH] add Vitaliy's widgets --- common/config/bootstrap.php | 1 + common/config/main.php | 4 ++++ common/modules/file/controller/DefaultController.php | 222 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ common/modules/file/controllers/UploaderController.php | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/widgets/views/image_sizer.php | 4 ++-- console/migrations/m160203_082111_jobs.php | 0 console/migrations/m160203_133308_add_field_specialization.php | 16 ++++++++++++++++ frontend/controllers/FileController.php | 219 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- frontend/views/accounts/_form.php | 2 +- 9 files changed, 262 insertions(+), 444 deletions(-) delete mode 100755 common/modules/file/controller/DefaultController.php create mode 100755 common/modules/file/controllers/UploaderController.php mode change 100644 => 100755 console/migrations/m160203_082111_jobs.php create mode 100644 console/migrations/m160203_133308_add_field_specialization.php delete mode 100755 frontend/controllers/FileController.php diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index 2533ec2..d4a7ece 100755 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -4,3 +4,4 @@ Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend'); Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend'); Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console'); Yii::setAlias('saveImageDir', '@frontend/web/images/upload/'); +Yii::setAlias('storage', dirname(dirname(__DIR__)) . '/storage'); diff --git a/common/config/main.php b/common/config/main.php index 1b7c720..c3b3ecb 100755 --- a/common/config/main.php +++ b/common/config/main.php @@ -34,6 +34,10 @@ return [ 'blog' => [ 'class' => 'common\modules\blog\Module', ], + 'file' => [ + 'class' => 'common\modules\file\Module', + ], + ], 'bootstrap' => [ 'options' diff --git a/common/modules/file/controller/DefaultController.php b/common/modules/file/controller/DefaultController.php deleted file mode 100755 index bb03f7a..0000000 --- a/common/modules/file/controller/DefaultController.php +++ /dev/null @@ -1,222 +0,0 @@ -$w){ - return true; - }else if($height >$h) { - return true; - } - return false; - } - - public function actionIndex(){ - die('hello'); - } - - private function resizeImg($w, $h, $imageAlias,$imageAliasSave){ - $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); - - $size = $img->getSize(); - - $width = $size->getWidth(); - $height = $size->getHeight(); - - $e_width = $w/$h; - $e_height = $h/$w; - - $e1_width = $width/$height; - $e1_height = $height/$width; - - - if($this->isBigger($width,$height,$w,$h)){ - if($e_width<$e1_width){ - - $new_width = $width*($e_width/$e1_width); - - $y = 0; - $x = $width/ 2-($new_width/2); - $width = $new_width; - - }else { - - $new_height = $height*($e_height/$e1_height); - $x = 0; - $y = $height/2-($new_height/2); - $height = $new_height; - } - - - } else { - $img->save($imageAliasSave, array('flatten' => false)); - return true; - } - - - Image::crop($imageAlias, $width, $height,[$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(){ - - $request = Yii::$app->request->post(); - - if($request){ - if ($request['old_img']) { - $this->deleteImages($request['old_img']); - } - if(isset($request['action']) && $request['action']=='save'){ - $object = str_replace('-', '\\',$request['model']); - $model = new $object; - $model = $model->findOne($request['id']); - $model->$request['field'] = $request['new_url']; -// print_r($model->validate()); -// print_r($model->getErrors()); -// die(var_dump($model->save())); - $model->save(); - } - } - - } - - - public function actionDownloadPhoto() - { - - $model = new ImageSizerForm(); - - $request = Yii::$app->request->post(); - if ($request) { - - $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($request['width'] && $request['height']){ - - $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension); - - $imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension; - - $this->resizeImg($request['width'], $request['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]); - } - - - } - } - - - public function getex($filename) { - return end(explode(".", $filename)); - } - - - public function actionImagesUpload(){ - if($_FILES['upload']) - { - if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) ) - { - $message = "Вы не выбрали файл"; - } - else if ($_FILES['upload']["size"] == 0 OR $_FILES['upload']["size"] > 2050000) - { - $message = "Размер файла не соответствует нормам"; - } - else if (($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png") AND ($_FILES['upload']['type'] != 'image/gif')) - { - $message = "Допускается загрузка только картинок JPG и PNG."; - } - else if (!is_uploaded_file($_FILES['upload']["tmp_name"])) - { - $message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз."; - } - else{ - $name =rand(1, 1000).md5($_FILES['upload']['name']).'.'.$this->getex($_FILES['upload']['name']); - move_uploaded_file($_FILES['upload']['tmp_name'], "../../storage/images/".$name); - $full_path = '/storage/images/'.$name; - $message = "Файл ".$_FILES['upload']['name']." загружен"; - $size=@getimagesize('images/'.$name); - - } - $callback = $_REQUEST['CKEditorFuncNum']; - echo ''; - } - } - - -} \ No newline at end of file diff --git a/common/modules/file/controllers/UploaderController.php b/common/modules/file/controllers/UploaderController.php new file mode 100755 index 0000000..502e086 --- /dev/null +++ b/common/modules/file/controllers/UploaderController.php @@ -0,0 +1,238 @@ +$w){ + return true; + }else if($height >$h) { + return true; + } + return false; + } + + + private function getUserPath(){ + if(isset(Yii::$app->user->id)){ + return 'user_'.Yii::$app->user->id; + }else { + return 'guest'; + } + } + + private function resizeImg($w, $h, $imageAlias,$imageAliasSave){ + $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); + + $size = $img->getSize(); + + $width = $size->getWidth(); + $height = $size->getHeight(); + + $e_width = $w/$h; + $e_height = $h/$w; + + $e1_width = $width/$height; + $e1_height = $height/$width; + + + if($this->isBigger($width,$height,$w,$h)){ + if($e_width<$e1_width){ + + $new_width = $width*($e_width/$e1_width); + + $y = 0; + $x = $width/ 2-($new_width/2); + $width = $new_width; + + }else { + + $new_height = $height*($e_height/$e1_height); + $x = 0; + $y = $height/2-($new_height/2); + $height = $new_height; + } + + + } else { + $img->save($imageAliasSave, array('flatten' => false)); + return true; + } + + + Image::crop($imageAlias, $width, $height,[$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(){ + + $request = Yii::$app->request->post(); + + if($request){ + if ($request['old_img']) { + $this->deleteImages($request['old_img']); + } + if(isset($request['action']) && $request['action']=='save'){ + $object = str_replace('-', '\\',$request['model']); + $model = new $object; + $model = $model->findOne($request['id']); + $model->$request['field'] = $request['new_url']; +// print_r($model->validate()); +// print_r($model->getErrors()); +// die(var_dump($model->save())); + $model->save(); + } + } + + } + + + public function actionDownloadPhoto() + { + + $model = new ImageSizerForm(); + + $request = Yii::$app->request->post(); + if ($request) { + + $model->file = UploadedFile::getInstance($model, 'file'); + + $md5_file = md5_file($model->file->tempName).rand(1, 1000); + + $imgDir = Yii::getAlias('@storage/'.$this->getUserPath().'/'.$md5_file.'/'); + + $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } + + $model->file->saveAs($imageOrigAlias); + + if($request['width'] && $request['height']){ + + $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension); + + $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension; + + $this->resizeImg($request['width'], $request['height'], $imageOrigAlias,$imageAlias); + + } else { + + $imageLink = '/storage/'.$this->getUserPath().'/'.$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]); + } + + + } + } + + + public function getex($filename) { + return end(explode(".", $filename)); + } + + + public function actionImagesUpload(){ + + if($_FILES['upload']) + { + if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) ) + { + $message = "Вы не выбрали файл"; + } + else if ($_FILES['upload']["size"] == 0 OR $_FILES['upload']["size"] > 2050000) + { + $message = "Размер файла не соответствует нормам"; + } + else if (($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png") AND ($_FILES['upload']['type'] != 'image/gif')) + { + $message = "Допускается загрузка только картинок JPG и PNG."; + } + else if (!is_uploaded_file($_FILES['upload']["tmp_name"])) + { + $message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз."; + } + else{ + $name =$_FILES['upload']['name'].'.'.$this->getex($_FILES['upload']['name']); + + $path = "../../storage/".$this->getUserPath()."/images/"; + if(!is_dir($path)) { + mkdir($path, 0755, true); + } + + + + move_uploaded_file($_FILES['upload']['tmp_name'], $path.$name); + + $full_path = '/storage/'.$this->getUserPath().'/images/'.$name; + + $message = "Файл ".$_FILES['upload']['name']." загружен"; + + + } + $callback = $_REQUEST['CKEditorFuncNum']; + echo ''; + } + } + + +} \ No newline at end of file diff --git a/common/widgets/views/image_sizer.php b/common/widgets/views/image_sizer.php index a6f08c4..3579881 100755 --- a/common/widgets/views/image_sizer.php +++ b/common/widgets/views/image_sizer.php @@ -16,7 +16,7 @@ $id = $model::tableName().'_id'; - $field, 'data-url'=>"/file/download-photo" ]);?> + $field, 'data-url'=>Yii::$app->getUrlManager()->createUrl('file/uploader/download-photo')]);?> "{$field}_picture_link"]) ?> @@ -83,7 +83,7 @@ $id = $model::tableName().'_id'; - $field, 'data-url'=>"/file/download-photo", 'multiple'=> 'multiple' ]);?> + $field, 'data-url'=>Yii::$app->getUrlManager()->createUrl('file/uploader/download-photo'), 'multiple'=> 'multiple' ]);?> "{$field}_picture_link"]) ?> diff --git a/console/migrations/m160203_082111_jobs.php b/console/migrations/m160203_082111_jobs.php old mode 100644 new mode 100755 index 1f7ba8e..1f7ba8e --- a/console/migrations/m160203_082111_jobs.php +++ b/console/migrations/m160203_082111_jobs.php diff --git a/console/migrations/m160203_133308_add_field_specialization.php b/console/migrations/m160203_133308_add_field_specialization.php new file mode 100644 index 0000000..68c6fb7 --- /dev/null +++ b/console/migrations/m160203_133308_add_field_specialization.php @@ -0,0 +1,16 @@ +addColumn('specialization', 'image', $this->string(255)); + } + + public function down() + { + $this->dropColumn('option', 'image'); + } +} diff --git a/frontend/controllers/FileController.php b/frontend/controllers/FileController.php deleted file mode 100755 index c7c08b2..0000000 --- a/frontend/controllers/FileController.php +++ /dev/null @@ -1,219 +0,0 @@ -$w){ - return true; - }else if($height >$h) { - return true; - } - return false; - } - - - private function resizeImg($w, $h, $imageAlias,$imageAliasSave){ - $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); - - $size = $img->getSize(); - - $width = $size->getWidth(); - $height = $size->getHeight(); - - $e_width = $w/$h; - $e_height = $h/$w; - - $e1_width = $width/$height; - $e1_height = $height/$width; - - - if($this->isBigger($width,$height,$w,$h)){ - if($e_width<$e1_width){ - - $new_width = $width*($e_width/$e1_width); - - $y = 0; - $x = $width/ 2-($new_width/2); - $width = $new_width; - - }else { - - $new_height = $height*($e_height/$e1_height); - $x = 0; - $y = $height/2-($new_height/2); - $height = $new_height; - } - - - } else { - $img->save($imageAliasSave, array('flatten' => false)); - return true; - } - - - Image::crop($imageAlias, $width, $height,[$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(){ - - $request = Yii::$app->request->post(); - - if($request){ - if ($request['old_img']) { - $this->deleteImages($request['old_img']); - } - if(isset($request['action']) && $request['action']=='save'){ - $object = str_replace('-', '\\',$request['model']); - $model = new $object; - $model = $model->findOne($request['id']); - $model->$request['field'] = $request['new_url']; -// print_r($model->validate()); -// print_r($model->getErrors()); -// die(var_dump($model->save())); - $model->save(); - } - } - - } - - - public function actionDownloadPhoto() - { - - $model = new ImageSizerForm(); - - $request = Yii::$app->request->post(); - if ($request) { - - $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($request['width'] && $request['height']){ - - $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension); - - $imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension; - - $this->resizeImg($request['width'], $request['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]); - } - - - } - } - - - public function getex($filename) { - return end(explode(".", $filename)); - } - - - public function actionImagesUpload(){ - if($_FILES['upload']) - { - if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) ) - { - $message = "Вы не выбрали файл"; - } - else if ($_FILES['upload']["size"] == 0 OR $_FILES['upload']["size"] > 2050000) - { - $message = "Размер файла не соответствует нормам"; - } - else if (($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png") AND ($_FILES['upload']['type'] != 'image/gif')) - { - $message = "Допускается загрузка только картинок JPG и PNG."; - } - else if (!is_uploaded_file($_FILES['upload']["tmp_name"])) - { - $message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз."; - } - else{ - $name =rand(1, 1000).md5($_FILES['upload']['name']).'.'.$this->getex($_FILES['upload']['name']); - move_uploaded_file($_FILES['upload']['tmp_name'], "../../storage/images/".$name); - $full_path = '/storage/images/'.$name; - $message = "Файл ".$_FILES['upload']['name']." загружен"; - $size=@getimagesize('images/'.$name); - - } - $callback = $_REQUEST['CKEditorFuncNum']; - echo ''; - } - } - - -} \ No newline at end of file diff --git a/frontend/views/accounts/_form.php b/frontend/views/accounts/_form.php index 62cecc8..2ee6892 100755 --- a/frontend/views/accounts/_form.php +++ b/frontend/views/accounts/_form.php @@ -46,7 +46,7 @@ use mihaildev\elfinder\ElFinder; 'editorOptions' => ElFinder::ckeditorOptions('elfinder',[ 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать 'inline' => false, //по умолчанию false]), - 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('file/images-upload') + 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('file/uploader/images-upload') ] ) ]); ?> -- libgit2 0.21.4