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
25 26 27 28 29
30 class WideImage_Operation_RoundCorners
31 {
32 33 34 35 36 37 38
39 function execute($image, $radius, $color, $smoothness, $corners)
40 {
41 if ($smoothness < 1)
42 $sample_ratio = 1;
43 elseif ($smoothness > 16)
44 $sample_ratio = 16;
45 else
46 $sample_ratio = $smoothness;
47
48 $corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio);
49 if ($color === null)
50 {
51 imagepalettecopy($corner->getHandle(), $image->getHandle());
52 $bg_color = $corner->allocateColor(0, 0, 0);
53
54 $corner->fill(0, 0, $bg_color);
55 $fg_color = $corner->allocateColor(255, 255, 255);
56 $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
57 $corner = $corner->resize($radius, $radius);
58
59 $result = $image->asTrueColor();
60
61 $tc = $result->getTransparentColor();
62 if ($tc == -1)
63 {
64 $tc = $result->allocateColorAlpha(255, 255, 255, 127);
65 imagecolortransparent($result->getHandle(), $tc);
66 $result->setTransparentColor($tc);
67 }
68
69 if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP)
70 $result = $result->applyMask($corner, -1, -1);
71
72 $corner = $corner->rotate(90);
73 if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT)
74 $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
75
76 $corner = $corner->rotate(90);
77 if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM)
78 $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
79
80 $corner = $corner->rotate(90);
81 if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM)
82 $result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
83
84 return $result;
85 }
86 else
87 {
88 $bg_color = $color;
89
90 $corner->fill(0, 0, $bg_color);
91 $fg_color = $corner->allocateColorAlpha(127, 127, 127, 127);
92 $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
93 $corner = $corner->resize($radius, $radius);
94
95 $result = $image->copy();
96 if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP)
97 $result = $result->merge($corner, -1, -1, 100);
98
99 $corner = $corner->rotate(90);
100 if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT)
101 $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
102
103 $corner = $corner->rotate(90);
104 if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM)
105 $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
106
107 $corner = $corner->rotate(90);
108 if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM)
109 $result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
110
111 return $result;
112 }
113 }
114 }
115