Commit bfa22e8e6df1fbaa20ce6d2ce12e5f5b18e7cae9
1 parent
3ff61e27
Adding resizing and download feature
Showing
11 changed files
with
55 additions
and
12 deletions
Show diff stats
backend/components/croppers/AbstractCrop.php
| ... | ... | @@ -13,7 +13,7 @@ abstract class AbstractCrop |
| 13 | 13 | |
| 14 | 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 | 17 | ->save($path); |
| 18 | 18 | } |
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file | ... | ... |
backend/components/croppers/CropContext.php
backend/components/croppers/CropFactory.php
| ... | ... | @@ -31,6 +31,15 @@ class CropFactory |
| 31 | 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 | 43 | public static function getInstance() { |
| 35 | 44 | if (null === self::$instance) { |
| 36 | 45 | self::$instance = new self(); | ... | ... |
backend/controllers/ImageController.php
| ... | ... | @@ -27,12 +27,16 @@ class ImageController extends Controller |
| 27 | 27 | $crop_id = $request->post('crop_id'); |
| 28 | 28 | |
| 29 | 29 | $cropFactory = CropFactory::getInstance(); |
| 30 | + $minHeight = $cropFactory->findMinHeight(); | |
| 30 | 31 | $cropContext = new CropContext($cropFactory->getCrop($crop_id)); |
| 31 | 32 | |
| 32 | 33 | foreach($model->imageFiles as $file) { |
| 33 | 34 | $path = dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '.' . $file->extension; |
| 34 | 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 | 42 | return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); |
| ... | ... | @@ -41,10 +45,18 @@ class ImageController extends Controller |
| 41 | 45 | public function actionGallery() |
| 42 | 46 | { |
| 43 | 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 | 62 | public function actionGetParams($crop_id) |
| ... | ... | @@ -61,4 +73,25 @@ class ImageController extends Controller |
| 61 | 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 | 98 | \ No newline at end of file | ... | ... |
backend/models/UploadForm.php
| ... | ... | @@ -11,7 +11,7 @@ class UploadForm extends Model |
| 11 | 11 | public function rules() |
| 12 | 12 | { |
| 13 | 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 | 1 | <?php |
| 2 | 2 | |
| 3 | -use \kartik\file\FileInput; | |
| 4 | -use yii\widgets\ActiveForm; | |
| 3 | + | |
| 5 | 4 | use yii\helpers\Html; |
| 6 | 5 | |
| 7 | 6 | /* @var $this yii\web\View */ |
| 8 | 7 | $this->title = 'Image gallery'; |
| 9 | 8 | ?> |
| 10 | - | |
| 9 | +<?= Html::a('Download all', ['download-all']) ?> | |
| 10 | +<div> | |
| 11 | 11 | <?php foreach ($files as $file): ?> |
| 12 | -<img height="500px" src="<?= Html::encode($file) ?>"> | |
| 13 | -<?php endforeach ?> | |
| 14 | 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