From 143c55a9e4858fe8a639e66353b72110b33a2ae5 Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 9 Nov 2016 14:25:19 +0200 Subject: [PATCH] Namespaces --- FileUploadAsset.php | 73 +++++++++++++++++++++++++++++++++++-------------------------------------- Module.php | 26 ++++++++++++-------------- composer.json | 4 +++- controllers/UploaderController.php | 484 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- models/ImageSizerForm.php | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- widgets/ImageUploader.php | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------- 6 files changed, 420 insertions(+), 384 deletions(-) diff --git a/FileUploadAsset.php b/FileUploadAsset.php index 0c9fdcd..728a87c 100755 --- a/FileUploadAsset.php +++ b/FileUploadAsset.php @@ -1,43 +1,40 @@ - * @since 2.0 - */ -class FileUploadAsset extends AssetBundle -{ - - /** - * @inheritdoc + * @link http://www.yiiframework.com/ + * @copyright Copyright (c) 2008 Yii Software LLC + * @license http://www.yiiframework.com/license/ */ - public function init() + + namespace artweb\artbox\file; + + use yii\web\AssetBundle; + + /** + * Asset bundle for the Twitter bootstrap javascript files. + * + * @author Qiang Xue + * @since 2.0 + */ + class FileUploadAsset extends AssetBundle { - parent::init(); - $this->sourcePath = __DIR__.'/assets'; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + $this->sourcePath = __DIR__ . '/assets'; + } + + public $css = [ + 'css/jquery.fileupload.css', + 'css/fileupload/style.css', + ]; + + public $js = [ + 'js/vendor/jquery.ui.widget.js', + 'js/jquery.iframe-transport.js', + 'js/jquery.fileupload.js', + ]; } - - public $css = [ - 'css/jquery.fileupload.css', - 'css/fileupload/style.css' - ]; - - public $js = [ - 'js/vendor/jquery.ui.widget.js', - 'js/jquery.iframe-transport.js', - 'js/jquery.fileupload.js' - ]; -} diff --git a/Module.php b/Module.php index 1855e9b..b4a2e61 100755 --- a/Module.php +++ b/Module.php @@ -1,15 +1,13 @@ =7.0", "yiisoft/yii2": "*", - "developeruz/yii2-db-rbac": "*" + "developeruz/yii2-db-rbac": "*", + "artweb/artbox": "dev-master", + "artweb/artbox/language": "dev-master" }, "autoload": { "psr-4": { diff --git a/controllers/UploaderController.php b/controllers/UploaderController.php index 0ef7c23..6e7144c 100755 --- a/controllers/UploaderController.php +++ b/controllers/UploaderController.php @@ -1,259 +1,267 @@ $w){ - return true; - }else if($height >$h) { - return true; + + public $enableCsrfValidation = false; + + public function isBigger($width, $height, $w, $h) + { + if ($width > $w) { + return true; + } else if ($height > $h) { + return true; + } + return false; } - return false; - } - - - private function getUserPath(){ - if(isset(Yii::$app->user->id)){ - return 'user_'.Yii::$app->user->id; - }else { - return 'guest'; + + 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($e_width<$e1_width){ - - $new_width = $width*($e_width/$e1_width); - + + 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 ($e_width < $e1_width) { + + $new_width = $width * ( $e_width / $e1_width ); + $y = 0; - $x = $width/ 2-($new_width/2); + $x = $width / 2 - ( $new_width / 2 ); $width = $new_width; - - }else { - - $new_height = $height*($e_height/$e1_height); + + } else { + + $new_height = $height * ( $e_height / $e1_height ); $x = 0; - $y = $height/2-($new_height/2); + $y = $height / 2 - ( $new_height / 2 ); $height = $new_height; } - - - - - 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].'/'.$rootDir[3].'/'; - - $allFiles = scandir($row); - - $allFiles = array_slice($allFiles, 2); - - foreach($allFiles as $oldFile){ - - unlink($row.$oldFile); - - } - + + 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, [ 'flatten' => false ]); + } - } - - public function actionDeleteImage(){ - - $this->enableCsrfValidation = false; - - $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']; - $model->save(); + + 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 ] . '/' . $rootDir[ 3 ] . '/'; + + $allFiles = scandir($row); + + $allFiles = array_slice($allFiles, 2); + + foreach ($allFiles as $oldFile) { + + unlink($row . $oldFile); + + } + } } - - } - - - public function actionDownloadPhoto() - { - - $model = new ImageSizerForm(); - - $request = Yii::$app->request->post(); - - if ($request) { - - $model->multi = isset($request['multi'])? 1 : 0; - - $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); + + public function actionDeleteImage() + { + + $this->enableCsrfValidation = false; + + $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' ]; + $model->save(); + } } - - $model->file->saveAs($imageOrigAlias); - - - if(isset($request['size'] )){ - - $request['size'] = ArrayHelper::toArray(json_decode($request['size'])); - - foreach($request['size'] as $size){ - if($size['width'] && $size['height']){ - - $imageAlias = Yii::getAlias($imgDir.$size['width'].'x'.$size['height'].'.'.$model->file->extension); - - $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.$size['width'].'x'.$size['height'].'.'.$model->file->extension; - - $this->resizeImg($size['width'], $size['height'], $imageOrigAlias,$imageAlias); - + + } + + public function actionDownloadPhoto() + { + + $model = new ImageSizerForm(); + + $request = Yii::$app->request->post(); + + if ($request) { + + $model->multi = isset( $request[ 'multi' ] ) ? 1 : 0; + + $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 (isset( $request[ 'size' ] )) { + + $request[ 'size' ] = ArrayHelper::toArray(json_decode($request[ 'size' ])); + + foreach ($request[ 'size' ] as $size) { + if ($size[ 'width' ] && $size[ 'height' ]) { + + $imageAlias = Yii::getAlias( + $imgDir . $size[ 'width' ] . 'x' . $size[ 'height' ] . '.' . $model->file->extension + ); + + $imageLink = '/storage/' . $this->getUserPath( + ) . '/' . $md5_file . '/' . $size[ 'width' ] . 'x' . $size[ 'height' ] . '.' . $model->file->extension; + + $this->resizeImg($size[ 'width' ], $size[ 'height' ], $imageOrigAlias, $imageAlias); + + } } + + } else { + + $imageLink = '/storage/' . $this->getUserPath( + ) . '/' . $md5_file . '/' . 'original' . '.' . $model->file->extension; + } - - } else { - - $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.'original'.'.'.$model->file->extension; - - } - - - if($model->multi){ - $view = $this->renderPartial('/_gallery_item', [ - 'item' => ['image'=>$imageLink], - 'field'=>$request['field'] - ]); - return json_encode(['link'=>$imageLink, - 'view' =>$view, - - ]); - - - } else { - $view = $this->renderPartial('/_one_item', [ - 'item' => ['image'=>$imageLink], - 'field'=>$request['field'] - ]); - return json_encode(['link'=>$imageLink, - 'view' =>$view, - ]); + + if ($model->multi) { + $view = $this->renderPartial( + '/_gallery_item', + [ + 'item' => [ 'image' => $imageLink ], + 'field' => $request[ 'field' ], + ] + ); + return json_encode( + [ + 'link' => $imageLink, + 'view' => $view, + + ] + ); + + } else { + $view = $this->renderPartial( + '/_one_item', + [ + 'item' => [ 'image' => $imageLink ], + 'field' => $request[ 'field' ], + ] + ); + return json_encode( + [ + 'link' => $imageLink, + 'view' => $view, + ] + ); + } + } - - } - } - - - public function getex($filename) { - $array = explode(".", $filename); - return array_pop($array); - } - - - public function actionImagesUpload(){ - - if($_FILES['upload']) + + public function getex($filename) { - 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{ - $filename = $_FILES['upload']['name']; - $name =$_FILES['upload']['name'].'.'.$this->getex($filename); - - $path = "../../storage/".$this->getUserPath()."/images/"; - if(!is_dir($path)) { - mkdir($path, 0755, true); + $array = explode(".", $filename); + return array_pop($array); + } + + 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 { + $filename = $_FILES[ 'upload' ][ 'name' ]; + $name = $_FILES[ 'upload' ][ 'name' ] . '.' . $this->getex($filename); + + $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' ] . " загружен"; + } - - - - move_uploaded_file($_FILES['upload']['tmp_name'], $path.$name); - - $full_path = '/storage/'.$this->getUserPath().'/images/'.$name; - - $message = "Файл ".$_FILES['upload']['name']." загружен"; - - + $callback = $_REQUEST[ 'CKEditorFuncNum' ]; + echo ''; } - $callback = $_REQUEST['CKEditorFuncNum']; - echo ''; } - } - - -} \ No newline at end of file + + } \ No newline at end of file diff --git a/models/ImageSizerForm.php b/models/ImageSizerForm.php index 99bf71e..9a350fe 100755 --- a/models/ImageSizerForm.php +++ b/models/ImageSizerForm.php @@ -1,38 +1,66 @@ 255], - [['model', 'form',], 'string'], - [['file','img','price_list'], 'file'], - ]; - } -} \ No newline at end of file + /** + * @var UploadedFile file attribute + */ + public $file; + public $width; + public $height; + public $field; + public $model; + public $form; + public $multi; + public $old_img; + public $img; + public $price_list; + + /** + * @return array the validation rules. + */ + public function rules() + { + return [ + [ + [ + 'width', + 'height', + 'multi', + ], + 'integer', + ], + [ + [ + 'field', + 'multi', + 'old_img', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'model', + 'form', + ], + 'string', + ], + [ + [ + 'file', + 'img', + 'price_list', + ], + 'file', + ], + ]; + } + } \ No newline at end of file diff --git a/widgets/ImageUploader.php b/widgets/ImageUploader.php index c04f7a9..de52d10 100755 --- a/widgets/ImageUploader.php +++ b/widgets/ImageUploader.php @@ -1,63 +1,66 @@ render('image_sizer', - [ - 'model'=>$this->model, - 'size' => $this->size, - 'field' => $this->field, - 'height' => $this->height, - 'width' => $this->width, - 'multi' => $this->multi, - 'name' => $this->name, - 'remover' => $this->remover - ]); - - } - - public function getGallery(){ - if($this->gallery){ - $array = explode(",", $this->gallery); - if(count($array) > 1){ - array_pop($array); + public $height = 0; + public $width = 0; + public $field; + public $file; + public $model; + public $multi = false; + public $gallery; + public $size; + public $name = 'Add file...'; + public $remover = 0; + + public function init() + { + + parent::init(); + + } + + public function run() + { + + return $this->render( + 'image_sizer', + [ + 'model' => $this->model, + 'size' => $this->size, + 'field' => $this->field, + 'height' => $this->height, + 'width' => $this->width, + 'multi' => $this->multi, + 'name' => $this->name, + 'remover' => $this->remover, + ] + ); + + } + + public function getGallery() + { + if ($this->gallery) { + $array = explode(",", $this->gallery); + if (count($array) > 1) { + array_pop($array); + } + return $array; + } else { + return []; } - return $array; - } else { - return array(); + } - - } - -} \ No newline at end of file + + } \ No newline at end of file -- libgit2 0.21.4