1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dwr\AvatarBundle\Model; |
4
|
|
|
|
5
|
|
|
class ProfileAvatar extends Avatar { |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
const PROFILE_IMAGE_RESOLUTION = 5; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
protected $size; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $picture; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $canvas; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $size |
27
|
|
|
*/ |
28
|
|
|
public function __construct($size) |
29
|
|
|
{ |
30
|
|
|
$this->size = $size; |
31
|
|
|
$this->canvas = imagecreatetruecolor($size, $size); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Avatar |
36
|
|
|
*/ |
37
|
|
|
public function getAvatar() |
38
|
|
|
{ |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function draw() |
43
|
|
|
{ |
44
|
|
|
$this->createBackground(); |
45
|
|
|
$this->createProfileImage(); |
46
|
|
|
|
47
|
|
|
ob_start(); |
48
|
|
|
imagejpeg($this->canvas); |
49
|
|
|
$image = ob_get_contents(); |
50
|
|
|
ob_end_clean(); |
51
|
|
|
|
52
|
|
|
$this->picture = $image; |
53
|
|
|
|
54
|
|
|
return $this->picture; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function createBackground() |
58
|
|
|
{ |
59
|
|
|
$backgroundColor = $this->randomizeColor($this->canvas, Avatar::$WHITE); |
|
|
|
|
60
|
|
|
imagefill($this->canvas, 0, 0, $backgroundColor); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function createProfileImage() |
64
|
|
|
{ |
65
|
|
|
$imageMapFields = $this->randomFillIn(); |
66
|
|
|
$imageCellSize = $this->getSize()/self::PROFILE_IMAGE_RESOLUTION; |
67
|
|
|
$imageColor = $this->randomizeColor($this->canvas, Avatar::$GREY); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$coordinates = array( |
70
|
|
|
'x1' => 0, 'y1' => 0, |
71
|
|
|
'x2' => $imageCellSize, 'y2' => $imageCellSize, |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$counter = 0; |
75
|
|
|
foreach ($imageMapFields as $field) { |
76
|
|
|
if ($counter % 5 !== 0) { |
77
|
|
|
$coordinates['x1'] += $imageCellSize; |
78
|
|
|
$coordinates['x2'] += $imageCellSize; |
79
|
|
|
$this->filledRectangle($coordinates, $imageColor, $field); |
80
|
|
|
} else { |
81
|
|
|
if ($counter > 0) { |
82
|
|
|
$coordinates = array( |
83
|
|
|
'x1' => 0, 'y1' => $coordinates['y1'] += $imageCellSize, |
84
|
|
|
'x2' => $imageCellSize, 'y2' => $coordinates['y2'] += $imageCellSize, |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
$this->filledRectangle($coordinates, $imageColor, $field); |
88
|
|
|
} |
89
|
|
|
$counter++; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function filledRectangle(array $coordinates, $color, $field) |
94
|
|
|
{ |
95
|
|
|
if ($field) { |
96
|
|
|
imagefilledrectangle( |
97
|
|
|
$this->canvas, |
98
|
|
|
$coordinates['x1'], |
99
|
|
|
$coordinates['y1'], |
100
|
|
|
$coordinates['x2'], |
101
|
|
|
$coordinates['y2'], |
102
|
|
|
$color |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private function randomFillIn() |
111
|
|
|
{ |
112
|
|
|
/** |
113
|
|
|
* Output: |
114
|
|
|
* $random = array( |
115
|
|
|
* array((bool)rand(0,1), (bool)rand(0,1)), |
116
|
|
|
* array((bool)rand(0,1), (bool)rand(0,1)), |
117
|
|
|
* array((bool)rand(0,1), (bool)rand(0,1)) |
118
|
|
|
* array((bool)rand(0,1), (bool)rand(0,1)) |
119
|
|
|
* array((bool)rand(0,1), (bool)rand(0,1)) |
120
|
|
|
* ); |
121
|
|
|
*/ |
122
|
|
|
$random = array(); |
123
|
|
|
for ($i = 0; $i < self::PROFILE_IMAGE_RESOLUTION; $i++) { |
124
|
|
|
for ($j = 0; $j < 2; $j++) { |
125
|
|
|
$random[$i][$j] = (bool)rand(0,1); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$filledInFields = array( |
130
|
|
|
$random[0][0], $random[0][1], !$random[0][1], $random[0][1], $random[0][0], |
131
|
|
|
$random[1][0], $random[1][1], !$random[1][1], $random[1][1], $random[1][0], |
132
|
|
|
$random[2][0], $random[2][1], !$random[2][1], $random[2][1], $random[2][0], |
133
|
|
|
$random[3][0], $random[3][1], !$random[3][1], $random[3][1], $random[3][0], |
134
|
|
|
$random[4][0], $random[4][1], !$random[4][1], $random[4][1], $random[4][0], |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $filledInFields; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..