Conditions | 9 |
Paths | 104 |
Total Lines | 83 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function generate($posY, $scaleToPx) { |
||
63 | // Extracting file name from full path |
||
64 | $file = reset($this->files); |
||
65 | $onlyName = explode('.', $file->fileName); |
||
66 | if (count($onlyName) > 1) { |
||
67 | array_pop($onlyName); |
||
68 | } |
||
69 | $onlyName = implode('.', $onlyName); |
||
70 | // You can't have this chars in CSS qualifier |
||
71 | $onlyName = str_replace(['.', '#'], '_', $onlyName); |
||
72 | |||
73 | // Expanding frames. Their sizes can change due to offset |
||
74 | foreach ($this->frames as $i => $frame) { |
||
75 | $this->expandFrame($i); |
||
76 | } |
||
77 | |||
78 | // $firstFrame = reset($this->frames); |
||
79 | // $this->width = imagesx($firstFrame->gdImage) * count($this->frames); |
||
80 | // $this->height = imagesy($firstFrame->gdImage); |
||
81 | // $maxDimension = max(imagesx($firstFrame->gdImage), imagesy($firstFrame->gdImage)); |
||
82 | $maxDimension = max($this->maxWidth, $this->height); |
||
83 | |||
84 | // Recreating image - if any |
||
85 | unset($this->image); |
||
86 | $this->image = ImageContainer::create($this->width, $this->height); |
||
87 | |||
88 | $durations = []; |
||
89 | $position = 0; |
||
90 | foreach ($this->frames as $i => $frame) { |
||
91 | // $frameGdImage = $this->expandFrame($i); |
||
92 | $frameGdImage = $frame->gdImage; |
||
|
|||
93 | |||
94 | $width = imagesx($frameGdImage); |
||
95 | $height = imagesy($frameGdImage); |
||
96 | |||
97 | $this->image->copyFromGd($frameGdImage, $position, 0); |
||
98 | |||
99 | // $frame = $this->frames[$i]; |
||
100 | // Fixing duration 0 to 10 |
||
101 | $durations[$i] = ($duration = $frame->getDuration()) ? $duration : 10; |
||
102 | |||
103 | $css = "%1\$s{$onlyName}_{$i}%2\$s{background-position: -{$position}px -{$posY}px;"; |
||
104 | |||
105 | // Extra info about frame |
||
106 | $size = $frame->getSize(); |
||
107 | $offset = $frame->getOffset(); |
||
108 | $css = "/* Frame {$size->getWidth()}x{$size->getHeight()} @ ({$offset->getX()},{$offset->getY()}) duration {$frame->getDuration()} disposition {$frame->getDisposalMethod()} */" . $css; |
||
109 | |||
110 | if ($scaleToPx > 0) { |
||
111 | if ($maxDimension != $scaleToPx) { |
||
112 | $css .= "zoom: calc({$scaleToPx}/{$maxDimension});"; |
||
113 | } |
||
114 | } |
||
115 | $css .= "width: {$width}px;height: {$height}px;}\n"; |
||
116 | |||
117 | if ($i === 0) { |
||
118 | // If it's first frame - generating CSS for static image |
||
119 | $css = "%1\$s{$onlyName}%2\$s,\n" . $css; |
||
120 | } |
||
121 | |||
122 | $this->css .= $css; |
||
123 | |||
124 | $position += $width; |
||
125 | } |
||
126 | |||
127 | $totalDuration = array_sum($durations); |
||
128 | $durInSec = round($totalDuration / 100, 4); |
||
129 | |||
130 | $animation = ''; |
||
131 | $cumulative = 0; |
||
132 | $position = 0; |
||
133 | foreach ($durations as $i => $duration) { |
||
134 | $animation .= $cumulative . "%% {background-position-x: {$position}px;}\n"; |
||
135 | |||
136 | $cumulative += round($duration / $totalDuration * 100, 3); |
||
137 | $position -= imagesx($this->frames[$i]->gdImage); |
||
138 | } |
||
139 | $animation = "%1\$s{$onlyName}%2\$s {animation: {$onlyName}_animation%2\$s {$durInSec}s step-end infinite;}\n" . |
||
140 | "@keyframes {$onlyName}_animation%2\$s {\n" . |
||
141 | $animation . |
||
142 | "}"; |
||
143 | |||
144 | $this->css .= $animation; |
||
145 | } |
||
215 |