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:
<?php
namespace MvcCore\Ext\Tools\Images\Gds;
class RoundCorners {
public static function Process ($img, $x, $y) {
$x *= 2;
$y *= 2;
$w = imagesx($img);
$h = imagesy($img);
$maskImg = imagecreatetruecolor($w, $h);
$maskWhite = imagecolorallocate($maskImg, 255, 255, 255);
$xHalf = $x / 2;
$yHalf = $y / 2;
$fix = 1;
imagefilledellipse($maskImg, $xHalf, $yHalf, $x, $y, $maskWhite);
imagefilledellipse($maskImg, $w - $xHalf - $fix, $yHalf, $x, $y, $maskWhite);
imagefilledellipse($maskImg, $xHalf, $h - $yHalf - $fix, $x, $y, $maskWhite);
imagefilledellipse($maskImg, $w - $xHalf - $fix, $h - $yHalf - $fix, $x, $y, $maskWhite);
$points = [
$xHalf, 0,
$w - $xHalf - $fix, 0,
$w, $yHalf,
$w, $h - $yHalf - $fix,
$w - $xHalf - $fix, $h,
$xHalf, $h,
0, $h - $yHalf - $fix,
0, $yHalf
];
imagefilledpolygon($maskImg, $points, 8, $maskWhite);
$newImg = imagecreatetruecolor($w, $h);
imagesavealpha($newImg, TRUE);
imagefill(
$newImg, 0, 0,
imagecolorallocatealpha($newImg, 0, 0, 0, 127)
);
imagecopy($newImg, $img, $xHalf, 0, $xHalf, 0, $w - $x, $yHalf);
imagecopy($newImg, $img, 0, $yHalf, 0, $yHalf, $w, $h - $y);
imagecopy($newImg, $img, $xHalf, $h - $yHalf, $xHalf, $h - $yHalf, $w - $x, $yHalf);
self::copyCornerWithMask($img, $maskImg, $newImg, 0, 0, $xHalf, $yHalf);
self::copyCornerWithMask($img, $maskImg, $newImg, $w - $xHalf, 0, $w, $yHalf);
self::copyCornerWithMask($img, $maskImg, $newImg, 0, $h - $yHalf, $xHalf, $h);
self::copyCornerWithMask($img, $maskImg, $newImg, $w - $xHalf, $h - $yHalf, $w, $h);
imagedestroy($img);
$img = $newImg;
imagedestroy($maskImg);
}
protected static function copyCornerWithMask (
$img, $maskImg, $newImg,
$x, $y, $w, $h
) {
for ($i = $x; $i < $w; $i += 1) {
for ($j = $y; $j < $h; $j += 1) {
$rgba = imagecolorsforindex(
$maskImg, imagecolorat($maskImg, $i, $j)
);
$whiteRatio = (
$rgba['red'] + $rgba['green'] + $rgba['blue']
) / 765;
$alpha = 127 - round(127 * $whiteRatio);
$color = imagecolorsforindex(
$img, imagecolorat($img, $i, $j)
);
imagesetpixel(
$newImg,
$i, $j,
imagecolorallocatealpha(
$newImg,
$color['red'],
$color['green'],
$color['blue'],
$alpha
)
);
}
}
}
}