Commit 4f24d74c93e5d50060474578d8bc549ab4fc441f
1 parent
d1f35cd0
Importers CRUD
Showing
7 changed files
with
77 additions
and
12 deletions
Show diff stats
backend/components/ImageSizer.php
| @@ -33,7 +33,9 @@ class ImageSizer extends Widget | @@ -33,7 +33,9 @@ class ImageSizer extends Widget | ||
| 33 | [ | 33 | [ |
| 34 | 'model'=>$this->model, | 34 | 'model'=>$this->model, |
| 35 | 'field' => $this->field, | 35 | 'field' => $this->field, |
| 36 | - 'form' => $this->form | 36 | + 'form' => $this->form, |
| 37 | + 'height' => $this->height, | ||
| 38 | + 'width' => $this->width | ||
| 37 | ]); | 39 | ]); |
| 38 | 40 | ||
| 39 | } | 41 | } |
backend/components/base/BaseController.php
| @@ -7,9 +7,13 @@ | @@ -7,9 +7,13 @@ | ||
| 7 | */ | 7 | */ |
| 8 | namespace backend\components\base; | 8 | namespace backend\components\base; |
| 9 | 9 | ||
| 10 | +use Yii; | ||
| 11 | +use yii\web\UploadedFile; | ||
| 12 | +use backend\models\ImageSizerForm; | ||
| 10 | use yii\web\Controller; | 13 | use yii\web\Controller; |
| 11 | - | ||
| 12 | - | 14 | +use Imagine\Gd\Imagine; |
| 15 | +use Imagine\Image\Box; | ||
| 16 | +use yii\imagine\Image; | ||
| 13 | 17 | ||
| 14 | class BaseController extends Controller { | 18 | class BaseController extends Controller { |
| 15 | 19 | ||
| @@ -42,24 +46,50 @@ class BaseController extends Controller { | @@ -42,24 +46,50 @@ class BaseController extends Controller { | ||
| 42 | 46 | ||
| 43 | } | 47 | } |
| 44 | 48 | ||
| 49 | +// private function resizeImg($w, $h, $filepath,$newfilepath){ | ||
| 50 | +// list($orig_width, $orig_height) = getimagesize($filepath); | ||
| 51 | +// $width = $orig_width; | ||
| 52 | +// $height = $orig_height; | ||
| 53 | +// if($width > $height) { | ||
| 54 | +// $y = 0; | ||
| 55 | +// $x = ($width - $height) / 2; | ||
| 56 | +// $smallestSide = $height; | ||
| 57 | +// } else { | ||
| 58 | +// $x = 0; | ||
| 59 | +// $y = ($height - $width) / 2; | ||
| 60 | +// $smallestSide = $width; | ||
| 61 | +// } | ||
| 62 | +// | ||
| 63 | +// $image_p = imagecreatetruecolor($w, $h); | ||
| 64 | +// | ||
| 65 | +// $image = imagecreatefromjpeg($filepath); | ||
| 66 | +// | ||
| 67 | +// imagecopyresampled($image_p, $image, 0, 0, $x, $y, | ||
| 68 | +// $w, $h, $smallestSide, $smallestSide); | ||
| 69 | +// | ||
| 70 | +// imagejpeg($image_p, $newfilepath); | ||
| 71 | +// | ||
| 72 | +// | ||
| 73 | +// } | ||
| 74 | + | ||
| 45 | public function actionDownloadPhoto() | 75 | public function actionDownloadPhoto() |
| 46 | { | 76 | { |
| 47 | - die('here'); | ||
| 48 | - $model = new UploadForm(); | ||
| 49 | 77 | ||
| 78 | + $model = new ImageSizerForm(); | ||
| 79 | + //die(print_r(Yii::$app->request->post())); | ||
| 50 | if ($model->load(Yii::$app->request->post())) { | 80 | if ($model->load(Yii::$app->request->post())) { |
| 51 | 81 | ||
| 52 | $model->file = UploadedFile::getInstance($model, 'file'); | 82 | $model->file = UploadedFile::getInstance($model, 'file'); |
| 53 | 83 | ||
| 54 | $md5_file = md5_file($model->file->tempName); | 84 | $md5_file = md5_file($model->file->tempName); |
| 55 | $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); | 85 | $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); |
| 56 | - $imageAlias = Yii::getAlias($imgDir.'480x365'.'.'.$model->file->extension); | ||
| 57 | - $imageLink = '/storage/'.$md5_file.'/480x365'.'.'.$model->file->extension; | 86 | + $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension); |
| 87 | + $imageLink = '/storage/'.$md5_file.$model->width.'x'.$model->height.'.'.$model->file->extension; | ||
| 58 | if(!is_dir($imgDir)) { | 88 | if(!is_dir($imgDir)) { |
| 59 | - mkdir($imgDir, 0777, true); | 89 | + mkdir($imgDir, 0755, true); |
| 60 | } | 90 | } |
| 61 | $model->file->saveAs($imageAlias); | 91 | $model->file->saveAs($imageAlias); |
| 62 | - $this->resizeImg(480, 365, $imageAlias,$imageAlias); | 92 | + $this->resizeImg($model->width, $model->height, $imageAlias,$imageAlias); |
| 63 | return json_encode(['link'=>$imageLink]); | 93 | return json_encode(['link'=>$imageLink]); |
| 64 | 94 | ||
| 65 | } | 95 | } |
backend/components/views/image_sizer.php
| @@ -25,6 +25,10 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | @@ -25,6 +25,10 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl | ||
| 25 | 25 | ||
| 26 | $("#<?= $field?>").fileupload({ | 26 | $("#<?= $field?>").fileupload({ |
| 27 | dataType: 'json', | 27 | dataType: 'json', |
| 28 | + formData: { | ||
| 29 | + height: <?=$height?>, | ||
| 30 | + width: <?= $width?> | ||
| 31 | + }, | ||
| 28 | done: function (e, data) { | 32 | done: function (e, data) { |
| 29 | var host = window.location.host.toString(); | 33 | var host = window.location.host.toString(); |
| 30 | console.log(host); | 34 | console.log(host); |
backend/controllers/SiteController.php
| @@ -23,7 +23,7 @@ class SiteController extends BaseController | @@ -23,7 +23,7 @@ class SiteController extends BaseController | ||
| 23 | 'class' => AccessControl::className(), | 23 | 'class' => AccessControl::className(), |
| 24 | 'rules' => [ | 24 | 'rules' => [ |
| 25 | [ | 25 | [ |
| 26 | - 'actions' => ['login', 'error'], | 26 | + 'actions' => ['login', 'error', 'download-photo' ], |
| 27 | 'allow' => true, | 27 | 'allow' => true, |
| 28 | ], | 28 | ], |
| 29 | [ | 29 | [ |
| 1 | +<?php | ||
| 2 | +namespace backend\models; | ||
| 3 | + | ||
| 4 | +use yii\base\Model; | ||
| 5 | +use yii\web\UploadedFile; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * UploadForm is the model behind the upload form. | ||
| 9 | + */ | ||
| 10 | +class ImageSizerForm extends Model | ||
| 11 | +{ | ||
| 12 | + /** | ||
| 13 | + * @var UploadedFile file attribute | ||
| 14 | + */ | ||
| 15 | + public $file; | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * @return array the validation rules. | ||
| 19 | + */ | ||
| 20 | + public function rules() | ||
| 21 | + { | ||
| 22 | + return [ | ||
| 23 | + [['width', 'height'], 'integer'], | ||
| 24 | + [['field', 'model', 'form'], 'string', 'max' => 255], | ||
| 25 | + [['file'], 'file'], | ||
| 26 | + ]; | ||
| 27 | + } | ||
| 28 | +} | ||
| 0 | \ No newline at end of file | 29 | \ No newline at end of file |
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']); ?> | 36 | + <?= \backend\components\ImageSizer::widget(['form'=>$form, 'model'=> $model, 'field'=>'photo','width'=>200,'height'=>200]); ?> |
| 37 | 37 | ||
| 38 | <?= $form->field($model, 'contacts')->textInput(['maxlength' => true]) ?> | 38 | <?= $form->field($model, 'contacts')->textInput(['maxlength' => true]) ?> |
| 39 | 39 |
composer.json
| @@ -18,7 +18,8 @@ | @@ -18,7 +18,8 @@ | ||
| 18 | "yiisoft/yii2": ">=2.0.6", | 18 | "yiisoft/yii2": ">=2.0.6", |
| 19 | "yiisoft/yii2-bootstrap": "*", | 19 | "yiisoft/yii2-bootstrap": "*", |
| 20 | "yiisoft/yii2-swiftmailer": "*", | 20 | "yiisoft/yii2-swiftmailer": "*", |
| 21 | - "yiisoft/multiparser": "*" | 21 | + "yiisoft/multiparser": "*", |
| 22 | + "yiisoft/yii2-imagine": "*" | ||
| 22 | }, | 23 | }, |
| 23 | "require-dev": { | 24 | "require-dev": { |
| 24 | "yiisoft/yii2-codeception": "*", | 25 | "yiisoft/yii2-codeception": "*", |