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

Sprite   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 127
rs 10
c 1
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A imageReset() 0 6 2
A renderLines() 0 8 2
A __construct() 0 6 2
A addToGrid() 0 8 3
A generateCss() 0 9 3
A generate() 0 15 2
A createGridSquare() 0 10 2
1
<?php
2
/** Created by Gorlum 08.01.2024 19:57 */
3
4
namespace Tools;
5
6
require_once __DIR__ . '/SpriteLine.php';
7
require_once __DIR__ . '/ImageFile.php';
8
require_once __DIR__ . '/ImageContainer.php';
9
10
use ImageFile;
11
12
class Sprite {
13
  const LAYOUT_LINE = 0;
14
  const LAYOUT_COLUMN = 1;
15
  const LAYOUT_SQUARE = 'square';
16
  const LAYOUT_BTREE = 'btree';
17
18
  /** @var SpriteLine[] $lines */
19
  public $lines = [];
20
21
  protected $gridSize = 0;
22
23
  protected $lineIndex = 0;
24
  protected $columnIndex = 0;
25
26
  protected $height = 0;
27
  protected $width = 0;
28
29
  /** @var ImageContainer|null $image */
30
  public $image = null;
31
32
  public function __construct($gridSize = 0) {
33
    if ($gridSize < 1) {
34
      $gridSize = 1;
35
    }
36
37
    $this->gridSize = $gridSize;
38
  }
39
40
  /**
41
   * @param ImageFile[] $images
42
   *
43
   * @return static
44
   *
45
   * @noinspection PhpUnusedParameterInspection
46
   */
47
  public static function createGridSquare($images, $layout) {
48
    $gridSize = ceil(sqrt(count($images)));
49
    usort($images, function (ImageFile $a, ImageFile $b) { return $b->height - $a->height; });
50
51
    $sprite = new static($gridSize);
52
    foreach ($images as $image) {
53
      $sprite->addToGrid($image);
54
    }
55
56
    return $sprite;
57
  }
58
59
60
  /**
61
   * @param ImageFile $imageFile
62
   *
63
   * @return void
64
   */
65
  public function addToGrid($imageFile) {
66
    // This is first image in row
67
    if (empty($this->lines[$this->lineIndex])) {
68
      $this->lines[$this->lineIndex] = new SpriteLine();
69
    }
70
    $this->lines[$this->lineIndex]->addImage($imageFile);
71
    if ($this->lines[$this->lineIndex]->getImageCount() >= $this->gridSize) {
72
      $this->lineIndex++;
73
    }
74
  }
75
76
  /**
77
   * @return void
78
   */
79
  public function imageReset() {
80
    if (!empty($this->image)) {
81
      unset($this->image);
82
    }
83
84
    $this->image = ImageContainer::create($this->width, $this->height);
85
  }
86
87
  /**
88
   * @param $scaleToPx
89
   */
90
  public function renderLines($scaleToPx) {
91
    $this->width = $this->height = 0;
92
    // Generating lines and calculating line sizes
93
    foreach ($this->lines as $line) {
94
      $line->generate($this->height, $scaleToPx);
95
96
      $this->height += $line->height;
97
      $this->width  = max($this->width, $line->width);
98
99
      // $line->image2->savePng($dirOut . count($breakpoints) . '.png'); // TODO remove debug
100
    }
101
  }
102
103
  /**
104
   * @param int $scaleToPx
105
   *
106
   * @return ImageContainer|null
107
   */
108
  public function generate($scaleToPx) {
109
    $this->renderLines($scaleToPx);
110
111
    // Recreating main sprite image with new width and height
112
    $this->imageReset();
113
114
    // Generating final sprite
115
    $position = 0;
116
    foreach ($this->lines as $line) {
117
      $this->image->copyFrom($line->image, 0, $position);
0 ignored issues
show
Bug introduced by
It seems like $line->image can also be of type null; however, parameter $anImage of Tools\ImageContainer::copyFrom() does only seem to accept Tools\ImageContainer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
      $this->image->copyFrom(/** @scrutinizer ignore-type */ $line->image, 0, $position);
Loading history...
118
119
      $position += $line->height;
120
    }
121
122
    return $this->image;
123
  }
124
125
  /**
126
   * @param $scaleToPx
127
   *
128
   * @return string %3$s - $outName, %4$s - $relativeUrl
129
   */
130
  public function generateCss($scaleToPx) {
131
    $css = '';
132
    foreach ($this->lines as $line) {
133
      $css .= $line->css;
134
    }
135
136
    return ".%3\$s{background-image: url('%4\$s%3\$s.png');display: inline-block;" .
137
      ($scaleToPx > 0 ? "transform-origin: top left;" : "") .
138
      "}\n" . $css;
139
  }
140
141
}
142