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 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Created by PhpStorm. | |
| 4 | - * User: denys | |
| 5 | - * Date: 29.10.15 | |
| 6 | - * Time: 11:35 | |
| 7 | - */ | |
| 8 | 2 | |
| 9 | 3 | namespace backend\components; |
| 10 | 4 | |
| ... | ... | @@ -40,4 +34,23 @@ class Helper |
| 40 | 34 | ); |
| 41 | 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 | 57 | \ No newline at end of file | ... | ... |
backend/components/croppers/CropContext.php
backend/components/croppers/CropFactory.php
backend/controllers/ImageController.php
| ... | ... | @@ -8,7 +8,6 @@ use backend\components\croppers\CropFactory; |
| 8 | 8 | use backend\components\Helper; |
| 9 | 9 | use Yii; |
| 10 | 10 | use yii\helpers\FileHelper; |
| 11 | -use yii\helpers\StringHelper; | |
| 12 | 11 | use yii\web\Controller; |
| 13 | 12 | use backend\models\UploadForm; |
| 14 | 13 | use yii\web\UploadedFile; |
| ... | ... | @@ -23,22 +22,23 @@ class ImageController extends Controller |
| 23 | 22 | |
| 24 | 23 | if ($request->isPost) { |
| 25 | 24 | $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); |
| 26 | - $model->upload(); | |
| 27 | 25 | |
| 28 | 26 | $crop_id = $request->post('crop_id'); |
| 29 | 27 | |
| 28 | + $model->upload($ukrSeeds[$crop_id]); | |
| 29 | + | |
| 30 | 30 | $cropFactory = CropFactory::getInstance(); |
| 31 | 31 | $minHeight = $cropFactory->findMinHeight(); |
| 32 | 32 | $cropContext = new CropContext($cropFactory->getCrop($crop_id)); |
| 33 | - | |
| 33 | + $basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/'; | |
| 34 | 34 | foreach($model->imageFiles as $file) { |
| 35 | 35 | $baseName = Helper::transliterate($file->baseName); |
| 36 | - $path = dirname(dirname(__DIR__)) . '/uploads/' . $baseName . '.' . $file->extension; | |
| 36 | + $path = $basePath . $baseName . '.' . $file->extension; | |
| 37 | 37 | $image = Yii::$app->imagine->open($path); |
| 38 | 38 | $image = $cropContext->cropImage($image, $path); |
| 39 | 39 | $box = $image->getSize()->heighten($minHeight); |
| 40 | 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 | 44 | return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); |
| ... | ... | @@ -50,12 +50,8 @@ class ImageController extends Controller |
| 50 | 50 | $images = []; |
| 51 | 51 | |
| 52 | 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 | 57 | return $this->render('gallery', ['files' => $images]); |
| ... | ... | @@ -82,13 +78,10 @@ class ImageController extends Controller |
| 82 | 78 | $zip = new \ZipArchive(); |
| 83 | 79 | |
| 84 | 80 | $filename = dirname(dirname(__DIR__)) . '/uploads/images.zip'; |
| 85 | - $zip->open($filename, \ZipArchive::CREATE); | |
| 81 | + $zip->open($filename, \ZipArchive::OVERWRITE); | |
| 86 | 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 | 87 | $zip->close(); | ... | ... |
backend/models/UploadForm.php
| ... | ... | @@ -3,6 +3,7 @@ namespace backend\models; |
| 3 | 3 | |
| 4 | 4 | use backend\components\Helper; |
| 5 | 5 | use yii\base\Model; |
| 6 | +use yii\helpers\FileHelper; | |
| 6 | 7 | use yii\web\UploadedFile; |
| 7 | 8 | |
| 8 | 9 | class UploadForm extends Model |
| ... | ... | @@ -12,16 +13,21 @@ class UploadForm extends Model |
| 12 | 13 | public function rules() |
| 13 | 14 | { |
| 14 | 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 | 28 | if ($this->validate()) { |
| 23 | 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 | 31 | $file->saveAs($path); |
| 26 | 32 | } |
| 27 | 33 | return true; | ... | ... |