Commit b5f053d9e0790ede5eb19005ea5ace1e4e277015
1 parent
d3e38cf8
Adding dirs for patterns
Showing
5 changed files
with
41 additions
and
27 deletions
Show diff stats
backend/components/Helper.php
| 1 | <?php | 1 | <?php |
| 2 | -/** | ||
| 3 | - * Created by PhpStorm. | ||
| 4 | - * User: denys | ||
| 5 | - * Date: 29.10.15 | ||
| 6 | - * Time: 11:35 | ||
| 7 | - */ | ||
| 8 | 2 | ||
| 9 | namespace backend\components; | 3 | namespace backend\components; |
| 10 | 4 | ||
| @@ -40,4 +34,23 @@ class Helper | @@ -40,4 +34,23 @@ class Helper | ||
| 40 | ); | 34 | ); |
| 41 | return strtr($str, $converter); | 35 | return strtr($str, $converter); |
| 42 | } | 36 | } |
| 37 | + | ||
| 38 | + public static function findUrl($filePath, $countDir) { | ||
| 39 | + $pathArr = explode('/', $filePath); | ||
| 40 | + $n = count($pathArr); | ||
| 41 | + $url = ''; | ||
| 42 | + for ($i = $n - $countDir - 1; $i < $n; $i++) | ||
| 43 | + $url .= '/' . $pathArr[$i]; | ||
| 44 | + return $url; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public static function isImage($file) { | ||
| 48 | + $a = getimagesize($file); | ||
| 49 | + $image_type = $a[2]; | ||
| 50 | + if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) | ||
| 51 | + { | ||
| 52 | + return true; | ||
| 53 | + } | ||
| 54 | + return false; | ||
| 55 | + } | ||
| 43 | } | 56 | } |
| 44 | \ No newline at end of file | 57 | \ No newline at end of file |
backend/components/croppers/CropContext.php
backend/components/croppers/CropFactory.php
| @@ -40,6 +40,8 @@ class CropFactory | @@ -40,6 +40,8 @@ class CropFactory | ||
| 40 | return $min; | 40 | return $min; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | + | ||
| 44 | + | ||
| 43 | public static function getInstance() { | 45 | public static function getInstance() { |
| 44 | if (null === self::$instance) { | 46 | if (null === self::$instance) { |
| 45 | self::$instance = new self(); | 47 | self::$instance = new self(); |
backend/controllers/ImageController.php
| @@ -8,7 +8,6 @@ use backend\components\croppers\CropFactory; | @@ -8,7 +8,6 @@ use backend\components\croppers\CropFactory; | ||
| 8 | use backend\components\Helper; | 8 | use backend\components\Helper; |
| 9 | use Yii; | 9 | use Yii; |
| 10 | use yii\helpers\FileHelper; | 10 | use yii\helpers\FileHelper; |
| 11 | -use yii\helpers\StringHelper; | ||
| 12 | use yii\web\Controller; | 11 | use yii\web\Controller; |
| 13 | use backend\models\UploadForm; | 12 | use backend\models\UploadForm; |
| 14 | use yii\web\UploadedFile; | 13 | use yii\web\UploadedFile; |
| @@ -23,22 +22,23 @@ class ImageController extends Controller | @@ -23,22 +22,23 @@ class ImageController extends Controller | ||
| 23 | 22 | ||
| 24 | if ($request->isPost) { | 23 | if ($request->isPost) { |
| 25 | $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); | 24 | $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); |
| 26 | - $model->upload(); | ||
| 27 | 25 | ||
| 28 | $crop_id = $request->post('crop_id'); | 26 | $crop_id = $request->post('crop_id'); |
| 29 | 27 | ||
| 28 | + $model->upload($ukrSeeds[$crop_id]); | ||
| 29 | + | ||
| 30 | $cropFactory = CropFactory::getInstance(); | 30 | $cropFactory = CropFactory::getInstance(); |
| 31 | $minHeight = $cropFactory->findMinHeight(); | 31 | $minHeight = $cropFactory->findMinHeight(); |
| 32 | $cropContext = new CropContext($cropFactory->getCrop($crop_id)); | 32 | $cropContext = new CropContext($cropFactory->getCrop($crop_id)); |
| 33 | - | 33 | + $basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/'; |
| 34 | foreach($model->imageFiles as $file) { | 34 | foreach($model->imageFiles as $file) { |
| 35 | $baseName = Helper::transliterate($file->baseName); | 35 | $baseName = Helper::transliterate($file->baseName); |
| 36 | - $path = dirname(dirname(__DIR__)) . '/uploads/' . $baseName . '.' . $file->extension; | 36 | + $path = $basePath . $baseName . '.' . $file->extension; |
| 37 | $image = Yii::$app->imagine->open($path); | 37 | $image = Yii::$app->imagine->open($path); |
| 38 | $image = $cropContext->cropImage($image, $path); | 38 | $image = $cropContext->cropImage($image, $path); |
| 39 | $box = $image->getSize()->heighten($minHeight); | 39 | $box = $image->getSize()->heighten($minHeight); |
| 40 | $image->resize($box) | 40 | $image->resize($box) |
| 41 | - ->save(dirname(dirname(__DIR__)) . '/uploads/' . $baseName . '-resized' . '.' . $file->extension); | 41 | + ->save($basePath . $baseName . '-resized' . '.' . $file->extension); |
| 42 | } | 42 | } |
| 43 | } | 43 | } |
| 44 | return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); | 44 | return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); |
| @@ -50,12 +50,8 @@ class ImageController extends Controller | @@ -50,12 +50,8 @@ class ImageController extends Controller | ||
| 50 | $images = []; | 50 | $images = []; |
| 51 | 51 | ||
| 52 | foreach($files as $file) { | 52 | foreach($files as $file) { |
| 53 | - $a = getimagesize($file); | ||
| 54 | - $image_type = $a[2]; | ||
| 55 | - | ||
| 56 | - if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) | ||
| 57 | - { | ||
| 58 | - $images[] = '/uploads/' . StringHelper::basename($file); | 53 | + if(Helper::isImage($file)) { |
| 54 | + $images[] = Helper::findUrl($file, 2); | ||
| 59 | } | 55 | } |
| 60 | } | 56 | } |
| 61 | return $this->render('gallery', ['files' => $images]); | 57 | return $this->render('gallery', ['files' => $images]); |
| @@ -82,13 +78,10 @@ class ImageController extends Controller | @@ -82,13 +78,10 @@ class ImageController extends Controller | ||
| 82 | $zip = new \ZipArchive(); | 78 | $zip = new \ZipArchive(); |
| 83 | 79 | ||
| 84 | $filename = dirname(dirname(__DIR__)) . '/uploads/images.zip'; | 80 | $filename = dirname(dirname(__DIR__)) . '/uploads/images.zip'; |
| 85 | - $zip->open($filename, \ZipArchive::CREATE); | 81 | + $zip->open($filename, \ZipArchive::OVERWRITE); |
| 86 | foreach($files as $k => $file) { | 82 | foreach($files as $k => $file) { |
| 87 | - $a = getimagesize($file); | ||
| 88 | - $image_type = $a[2]; | ||
| 89 | - if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) | ||
| 90 | - { | ||
| 91 | - $zip->addFile($file, StringHelper::basename($file)); | 83 | + if(Helper::isImage($file)) { |
| 84 | + $zip->addFile($file, Helper::findUrl($file, 1)); | ||
| 92 | } | 85 | } |
| 93 | } | 86 | } |
| 94 | $zip->close(); | 87 | $zip->close(); |
backend/models/UploadForm.php
| @@ -3,6 +3,7 @@ namespace backend\models; | @@ -3,6 +3,7 @@ namespace backend\models; | ||
| 3 | 3 | ||
| 4 | use backend\components\Helper; | 4 | use backend\components\Helper; |
| 5 | use yii\base\Model; | 5 | use yii\base\Model; |
| 6 | +use yii\helpers\FileHelper; | ||
| 6 | use yii\web\UploadedFile; | 7 | use yii\web\UploadedFile; |
| 7 | 8 | ||
| 8 | class UploadForm extends Model | 9 | class UploadForm extends Model |
| @@ -12,16 +13,21 @@ class UploadForm extends Model | @@ -12,16 +13,21 @@ class UploadForm extends Model | ||
| 12 | public function rules() | 13 | public function rules() |
| 13 | { | 14 | { |
| 14 | return [ | 15 | return [ |
| 15 | - [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 20], | 16 | + [['imageFiles'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg', 'maxFiles' => 20], |
| 16 | 17 | ||
| 17 | ]; | 18 | ]; |
| 18 | } | 19 | } |
| 19 | 20 | ||
| 20 | - public function upload() | 21 | + public function upload($nameDir) |
| 21 | { | 22 | { |
| 23 | + $basePath = dirname(dirname(__DIR__)) . '/uploads/'; | ||
| 24 | + | ||
| 25 | + if(!file_exists($basePath . $nameDir)) | ||
| 26 | + FileHelper::createDirectory($basePath . $nameDir . '/'); | ||
| 27 | + | ||
| 22 | if ($this->validate()) { | 28 | if ($this->validate()) { |
| 23 | foreach($this->imageFiles as $file) { | 29 | foreach($this->imageFiles as $file) { |
| 24 | - $path = dirname(dirname(__DIR__)) . '/uploads/' . Helper::transliterate($file->baseName) . '.' . $file->extension; | 30 | + $path = $basePath . $nameDir . '/' . Helper::transliterate($file->baseName) . '.' . $file->extension; |
| 25 | $file->saveAs($path); | 31 | $file->saveAs($path); |
| 26 | } | 32 | } |
| 27 | return true; | 33 | return true; |