Conditions | 5 |
Paths | 9 |
Total Lines | 53 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
113 | private function createImage($code, $width = 150, $height = 50) |
||
114 | { |
||
115 | // Создаем пустое изображение |
||
116 | $img = \imagecreatetruecolor($width, $height); |
||
117 | |||
118 | $background_color = [\mt_rand(200, 255), \mt_rand(200, 255), \mt_rand(200, 255)]; |
||
119 | // Заливаем фон белым цветом |
||
120 | $background = \imagecolorallocate($img, $background_color[0], $background_color[1], $background_color[2]); |
||
121 | \imagefill($img, 0, 0, $background); |
||
122 | |||
123 | |||
124 | // Накладываем защитный код |
||
125 | $x = 0; |
||
126 | $letters = \str_split($code); |
||
127 | $figures = [50, 70, 90, 110, 130, 150, 170, 190, 210]; |
||
128 | |||
129 | foreach ($letters as $letter) { |
||
130 | //Ориентир |
||
131 | $h = 1; |
||
132 | //Рисуем |
||
133 | $color = \imagecolorallocatealpha( |
||
134 | $img, |
||
135 | $figures[\rand(0, \count($figures) - 1)], |
||
136 | $figures[\rand(0, \count($figures) - 1)], |
||
137 | $figures[\rand(0, \count($figures) - 1)], |
||
138 | rand(10, 30) |
||
139 | ); |
||
140 | |||
141 | |||
142 | // Формируем координаты для вывода символа |
||
143 | if (empty($x)) { |
||
144 | $x = (int) ($width * 0.08); |
||
145 | } else { |
||
146 | $x = (int) ($x + ($width * 0.8) / \count($letters) + \rand(0, (int) ($width * 0.01))); |
||
147 | } |
||
148 | |||
149 | if ($h == rand(1, 2)) { |
||
150 | $y = (int) ((($height * 1) / 4) + \rand(0, (int) ($height * 0.1))); |
||
151 | } else { |
||
152 | $y = (int) ((($height * 1) / 4) - \rand(0, (int) ($height * 0.1))); |
||
153 | } |
||
154 | |||
155 | |||
156 | // Изменяем регистр символа |
||
157 | if ($h == \rand(0, 1)) { |
||
158 | $letter = \strtoupper($letter); |
||
159 | } |
||
160 | // Выводим символ на изображение |
||
161 | \imagestring($img, 6, $x, $y, $letter, $color); |
||
162 | $x++; |
||
163 | } |
||
164 | |||
165 | return $img; |
||
166 | } |
||
177 |