forked from PUGX/badge-poser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageCreator.php
229 lines (196 loc) · 6.25 KB
/
ImageCreator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
* This file is part of the badge-poser package
*
* (c) Giulio De Donato <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PUGX\BadgeBundle\Service;
use Symfony\Bridge\Monolog\Logger;
use PUGX\BadgeBundle\Exception\InvalidArgumentException;
use PUGX\BadgeBundle\Event\PackageEvent;
class ImageCreator implements ImageCreatorInterface
{
private $logger;
protected $dispatcher;
protected $imageNames = array('empty' => 'empty.png', 'downloads' => 'downloads.png', 'stable' => 'stable.png', 'unstable' => 'unstable.png');
protected $imagePath;
protected $fontPath;
protected $defaultFont;
protected $defaultImage;
public function __construct(Logger $logger, $fontPath, $imagePath, $defaultFont = 'DroidSans.ttf', $defaultImage = null)
{
$this->logger = $logger;
$this->fontPath = $fontPath;
$this->imagePath = $imagePath;
if (!$defaultImage) {
$this->defaultImage = $this->imageNames['empty'];
}
$this->defaultFont = $defaultFont;
}
/**
* This function transform a number to a float value or raise an Exception.
*
* @param mixed $number
*
* @return int
* @throws \PUGX\BadgeBundle\Exception\InvalidArgumentException
*/
private function normalizeNumber($number)
{
if (!is_numeric($number)) {
throw new InvalidArgumentException('Number expected');
}
$number = floatval($number);
if ($number < 0) {
throw new InvalidArgumentException('The number expected was >= 0');
}
// avoid division by 0
if ($number == 0) {
$number = 1;
}
return $number;
}
/**
* Stream the output.
*
* @param resource $image
*
* @return Boolean
*/
public function streamRawImageData($image)
{
return imagepng($image);
}
/**
* Destroy the resource.
*
* @param $image
*
* @return Boolean
*/
public function destroyImage($image)
{
return imagedestroy($image);
}
/**
* Function that should return a human readable number in a maximum number of chars.
*
* @param int $number
* @param int $maxChar
*
* @return string
* @throws \PUGX\BadgeBundle\Exception\InvalidArgumentException
*/
public function transformNumberToReadableFormat($number, $maxChar = 6)
{
$defaultFormatter = '%.0f %s';
$dimensions = array(
'bb' => 1000000000000,
'mm' => 1000000000,
'm' => 1000000,
'k' => 1000,
' ' => 1,
);
$number = $this->normalizeNumber($number);
foreach ($dimensions as $suffix => $key) {
if ($number >= $key) {
$number = $number / $key;
// 2 is strlen(' ' . '.'); space and dot
$floatPointNumber = $maxChar - strlen($suffix) - 2 - strlen(intval($number));
$formatter = $defaultFormatter;
$decimal_part = $number - floor($number);
if ($decimal_part > 0 && $floatPointNumber > 0) {
$formatter = '%.' . $floatPointNumber . 'f %s';
}
$readable = sprintf($formatter, $number, $suffix);
$readable = str_pad($readable, $maxChar, ' ', STR_PAD_LEFT);
return $readable;
}
}
throw new InvalidArgumentException(sprintf('impossible to transform to readable number[%s] with [%d] chars', $number, $maxChar));
}
/**
* Add a shadowed text to an Image.
*
* @param resource $image
* @param string $text
* @param int $x
* @param int $y
* @param float $size
* @param string $font
* @param Boolean $withShadow
* @param int $angle
*/
private function addShadowedText($image, $text, $x = 3, $y = 13, $size = 8.5, $font = null, $withShadow = true, $angle = 0)
{
if (null === $font) {
$font = $this->fontPath . DIRECTORY_SEPARATOR . $this->defaultFont;
}
$white = imagecolorallocate($image, 255, 255, 250);
$black = imagecolorallocate($image, 0, 0, 0);
if ($withShadow) {
$imageArray = imagettftext($image, $size, $angle, $x + 1, $y + 1, $black, $font, $text);
}
$imageArray = imagettftext($image, $size, $angle, $x, $y, $white, $font, $text);
}
/**
* Create the image resource, with Blending and Alpha.
*
* @param string $imagePath
*
* @return resource
*/
private function createImage($imagePath)
{
$image = imagecreatefrompng($imagePath);
imageAlphaBlending($image, true);
imageSaveAlpha($image, true);
return $image;
}
/**
* Create the 'downloads' image with the standard Font and standard Image.
*
* @param string $value
*
* @return resource
*/
public function createDownloadsImage($value)
{
$imagePath = $this->imagePath . DIRECTORY_SEPARATOR . $this->imageNames['downloads'];
$image = $this->createImage($imagePath);
$value = $this->transformNumberToReadableFormat($value);
$this->addShadowedText($image, $value, 64, 13.5);
return $image;
}
/**
* Create the 'stable' image with the standard Font and standard Image.
*
* @param string $value
*
* @return resource
*/
public function createStableImage($value)
{
$imagePath = $this->imagePath . DIRECTORY_SEPARATOR . $this->imageNames['stable'];
$image = $this->createImage($imagePath);
$this->addShadowedText($image, $value, 59, 13.5);
return $image;
}
/**
* Create the 'stable' image with the standard Font and standard Image.
*
* @param string $value
*
* @return resource
*/
public function createUnstableImage($value = '@dev')
{
$imagePath = $this->imagePath . DIRECTORY_SEPARATOR . $this->imageNames['unstable'];
$image = $this->createImage($imagePath);
$this->addShadowedText($image, $value, 51, 12, 7);
return $image;
}
}