Test Failed
Push — trunk ( dc8fe4...b76cfb )
by SuperNova.WS
10:59
created

Spritify::tryReadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/** Created by Gorlum 08.01.2024 19:21 */
4
5
/** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */
6
7
namespace Tools;
8
9
use Exception;
10
use ImageFile;
11
12
define('INSIDE', true);
13
14
require_once __DIR__ . '/../../classes/debug.php';
15
require_once __DIR__ . '/Sprite.php';
16
17
class Spritify {
18
  /**
19
   * @param string $dirIn       Input directory
20
   * @param string $dirOut      Output directory
21
   * @param string $outName     Name to use as CSS/PNG files
22
   * @param string $cssPrefix   Prefix to CSS qualifier. Default '#'
23
   * @param string $cssSuffix   Suffix to CSS qualifier. Default ''
24
   * @param int    $scaleToPx   Pixel size to scale largest side of sprite to for scale(). Default 0 - no scaling
25
   * @param string $relativeUrl Url relative to root where PNG sprite will reside - '/design/images/' by default
26
   *
27
   * @return void
28
   * @throws Exception
29
   */
30
  public static function go($dirIn, $dirOut, $outName, $cssPrefix = '#', $cssSuffix = '', $scaleToPx = 0, $relativeUrl = '/design/images/') {
31
    print "Processing folder/filter `$dirIn` to `$outName`\n";
32
33
//    $images = self::propagateImagesScanDir($dirIn);
34
    $images = self::propagateImagesGlob($dirIn);
35
    if (empty($images)) {
36
      print "No images found to process\n\n";
37
38
      return;
39
    }
40
41
    $sprite = Sprite::createGridSquare($images, Sprite::LAYOUT_SQUARE);
42
43
    $finalImage = $sprite->generate($scaleToPx);
44
45
    // Checking if output directory exists and creating one - if not
46
    if (!is_dir($dirOut) && !mkdir($dirOut, 0777, true)) {
47
      throw new Exception("Can't create output directory {$dirOut}\n");
48
    }
49
    // Saving PNG
50
    $finalImage->savePng($dirOut . $outName . '.png');
51
    file_put_contents(
52
      $dirOut . $outName . '.css',
53
      sprintf($sprite->generateCss($scaleToPx), $cssPrefix, $cssSuffix, $outName, $relativeUrl)
54
    );
55
56
    print "Folder/filter processed\n\n";
57
  }
58
59
  /**
60
   * Propagates array with image files from glob pattern (as in OS - with * and ?)
61
   *
62
   * @param string $dirIn
63
   *
64
   * @return ImageFile[]|false
65
   */
66
  protected static function propagateImagesGlob($dirIn) {
67
    if (($fullPathList = glob($dirIn, GLOB_BRACE)) === false) {
68
      print "Can't find directory `$dirIn`\n";
69
70
      return false;
71
    }
72
73
    /** @var ImageFile[] $images */
74
    $images = [];
75
    foreach ($fullPathList as $fullPath) {
76
      $fullPath = str_replace('\\', '/', realpath($fullPath));
77
      $images   = self::tryReadFile($fullPath, $images);
78
    }
79
80
    return $images;
81
  }
82
83
  /**
84
   * Propagates array with image files from folder
85
   *
86
   * @param string $dirIn
87
   *
88
   * @return ImageFile[]|false
89
   */
90
  protected static function propagateImagesScanDir($dirIn) {
91
    $dirRealPath = str_replace('\\', '/', realpath($dirIn ?: './'));
92
    if (!file_exists($dirRealPath) || !is_dir($dirRealPath)) {
93
      print "Can't find directory `$dirIn`\n";
94
95
      return false;
96
    }
97
98
    /** @var ImageFile[] $images */
99
    $images = [];
100
101
    foreach (scandir($dirRealPath) as $fileNameOnly) {
102
      if (in_array($fileNameOnly, ['.', '..',]) || is_dir($fullPath = "$dirRealPath/$fileNameOnly")) {
103
        continue;
104
      }
105
      $images = self::tryReadFile($fullPath, $images);
106
    }
107
108
    return $images;
109
  }
110
111
  /**
112
   * @param             $fullPath
113
   * @param ImageFile[] $images
114
   *
115
   * @return ImageFile[]
116
   */
117
  protected static function tryReadFile($fullPath, $images) {
118
    if ($image = ImageFile::read($fullPath)) {
119
      $images[] = $image;
120
      print "`$fullPath` read OK\n";
121
    } else {
122
      print "ERROR: Can't read file `$fullPath`\n";
123
    }
124
125
    return $images;
126
  }
127
128
}
129