Commit bfa22e8e6df1fbaa20ce6d2ce12e5f5b18e7cae9

Authored by Administrator
1 parent 3ff61e27

Adding resizing and download feature

backend/components/croppers/AbstractCrop.php
@@ -13,7 +13,7 @@ abstract class AbstractCrop @@ -13,7 +13,7 @@ abstract class AbstractCrop
13 13
14 public function crop($image, $path) 14 public function crop($image, $path)
15 { 15 {
16 - $image->crop(new Point(static::X, static::Y), new Box(static::WIDTH, static::HEIGHT)) 16 + return $image->crop(new Point(static::X, static::Y), new Box(static::WIDTH, static::HEIGHT))
17 ->save($path); 17 ->save($path);
18 } 18 }
19 } 19 }
20 \ No newline at end of file 20 \ No newline at end of file
backend/components/croppers/CropContext.php
@@ -15,6 +15,6 @@ class CropContext @@ -15,6 +15,6 @@ class CropContext
15 } 15 }
16 16
17 public function cropImage($image, $path) { 17 public function cropImage($image, $path) {
18 - $this->cropper->crop($image, $path); 18 + return $this->cropper->crop($image, $path);
19 } 19 }
20 } 20 }
21 \ No newline at end of file 21 \ No newline at end of file
backend/components/croppers/CropFactory.php
@@ -31,6 +31,15 @@ class CropFactory @@ -31,6 +31,15 @@ class CropFactory
31 return $this->crops[$crop_id]; 31 return $this->crops[$crop_id];
32 } 32 }
33 33
  34 + public function findMinHeight() {
  35 + $min = PHP_INT_MAX;
  36 + foreach ($this->crops as $crop) {
  37 + $height = $crop->getConstants()['HEIGHT'];
  38 + $min = $min > $height ? $height : $min;
  39 + }
  40 + return $min;
  41 + }
  42 +
34 public static function getInstance() { 43 public static function getInstance() {
35 if (null === self::$instance) { 44 if (null === self::$instance) {
36 self::$instance = new self(); 45 self::$instance = new self();
backend/controllers/ImageController.php
@@ -27,12 +27,16 @@ class ImageController extends Controller @@ -27,12 +27,16 @@ class ImageController extends Controller
27 $crop_id = $request->post('crop_id'); 27 $crop_id = $request->post('crop_id');
28 28
29 $cropFactory = CropFactory::getInstance(); 29 $cropFactory = CropFactory::getInstance();
  30 + $minHeight = $cropFactory->findMinHeight();
30 $cropContext = new CropContext($cropFactory->getCrop($crop_id)); 31 $cropContext = new CropContext($cropFactory->getCrop($crop_id));
31 32
32 foreach($model->imageFiles as $file) { 33 foreach($model->imageFiles as $file) {
33 $path = dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '.' . $file->extension; 34 $path = dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '.' . $file->extension;
34 $image = Yii::$app->imagine->open($path); 35 $image = Yii::$app->imagine->open($path);
35 - $cropContext->cropImage($image, $path); 36 + $image = $cropContext->cropImage($image, $path);
  37 + $box = $image->getSize()->heighten($minHeight);
  38 + $image->resize($box)
  39 + ->save(dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '-resized' . '.' . $file->extension);
36 } 40 }
37 } 41 }
38 return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); 42 return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]);
@@ -41,10 +45,18 @@ class ImageController extends Controller @@ -41,10 +45,18 @@ class ImageController extends Controller
41 public function actionGallery() 45 public function actionGallery()
42 { 46 {
43 $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/'); 47 $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
44 - foreach($files as $k => $file) {  
45 - $files[$k] = '/uploads/' . StringHelper::basename($file); 48 + $images = [];
  49 +
  50 + foreach($files as $file) {
  51 + $a = getimagesize($file);
  52 + $image_type = $a[2];
  53 +
  54 + if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP]))
  55 + {
  56 + $images[] = '/uploads/' . StringHelper::basename($file);
  57 + }
46 } 58 }
47 - return $this->render('gallery', ['files' => $files]); 59 + return $this->render('gallery', ['files' => $images]);
48 } 60 }
49 61
50 public function actionGetParams($crop_id) 62 public function actionGetParams($crop_id)
@@ -61,4 +73,25 @@ class ImageController extends Controller @@ -61,4 +73,25 @@ class ImageController extends Controller
61 return json_encode($res); 73 return json_encode($res);
62 } 74 }
63 } 75 }
  76 +
  77 + public function actionDownloadAll()
  78 + {
  79 + $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
  80 + $zip = new \ZipArchive();
  81 +
  82 + $filename = dirname(dirname(__DIR__)) . '/uploads/images.zip';
  83 + $zip->open($filename, \ZipArchive::CREATE);
  84 + foreach($files as $k => $file) {
  85 + $a = getimagesize($file);
  86 + $image_type = $a[2];
  87 + if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP]))
  88 + {
  89 + $zip->addFile($file, StringHelper::basename($file));
  90 + }
  91 + }
  92 + $zip->close();
  93 +
  94 + $response = Yii::$app->response;
  95 + $response->sendFile($filename);
  96 + }
64 } 97 }
65 \ No newline at end of file 98 \ No newline at end of file
backend/models/UploadForm.php
@@ -11,7 +11,7 @@ class UploadForm extends Model @@ -11,7 +11,7 @@ class UploadForm extends Model
11 public function rules() 11 public function rules()
12 { 12 {
13 return [ 13 return [
14 - [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4], 14 + [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 20],
15 15
16 ]; 16 ];
17 } 17 }
backend/views/image/gallery.php
1 <?php 1 <?php
2 2
3 -use \kartik\file\FileInput;  
4 -use yii\widgets\ActiveForm; 3 +
5 use yii\helpers\Html; 4 use yii\helpers\Html;
6 5
7 /* @var $this yii\web\View */ 6 /* @var $this yii\web\View */
8 $this->title = 'Image gallery'; 7 $this->title = 'Image gallery';
9 ?> 8 ?>
10 - 9 +<?= Html::a('Download all', ['download-all']) ?>
  10 +<div>
11 <?php foreach ($files as $file): ?> 11 <?php foreach ($files as $file): ?>
12 -<img height="500px" src="<?= Html::encode($file) ?>">  
13 -<?php endforeach ?>  
14 \ No newline at end of file 12 \ No newline at end of file
  13 +<img height="300px" src="<?= Html::encode($file) ?>">
  14 +<?php endforeach ?>
  15 +</div>
uploads/1900cofe290515_019.jpg deleted

2.9 KB

uploads/1900cofe290515_044.jpg deleted

22.1 KB

uploads/1900cofe290515_044new.jpg deleted

24.3 KB

uploads/Screenshot from 2015-09-23 10:45:34.png deleted

85.5 KB

uploads/petrushkaUniversalna.jpg deleted

33.8 KB