1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Limsum\ColorBlock; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Response; |
6
|
|
|
|
7
|
|
|
class ColorBlockPng extends AbstractColorBlockImage |
8
|
|
|
{ |
9
|
1 |
|
public function image(): \Illuminate\Http\Response |
10
|
|
|
{ |
11
|
1 |
|
if (!extension_loaded('gd')) { |
12
|
|
|
throw new \Exception('FG extension not loaded.'); |
13
|
|
|
} |
14
|
|
|
|
15
|
1 |
|
$image = imagecreatetruecolor((int) $this->with, (int) $this->height); |
16
|
|
|
|
17
|
1 |
|
$color = $this->hexColorAllocate($image, $this->color); |
18
|
|
|
|
19
|
1 |
|
imagefilledrectangle($image, 0, 0, (int) $this->with, (int) $this->height, $color); |
20
|
|
|
|
21
|
1 |
|
ob_start(); |
22
|
1 |
|
$this->makeImage($image); |
23
|
1 |
|
$imageData = ob_get_contents(); |
24
|
1 |
|
ob_end_clean(); |
25
|
|
|
|
26
|
1 |
|
imagedestroy($image); |
27
|
|
|
|
28
|
1 |
|
return Response::make($imageData, 200, [ |
29
|
1 |
|
'Content-Type' => $this->mimeType(), |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $image |
35
|
|
|
* @param $hex |
36
|
|
|
* |
37
|
|
|
* @return bool|int |
38
|
|
|
*/ |
39
|
1 |
|
protected function hexColorAllocate($image, $hex): bool|int |
40
|
|
|
{ |
41
|
1 |
|
$hex = ltrim($hex, '#'); |
42
|
1 |
|
$r = (int) hexdec(substr($hex, 0, 2)); |
43
|
1 |
|
$g = (int) hexdec(substr($hex, 2, 2)); |
44
|
1 |
|
$b = (int) hexdec(substr($hex, 4, 2)); |
45
|
|
|
|
46
|
1 |
|
return imagecolorallocate($image, $r, $g, $b); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $image |
51
|
|
|
*/ |
52
|
1 |
|
protected function makeImage($image) |
53
|
|
|
{ |
54
|
1 |
|
imagepng($image); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
1 |
|
protected function mimeType():string |
61
|
|
|
{ |
62
|
1 |
|
return 'image/png'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|