Commit 3a9830b4df5e85600baae182a8ffe0fd6c24e935
1 parent
ae264175
Importers CRUD
Showing
8 changed files
with
52 additions
and
44 deletions
 
Show diff stats
.htaccess
backend/components/ImageSizer.php
| @@ -18,6 +18,7 @@ class ImageSizer extends Widget | @@ -18,6 +18,7 @@ class ImageSizer extends Widget | ||
| 18 | public $file; | 18 | public $file; | 
| 19 | public $model; | 19 | public $model; | 
| 20 | public $form; | 20 | public $form; | 
| 21 | + public $multi = false; | ||
| 21 | 22 | ||
| 22 | public function init(){ | 23 | public function init(){ | 
| 23 | 24 | ||
| @@ -35,7 +36,8 @@ class ImageSizer extends Widget | @@ -35,7 +36,8 @@ class ImageSizer extends Widget | ||
| 35 | 'field' => $this->field, | 36 | 'field' => $this->field, | 
| 36 | 'form' => $this->form, | 37 | 'form' => $this->form, | 
| 37 | 'height' => $this->height, | 38 | 'height' => $this->height, | 
| 38 | - 'width' => $this->width | 39 | + 'width' => $this->width, | 
| 40 | + 'multi' => $this->multi | ||
| 39 | ]); | 41 | ]); | 
| 40 | 42 | ||
| 41 | } | 43 | } | 
backend/components/base/BaseController.php
| @@ -26,49 +26,46 @@ class BaseController extends Controller { | @@ -26,49 +26,46 @@ class BaseController extends Controller { | ||
| 26 | $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); | 26 | $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); | 
| 27 | 27 | ||
| 28 | $size = $img->getSize(); | 28 | $size = $img->getSize(); | 
| 29 | - $ratio = $size->getWidth()/$size->getHeight(); | ||
| 30 | 29 | ||
| 31 | - $height = $h; | ||
| 32 | - $width = round($height * $ratio); | 30 | + $width = $size->getWidth(); | 
| 31 | + $height = $size->getHeight(); | ||
| 32 | + | ||
| 33 | + if($width > $height) { | ||
| 34 | + $y = 0; | ||
| 35 | + $x = ($width - $height) / 2; | ||
| 36 | + $smallestSide = $height; | ||
| 37 | + } else { | ||
| 38 | + $x = 0; | ||
| 39 | + $y = ($height - $width) / 2; | ||
| 40 | + $smallestSide = $width; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + | ||
| 46 | +// die('$width = '.$width.','.'$height = '.$height.','.'$y = '.$y.','.'$x = '.$x.','.'$smallestSide = '.$smallestSide.',' ); | ||
| 47 | +// | ||
| 48 | +// $ratio = $size->getWidth()/$size->getHeight(); | ||
| 49 | +// | ||
| 50 | +// $height = $h; | ||
| 51 | +// $width = round($height * $ratio); | ||
| 52 | + | ||
| 53 | + $image_p = imagecreatetruecolor($w, $h); | ||
| 54 | + | ||
| 55 | + $image = imagecreatefromjpeg($imageAlias); | ||
| 33 | 56 | ||
| 57 | + imagecopyresampled($image_p, $image, 0, 0, $x, $y, | ||
| 58 | + $w, $h, $smallestSide, $smallestSide); | ||
| 59 | + | ||
| 60 | + imagejpeg($image_p, $imageAliasSave); | ||
| 34 | 61 | ||
| 35 | - $imagine = new Imagine(); | ||
| 36 | 62 | ||
| 37 | - $imagine->open($imageAlias) | ||
| 38 | - ->resize(new Box($width, $h)) | ||
| 39 | - ->save($imageAlias, array('flatten' => false)); | ||
| 40 | 63 | ||
| 41 | 64 | ||
| 42 | - Image::crop($imageAlias, $w, $h,[($width/2)-($w/2),0]) | ||
| 43 | - ->save(Yii::getAlias($imageAliasSave), ['quality' => | ||
| 44 | - 100]); | ||
| 45 | 65 | ||
| 46 | 66 | ||
| 47 | } | 67 | } | 
| 48 | -// | ||
| 49 | -// private function resizeImg($w, $h, $filepath,$newfilepath){ | ||
| 50 | -// list($width, $height) = getimagesize($filepath); | ||
| 51 | -// if($width > $height) { | ||
| 52 | -// $y = 0; | ||
| 53 | -// $x = ($width - $height) / 2; | ||
| 54 | -// $smallestSide = $height; | ||
| 55 | -// } else { | ||
| 56 | -// $x = 0; | ||
| 57 | -// $y = ($height - $width) / 2; | ||
| 58 | -// $smallestSide = $width; | ||
| 59 | -// } | ||
| 60 | -// | ||
| 61 | -// $image_p = imagecreatetruecolor($w, $h); | ||
| 62 | -// | ||
| 63 | -// $image = imagecreatefromjpeg($filepath); | ||
| 64 | -// | ||
| 65 | -// imagecopyresampled($image_p, $image, 0, 0, $x, $y, | ||
| 66 | -// $w, $h, $smallestSide, $smallestSide); | ||
| 67 | -// | ||
| 68 | -// imagejpeg($image_p, $newfilepath); | ||
| 69 | -// | ||
| 70 | -// | ||
| 71 | -// } | 68 | + | 
| 72 | 69 | ||
| 73 | public function actionDownloadPhoto() | 70 | public function actionDownloadPhoto() | 
| 74 | { | 71 | { | 
| @@ -82,7 +79,7 @@ class BaseController extends Controller { | @@ -82,7 +79,7 @@ class BaseController extends Controller { | ||
| 82 | $md5_file = md5_file($model->file->tempName); | 79 | $md5_file = md5_file($model->file->tempName); | 
| 83 | $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); | 80 | $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); | 
| 84 | $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); | 81 | $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); | 
| 85 | - $imageLink = '/storage/'.$md5_file.$model->width.'x'.$model->height.'.'.$model->file->extension; | 82 | + $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension; | 
| 86 | 83 | ||
| 87 | if(!is_dir($imgDir)) { | 84 | if(!is_dir($imgDir)) { | 
| 88 | mkdir($imgDir, 0755, true); | 85 | mkdir($imgDir, 0755, true); | 
backend/components/views/image_sizer.php
| @@ -12,9 +12,11 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | @@ -12,9 +12,11 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | ||
| 12 | 12 | ||
| 13 | ?> | 13 | ?> | 
| 14 | 14 | ||
| 15 | -<?= $form->field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo"]); ?> | 15 | +<?= $form->field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo", 'multiple'=> $multi?'multiple':'' ]); ?> | 
| 16 | 16 | ||
| 17 | <?= $form->field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> | 17 | <?= $form->field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> | 
| 18 | +<?= $form->field(new \backend\models\ImageSizerForm(),'height')->hiddenInput(['value' => $height])->label(false) ?> | ||
| 19 | +<?= $form->field(new \backend\models\ImageSizerForm(), 'width')->hiddenInput(['value' => $width])->label(false) ?> | ||
| 18 | 20 | ||
| 19 | <div id="<?= $field?>_img_block"> | 21 | <div id="<?= $field?>_img_block"> | 
| 20 | <?= $model->$field ? Html::img('/'.$model->$field): '' ?> | 22 | <?= $model->$field ? Html::img('/'.$model->$field): '' ?> | 
| @@ -25,14 +27,8 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | @@ -25,14 +27,8 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | ||
| 25 | 27 | ||
| 26 | $("#<?= $field?>").fileupload({ | 28 | $("#<?= $field?>").fileupload({ | 
| 27 | dataType: 'json', | 29 | dataType: 'json', | 
| 28 | - ImageSizerForm: { | ||
| 29 | - height: <?=$height?>, | ||
| 30 | - width: <?= $width?> | ||
| 31 | - }, | ||
| 32 | done: function (e, data) { | 30 | done: function (e, data) { | 
| 33 | var host = window.location.host.toString(); | 31 | var host = window.location.host.toString(); | 
| 34 | - console.log(host); | ||
| 35 | - console.log(data.result.link); | ||
| 36 | var img = '<img src="http://'+host+data.result.link+'">'; | 32 | var img = '<img src="http://'+host+data.result.link+'">'; | 
| 37 | var block = $("#<?= $field?>_img_block"); | 33 | var block = $("#<?= $field?>_img_block"); | 
| 38 | block.find('img').remove(); | 34 | block.find('img').remove(); | 
backend/models/Importers.php
| @@ -36,6 +36,10 @@ class Importers extends BaseActiveRecord | @@ -36,6 +36,10 @@ class Importers extends BaseActiveRecord | ||
| 36 | /** | 36 | /** | 
| 37 | * @inheritdoc | 37 | * @inheritdoc | 
| 38 | */ | 38 | */ | 
| 39 | + public static function tableName() | ||
| 40 | + { | ||
| 41 | + return 'w_importers'; | ||
| 42 | + } | ||
| 39 | 43 | ||
| 40 | public function rules() | 44 | public function rules() | 
| 41 | { | 45 | { | 
backend/views/user/_form.php
| @@ -33,7 +33,7 @@ use yii\helpers\ArrayHelper; | @@ -33,7 +33,7 @@ use yii\helpers\ArrayHelper; | ||
| 33 | ], 'id', 'name')) ?> | 33 | ], 'id', 'name')) ?> | 
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | - <?= \backend\components\ImageSizer::widget(['form'=>$form, 'model'=> $model, 'field'=>'photo','width'=>200,'height'=>200]); ?> | 36 | + <?= \backend\components\ImageSizer::widget(['form'=>$form, 'model'=> $model, 'field'=>'photo','width'=>200,'height'=>200,'multi'=>false]); ?> | 
| 37 | 37 | ||
| 38 | <?= $form->field($model, 'contacts')->textInput(['maxlength' => true]) ?> | 38 | <?= $form->field($model, 'contacts')->textInput(['maxlength' => true]) ?> | 
| 39 | 39 | 
| 1 | +<?php |