1: <?php
2: /**
3: ##DOC-SIGNATURE##
4:
5: This file is part of WideImage.
6:
7: WideImage is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or
10: (at your option) any later version.
11:
12: WideImage is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU Lesser General Public License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with WideImage; if not, write to the Free Software
19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20:
21: * @package Internal/Operations
22: **/
23:
24: namespace WideImage\Operation;
25:
26: use WideImage\WideImage;
27:
28: /**
29: * ApplyMask operation class
30: *
31: * @package Internal/Operations
32: */
33: class RoundCorners
34: {
35: /**
36: * @param \WideImage\Image $image
37: * @param int $radius
38: * @param int $color
39: * @param int $smoothness
40: * @return \WideImage\Image
41: */
42: public function execute($image, $radius, $color, $smoothness, $corners)
43: {
44: if ($smoothness < 1) {
45: $sample_ratio = 1;
46: } elseif ($smoothness > 16) {
47: $sample_ratio = 16;
48: } else {
49: $sample_ratio = $smoothness;
50: }
51:
52: $corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio);
53:
54: if ($color === null) {
55: imagepalettecopy($corner->getHandle(), $image->getHandle());
56: $bg_color = $corner->allocateColor(0, 0, 0);
57:
58: $corner->fill(0, 0, $bg_color);
59: $fg_color = $corner->allocateColor(255, 255, 255);
60: $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
61: $corner = $corner->resize($radius, $radius);
62:
63: $result = $image->asTrueColor();
64:
65: $tc = $result->getTransparentColor();
66:
67: if ($tc == -1) {
68: $tc = $result->allocateColorAlpha(255, 255, 255, 127);
69: imagecolortransparent($result->getHandle(), $tc);
70: $result->setTransparentColor($tc);
71: }
72:
73: if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
74: $result = $result->applyMask($corner, -1, -1);
75: }
76:
77: $corner = $corner->rotate(90);
78: if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
79: $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
80: }
81:
82: $corner = $corner->rotate(90);
83: if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
84: $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
85: }
86:
87: $corner = $corner->rotate(90);
88: if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
89: $result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
90: }
91:
92: return $result;
93: } else {
94: $bg_color = $color;
95:
96: $corner->fill(0, 0, $bg_color);
97: $fg_color = $corner->allocateColorAlpha(127, 127, 127, 127);
98: $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
99: $corner = $corner->resize($radius, $radius);
100:
101: $result = $image->copy();
102: if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
103: $result = $result->merge($corner, -1, -1, 100);
104: }
105:
106: $corner = $corner->rotate(90);
107: if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
108: $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
109: }
110:
111: $corner = $corner->rotate(90);
112: if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
113: $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
114: }
115:
116: $corner = $corner->rotate(90);
117: if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
118: $result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
119: }
120:
121: return $result;
122: }
123: }
124: }
125: