From bfa22e8e6df1fbaa20ce6d2ce12e5f5b18e7cae9 Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 29 Oct 2015 11:10:07 +0200 Subject: [PATCH] Adding resizing and download feature --- backend/components/croppers/AbstractCrop.php | 2 +- backend/components/croppers/CropContext.php | 2 +- backend/components/croppers/CropFactory.php | 9 +++++++++ backend/controllers/ImageController.php | 41 +++++++++++++++++++++++++++++++++++++---- backend/models/UploadForm.php | 2 +- backend/views/image/gallery.php | 11 ++++++----- uploads/1900cofe290515_019.jpg | Bin 2970 -> 0 bytes uploads/1900cofe290515_044.jpg | Bin 22647 -> 0 bytes uploads/1900cofe290515_044new.jpg | Bin 24909 -> 0 bytes uploads/Screenshot from 2015-09-23 10:45:34.png | Bin 87577 -> 0 bytes uploads/petrushkaUniversalna.jpg | Bin 34596 -> 0 bytes 11 files changed, 55 insertions(+), 12 deletions(-) delete mode 100644 uploads/1900cofe290515_019.jpg delete mode 100644 uploads/1900cofe290515_044.jpg delete mode 100644 uploads/1900cofe290515_044new.jpg delete mode 100644 uploads/Screenshot from 2015-09-23 10:45:34.png delete mode 100644 uploads/petrushkaUniversalna.jpg diff --git a/backend/components/croppers/AbstractCrop.php b/backend/components/croppers/AbstractCrop.php index ee15a7d..ae7b1ae 100644 --- a/backend/components/croppers/AbstractCrop.php +++ b/backend/components/croppers/AbstractCrop.php @@ -13,7 +13,7 @@ abstract class AbstractCrop public function crop($image, $path) { - $image->crop(new Point(static::X, static::Y), new Box(static::WIDTH, static::HEIGHT)) + return $image->crop(new Point(static::X, static::Y), new Box(static::WIDTH, static::HEIGHT)) ->save($path); } } \ No newline at end of file diff --git a/backend/components/croppers/CropContext.php b/backend/components/croppers/CropContext.php index ae694cc..80378ba 100644 --- a/backend/components/croppers/CropContext.php +++ b/backend/components/croppers/CropContext.php @@ -15,6 +15,6 @@ class CropContext } public function cropImage($image, $path) { - $this->cropper->crop($image, $path); + return $this->cropper->crop($image, $path); } } \ No newline at end of file diff --git a/backend/components/croppers/CropFactory.php b/backend/components/croppers/CropFactory.php index e538fe3..e0879f6 100644 --- a/backend/components/croppers/CropFactory.php +++ b/backend/components/croppers/CropFactory.php @@ -31,6 +31,15 @@ class CropFactory return $this->crops[$crop_id]; } + public function findMinHeight() { + $min = PHP_INT_MAX; + foreach ($this->crops as $crop) { + $height = $crop->getConstants()['HEIGHT']; + $min = $min > $height ? $height : $min; + } + return $min; + } + public static function getInstance() { if (null === self::$instance) { self::$instance = new self(); diff --git a/backend/controllers/ImageController.php b/backend/controllers/ImageController.php index 5e61207..5ac7d03 100644 --- a/backend/controllers/ImageController.php +++ b/backend/controllers/ImageController.php @@ -27,12 +27,16 @@ class ImageController extends Controller $crop_id = $request->post('crop_id'); $cropFactory = CropFactory::getInstance(); + $minHeight = $cropFactory->findMinHeight(); $cropContext = new CropContext($cropFactory->getCrop($crop_id)); foreach($model->imageFiles as $file) { $path = dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '.' . $file->extension; $image = Yii::$app->imagine->open($path); - $cropContext->cropImage($image, $path); + $image = $cropContext->cropImage($image, $path); + $box = $image->getSize()->heighten($minHeight); + $image->resize($box) + ->save(dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '-resized' . '.' . $file->extension); } } return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); @@ -41,10 +45,18 @@ class ImageController extends Controller public function actionGallery() { $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/'); - foreach($files as $k => $file) { - $files[$k] = '/uploads/' . StringHelper::basename($file); + $images = []; + + foreach($files as $file) { + $a = getimagesize($file); + $image_type = $a[2]; + + if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) + { + $images[] = '/uploads/' . StringHelper::basename($file); + } } - return $this->render('gallery', ['files' => $files]); + return $this->render('gallery', ['files' => $images]); } public function actionGetParams($crop_id) @@ -61,4 +73,25 @@ class ImageController extends Controller return json_encode($res); } } + + public function actionDownloadAll() + { + $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/'); + $zip = new \ZipArchive(); + + $filename = dirname(dirname(__DIR__)) . '/uploads/images.zip'; + $zip->open($filename, \ZipArchive::CREATE); + foreach($files as $k => $file) { + $a = getimagesize($file); + $image_type = $a[2]; + if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) + { + $zip->addFile($file, StringHelper::basename($file)); + } + } + $zip->close(); + + $response = Yii::$app->response; + $response->sendFile($filename); + } } \ No newline at end of file diff --git a/backend/models/UploadForm.php b/backend/models/UploadForm.php index 703ed18..4c2fd42 100644 --- a/backend/models/UploadForm.php +++ b/backend/models/UploadForm.php @@ -11,7 +11,7 @@ class UploadForm extends Model public function rules() { return [ - [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4], + [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 20], ]; } diff --git a/backend/views/image/gallery.php b/backend/views/image/gallery.php index 5d3c725..2414012 100644 --- a/backend/views/image/gallery.php +++ b/backend/views/image/gallery.php @@ -1,13 +1,14 @@ title = 'Image gallery'; ?> - + +
- - \ No newline at end of file + + +
diff --git a/uploads/1900cofe290515_019.jpg b/uploads/1900cofe290515_019.jpg deleted file mode 100644 index 0888c06..0000000 Binary files a/uploads/1900cofe290515_019.jpg and /dev/null differ diff --git a/uploads/1900cofe290515_044.jpg b/uploads/1900cofe290515_044.jpg deleted file mode 100644 index 5caba7b..0000000 Binary files a/uploads/1900cofe290515_044.jpg and /dev/null differ diff --git a/uploads/1900cofe290515_044new.jpg b/uploads/1900cofe290515_044new.jpg deleted file mode 100644 index cfe4921..0000000 Binary files a/uploads/1900cofe290515_044new.jpg and /dev/null differ diff --git a/uploads/Screenshot from 2015-09-23 10:45:34.png b/uploads/Screenshot from 2015-09-23 10:45:34.png deleted file mode 100644 index 484f841..0000000 Binary files a/uploads/Screenshot from 2015-09-23 10:45:34.png and /dev/null differ diff --git a/uploads/petrushkaUniversalna.jpg b/uploads/petrushkaUniversalna.jpg deleted file mode 100644 index e6ed527..0000000 Binary files a/uploads/petrushkaUniversalna.jpg and /dev/null differ -- libgit2 0.21.4