1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dwr\AvatarBundle\Model; |
4
|
|
|
|
5
|
|
|
abstract class Avatar { |
6
|
|
|
|
7
|
|
|
public static $WHITE = ['r' => 255, 'g' => 255, 'b' => 255]; |
8
|
|
|
public static $GREY = ['r' => 100, 'g' => 100, 'b' => 100]; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param int $size |
12
|
|
|
*/ |
13
|
|
|
public function setSize($size) |
14
|
|
|
{ |
15
|
|
|
$this->size = $size; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return int |
20
|
|
|
*/ |
21
|
|
|
public function getSize() |
22
|
|
|
{ |
23
|
|
|
return $this->size; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function render() |
30
|
|
|
{ |
31
|
|
|
return base64_encode($this->getPicture()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function getPicture() |
35
|
|
|
{ |
36
|
|
|
if($this->picture){ |
37
|
|
|
return $this->picture; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->draw(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $directory |
45
|
|
|
* @param mixed $filename |
46
|
|
|
* @return \SplFileObject |
47
|
|
|
*/ |
48
|
|
|
public function save($directory, $filename = null) |
49
|
|
|
{ |
50
|
|
|
$filename = $directory . $this->createFileName($filename); |
51
|
|
|
$file = new \SplFileObject($filename, 'x'); |
52
|
|
|
$file->fwrite(base64_decode($this->render())); |
53
|
|
|
|
54
|
|
|
return $file; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function createFileName($filename) |
58
|
|
|
{ |
59
|
|
|
if ($filename){ |
60
|
|
|
return $filename; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$filename = date('YmdHis') . uniqid() . '.jpg'; |
64
|
|
|
|
65
|
|
|
return $filename; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param resource $canvas |
70
|
|
|
* @param array $mixColor |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
protected function randomizeColor($canvas, $mixColor) |
74
|
|
|
{ |
75
|
|
|
/** |
76
|
|
|
* MixColor: |
77
|
|
|
* Mixing random colors with white (255, 255, 255) creates neutral pastels |
78
|
|
|
* by increasing the lightness while keeping the hue of the original color. |
79
|
|
|
* $mixColor = ['r' => 255, 'g' => 255, 'b' => 255]; // white |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
$red = (rand(100, 255) + $mixColor['r']) /2; |
83
|
|
|
$green = (rand(100, 255) + $mixColor['g']) /2; |
84
|
|
|
$blue = (rand(100, 255) + $mixColor['b']) /2; |
85
|
|
|
|
86
|
|
|
return imagecolorallocate($canvas, $red, $green, $blue); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Avatar |
91
|
|
|
*/ |
92
|
|
|
abstract public function getAvatar(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
abstract protected function draw(); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: