1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: |
|
23: |
|
24: | namespace WideImage\Operation;
|
25: |
|
26: | use WideImage\TrueColorImage;
|
27: |
|
28: | |
29: | |
30: | |
31: | |
32: |
|
33: | class GetMask
|
34: | {
|
35: | |
36: | |
37: | |
38: | |
39: | |
40: |
|
41: | public function execute($image)
|
42: | {
|
43: | $width = $image->getWidth();
|
44: | $height = $image->getHeight();
|
45: |
|
46: | $mask = TrueColorImage::create($width, $height);
|
47: | $mask->setTransparentColor(-1);
|
48: | $mask->alphaBlending(false);
|
49: | $mask->saveAlpha(false);
|
50: |
|
51: | for ($i = 0; $i <= 255; $i++) {
|
52: | $greyscale[$i] = ImageColorAllocate($mask->getHandle(), $i, $i, $i);
|
53: | }
|
54: |
|
55: | imagefilledrectangle($mask->getHandle(), 0, 0, $width, $height, $greyscale[255]);
|
56: |
|
57: | $transparentColor = $image->getTransparentColor();
|
58: | $alphaToGreyRatio = 255 / 127;
|
59: |
|
60: | for ($x = 0; $x < $width; $x++) {
|
61: | for ($y = 0; $y < $height; $y++) {
|
62: | $color = $image->getColorAt($x, $y);
|
63: |
|
64: | if ($color == $transparentColor) {
|
65: | $rgba['alpha'] = 127;
|
66: | } else {
|
67: | $rgba = $image->getColorRGB($color);
|
68: | }
|
69: |
|
70: | imagesetpixel($mask->getHandle(), $x, $y, $greyscale[255 - round($rgba['alpha'] * $alphaToGreyRatio)]);
|
71: | }
|
72: | }
|
73: |
|
74: | return $mask;
|
75: | }
|
76: | }
|
77: | |