From 4c977682c7a56f9a2f7bd1da106bdeef90ce9be8 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 7 Oct 2015 14:53:55 +0300 Subject: [PATCH] Importers CRUD --- backend/components/ImageSizer.php | 5 +++++ backend/components/base/BaseController.php | 61 +++++++++++++++++++++++++++++++++++++++---------------------- backend/components/views/_gallery_item.php | 8 ++++++++ backend/components/views/image_sizer.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- backend/models/ImageSizerForm.php | 1 + backend/views/user/_form.php | 2 +- backend/web/font/glyphicons-halflings-regular.eot | Bin 0 -> 20127 bytes backend/web/font/glyphicons-halflings-regular.svg | 288 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/web/font/glyphicons-halflings-regular.ttf | Bin 0 -> 45404 bytes backend/web/font/glyphicons-halflings-regular.woff | Bin 0 -> 23424 bytes backend/web/font/glyphicons-halflings-regular.woff2 | Bin 0 -> 18028 bytes 11 files changed, 410 insertions(+), 24 deletions(-) create mode 100644 backend/components/views/_gallery_item.php create mode 100644 backend/web/font/glyphicons-halflings-regular.eot create mode 100644 backend/web/font/glyphicons-halflings-regular.svg create mode 100644 backend/web/font/glyphicons-halflings-regular.ttf create mode 100644 backend/web/font/glyphicons-halflings-regular.woff create mode 100644 backend/web/font/glyphicons-halflings-regular.woff2 diff --git a/backend/components/ImageSizer.php b/backend/components/ImageSizer.php index 9aa9ff6..bccb202 100644 --- a/backend/components/ImageSizer.php +++ b/backend/components/ImageSizer.php @@ -19,6 +19,7 @@ class ImageSizer extends Widget public $model; public $form; public $multi = false; + public $gallery; public function init(){ @@ -42,4 +43,8 @@ class ImageSizer extends Widget } + public function getGalley(){ + return $this->gallery; + } + } \ No newline at end of file diff --git a/backend/components/base/BaseController.php b/backend/components/base/BaseController.php index 7141e09..00b6654 100644 --- a/backend/components/base/BaseController.php +++ b/backend/components/base/BaseController.php @@ -40,25 +40,15 @@ class BaseController extends Controller { $smallestSide = $width; } + Image::crop($imageAlias, $smallestSide, $smallestSide,[$x,$y]) + ->save(Yii::getAlias($imageAliasSave), ['quality' => + 100]); - -// die('$width = '.$width.','.'$height = '.$height.','.'$y = '.$y.','.'$x = '.$x.','.'$smallestSide = '.$smallestSide.',' ); -// -// $ratio = $size->getWidth()/$size->getHeight(); -// -// $height = $h; -// $width = round($height * $ratio); - - $image_p = imagecreatetruecolor($w, $h); - - $image = imagecreatefromjpeg($imageAlias); - - imagecopyresampled($image_p, $image, 0, 0, $x, $y, - $w, $h, $smallestSide, $smallestSide); - - imagejpeg($image_p, $imageAliasSave); - + $imagine = new Imagine(); + $imagine->open($imageAliasSave) + ->resize(new Box($w, $h)) + ->save($imageAliasSave, array('flatten' => false)); @@ -77,16 +67,43 @@ class BaseController extends Controller { $model->file = UploadedFile::getInstance($model, 'file'); $md5_file = md5_file($model->file->tempName); + $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); - $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); - $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension; + + $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension); if(!is_dir($imgDir)) { mkdir($imgDir, 0755, true); } - $model->file->saveAs($imageAlias); - $this->resizeImg($model->width, $model->height, $imageAlias,$imageAlias); - return json_encode(['link'=>$imageLink]); + + $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 = $imageOrigAlias; + + } + + if($model->multi){ + + return json_encode(['link'=>$imageLink]); + + } else { + $view = $this->renderPartial('@app/components/views/_gallery_item', [ + 'item' => ['image'=>$imageLink], + ]); + + return json_encode(['link'=>$imageLink, 'view' =>$view]); + } + } } diff --git a/backend/components/views/_gallery_item.php b/backend/components/views/_gallery_item.php new file mode 100644 index 0000000..e8febd9 --- /dev/null +++ b/backend/components/views/_gallery_item.php @@ -0,0 +1,8 @@ + + \ No newline at end of file diff --git a/backend/components/views/image_sizer.php b/backend/components/views/image_sizer.php index 6ff14ab..a66cca0 100644 --- a/backend/components/views/image_sizer.php +++ b/backend/components/views/image_sizer.php @@ -11,13 +11,40 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.iframe- $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupload.js'); ?> + + +field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo" ]); ?> field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> field(new \backend\models\ImageSizerForm(),'height')->hiddenInput(['value' => $height])->label(false) ?> field(new \backend\models\ImageSizerForm(), 'width')->hiddenInput(['value' => $width])->label(false) ?> +
$field ? Html::img('/'.$model->$field): '' ?>
@@ -39,3 +66,43 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl }) + + field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo", 'multiple'=> 'multiple']); ?> + + field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> + field(new \backend\models\ImageSizerForm(),'height')->hiddenInput(['value' => $height])->label(false) ?> + field(new \backend\models\ImageSizerForm(), 'width')->hiddenInput(['value' => $width])->label(false) ?> + field(new \backend\models\ImageSizerForm(), 'multi')->hiddenInput(['value' => $multi])->label(false) ?> +
+ context->getGalley() as $image){ + echo $this->render('_gallery_item', [ 'item' => ['image'=>$image]]); + } + ?> +
+ + \ No newline at end of file diff --git a/backend/models/ImageSizerForm.php b/backend/models/ImageSizerForm.php index 4ebe896..69158eb 100644 --- a/backend/models/ImageSizerForm.php +++ b/backend/models/ImageSizerForm.php @@ -18,6 +18,7 @@ class ImageSizerForm extends Model public $field; public $model; public $form; + public $multi; /** * @return array the validation rules. diff --git a/backend/views/user/_form.php b/backend/views/user/_form.php index 9a60b15..558f441 100644 --- a/backend/views/user/_form.php +++ b/backend/views/user/_form.php @@ -33,7 +33,7 @@ use yii\helpers\ArrayHelper; ], 'id', 'name')) ?> - $form, 'model'=> $model, 'field'=>'photo','width'=>200,'height'=>200,'multi'=>false]); ?> + $form, 'model'=> $model, 'field'=>'photo','width'=>200,'height'=>200,'multi'=>true, 'gallery' => []]); ?> field($model, 'contacts')->textInput(['maxlength' => true]) ?> diff --git a/backend/web/font/glyphicons-halflings-regular.eot b/backend/web/font/glyphicons-halflings-regular.eot new file mode 100644 index 0000000..b93a495 Binary files /dev/null and b/backend/web/font/glyphicons-halflings-regular.eot differ diff --git a/backend/web/font/glyphicons-halflings-regular.svg b/backend/web/font/glyphicons-halflings-regular.svg new file mode 100644 index 0000000..94fb549 --- /dev/null +++ b/backend/web/font/glyphicons-halflings-regular.svg @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/backend/web/font/glyphicons-halflings-regular.ttf b/backend/web/font/glyphicons-halflings-regular.ttf new file mode 100644 index 0000000..1413fc6 Binary files /dev/null and b/backend/web/font/glyphicons-halflings-regular.ttf differ diff --git a/backend/web/font/glyphicons-halflings-regular.woff b/backend/web/font/glyphicons-halflings-regular.woff new file mode 100644 index 0000000..9e61285 Binary files /dev/null and b/backend/web/font/glyphicons-halflings-regular.woff differ diff --git a/backend/web/font/glyphicons-halflings-regular.woff2 b/backend/web/font/glyphicons-halflings-regular.woff2 new file mode 100644 index 0000000..64539b5 Binary files /dev/null and b/backend/web/font/glyphicons-halflings-regular.woff2 differ -- libgit2 0.21.4