Commit 4f24d74c93e5d50060474578d8bc549ab4fc441f
1 parent
d1f35cd0
Importers CRUD
Showing
7 changed files
with
77 additions
and
12 deletions
Show diff stats
backend/components/ImageSizer.php
backend/components/base/BaseController.php
... | ... | @@ -7,9 +7,13 @@ |
7 | 7 | */ |
8 | 8 | namespace backend\components\base; |
9 | 9 | |
10 | +use Yii; | |
11 | +use yii\web\UploadedFile; | |
12 | +use backend\models\ImageSizerForm; | |
10 | 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 | 18 | class BaseController extends Controller { |
15 | 19 | |
... | ... | @@ -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 | 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 | 80 | if ($model->load(Yii::$app->request->post())) { |
51 | 81 | |
52 | 82 | $model->file = UploadedFile::getInstance($model, 'file'); |
53 | 83 | |
54 | 84 | $md5_file = md5_file($model->file->tempName); |
55 | 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 | 88 | if(!is_dir($imgDir)) { |
59 | - mkdir($imgDir, 0777, true); | |
89 | + mkdir($imgDir, 0755, true); | |
60 | 90 | } |
61 | 91 | $model->file->saveAs($imageAlias); |
62 | - $this->resizeImg(480, 365, $imageAlias,$imageAlias); | |
92 | + $this->resizeImg($model->width, $model->height, $imageAlias,$imageAlias); | |
63 | 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 | 25 | |
26 | 26 | $("#<?= $field?>").fileupload({ |
27 | 27 | dataType: 'json', |
28 | + formData: { | |
29 | + height: <?=$height?>, | |
30 | + width: <?= $width?> | |
31 | + }, | |
28 | 32 | done: function (e, data) { |
29 | 33 | var host = window.location.host.toString(); |
30 | 34 | console.log(host); | ... | ... |
backend/controllers/SiteController.php
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 | 29 | \ No newline at end of file | ... | ... |
backend/views/user/_form.php
... | ... | @@ -33,7 +33,7 @@ use yii\helpers\ArrayHelper; |
33 | 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 | 38 | <?= $form->field($model, 'contacts')->textInput(['maxlength' => true]) ?> |
39 | 39 | ... | ... |
composer.json
... | ... | @@ -18,7 +18,8 @@ |
18 | 18 | "yiisoft/yii2": ">=2.0.6", |
19 | 19 | "yiisoft/yii2-bootstrap": "*", |
20 | 20 | "yiisoft/yii2-swiftmailer": "*", |
21 | - "yiisoft/multiparser": "*" | |
21 | + "yiisoft/multiparser": "*", | |
22 | + "yiisoft/yii2-imagine": "*" | |
22 | 23 | }, |
23 | 24 | "require-dev": { |
24 | 25 | "yiisoft/yii2-codeception": "*", | ... | ... |