Blame view

backend/controllers/ImageController.php 3.04 KB
01ebf78c   Administrator   Initial commit
1
2
3
4
5
6
7
8
  <?php
  
  namespace backend\controllers;
  
  use backend\components\croppers\CropContext;
  use backend\components\croppers\CropFactory;
  
  use Yii;
f9404a23   Administrator   Start gallery
9
  use yii\helpers\FileHelper;
3ff61e27   Administrator   Adding basic gallery
10
  use yii\helpers\StringHelper;
01ebf78c   Administrator   Initial commit
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  use yii\web\Controller;
  use backend\models\UploadForm;
  use yii\web\UploadedFile;
  
  class ImageController extends Controller
  {
      public function actionIndex()
      {
          $request = Yii::$app->request;
          $model = new UploadForm();
          $ukrSeeds = Yii::$app->params['ukrSeeds'];
  
          if ($request->isPost) {
              $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
              $model->upload();
  
              $crop_id = $request->post('crop_id');
  
              $cropFactory = CropFactory::getInstance();
bfa22e8e   Administrator   Adding resizing a...
30
              $minHeight = $cropFactory->findMinHeight();
01ebf78c   Administrator   Initial commit
31
32
33
34
35
              $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);
bfa22e8e   Administrator   Adding resizing a...
36
37
38
39
                  $image = $cropContext->cropImage($image, $path);
                  $box = $image->getSize()->heighten($minHeight);
                  $image->resize($box)
                      ->save(dirname(dirname(__DIR__)) . '/uploads/' . $file->baseName . '-resized' . '.' . $file->extension);
01ebf78c   Administrator   Initial commit
40
41
42
43
44
              }
          }
          return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]);
      }
  
f9404a23   Administrator   Start gallery
45
46
47
      public function actionGallery()
      {
          $files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
bfa22e8e   Administrator   Adding resizing a...
48
49
50
51
52
53
54
55
56
57
          $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);
              }
3ff61e27   Administrator   Adding basic gallery
58
          }
bfa22e8e   Administrator   Adding resizing a...
59
          return $this->render('gallery', ['files' => $images]);
f9404a23   Administrator   Start gallery
60
61
      }
  
01ebf78c   Administrator   Initial commit
62
63
64
65
66
67
68
69
70
71
72
73
74
75
      public function actionGetParams($crop_id)
      {
          if (Yii::$app->request->isAjax) {
  
              $cropFactory = CropFactory::getInstance();
  
              $res = [
                  'body'    => $cropFactory->getCrop($crop_id)->getConstants(),
                  'success' => true,
              ];
  
              return json_encode($res);
          }
      }
bfa22e8e   Administrator   Adding resizing a...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  
      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);
      }
01ebf78c   Administrator   Initial commit
97
  }