function RoundedImageCorners(&$gdimg, $radius_x, $radius_y)
{
    if ($gdimg_cornermask_triple = ImageCreateTrueColor($radius_x * 6, $radius_y * 6)) {
        if ($gdimg_cornermask = ImageCreateTrueColor(ImageSX($gdimg), ImageSY($gdimg))) {
            $color_transparent = ImageColorAllocate($gdimg_cornermask_triple, 255, 255, 255);
            ImageFilledEllipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
            ImageFilledRectangle($gdimg_cornermask, 0, 0, ImageSX($gdimg), ImageSY($gdimg), $color_transparent);
            ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, 0, $radius_x, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
            ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, ImageSY($gdimg) - $radius_y, $radius_x, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
            ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, ImageSX($gdimg) - $radius_x, ImageSY($gdimg) - $radius_y, $radius_x * 3, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
            ImageCopyResampled($gdimg_cornermask, $gdimg_cornermask_triple, ImageSX($gdimg) - $radius_x, 0, $radius_x * 3, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
            ApplyMask($gdimg_cornermask, $gdimg);
            ImageDestroy($gdimg_cornermask);
            $DebugMessage = 'RoundedImageCorners(' . $radius_x . ', ' . $radius_y . ') succeeded' . __FILE__ . __LINE__;
            return true;
        } else {
            $DebugMessage = 'FAILED: $gdimg_cornermask = ImageColorAllocate(' . ImageSX($gdimg) . ', ' . ImageSY($gdimg) . ')' . __FILE__ . __LINE__;
        }
        ImageDestroy($gdimg_cornermask_triple);
    } else {
        $DebugMessage = 'FAILED: $gdimg_cornermask_triple = ImageColorAllocate(' . $radius_x * 6 . ', ' . $radius_y * 6 . ')' . __FILE__ . __LINE__;
    }
    return false;
}
Пример #2
0
function ApplyRoundedCorners(&$image, $radius = 0)
{
    if ($radius <= 0) {
        $radius = Round(ImageSX($image) / 4);
    }
    /* Generate mask at twice desired resolution and downsample afterwards for
     * easy antialiasing mask is generated as a white double-size elipse on a
     * triple-size black background and copy-paste-resampled onto a
     * correct-size mask image as 4 corners due to errors when the entire mask
     * is resampled at once (gray edges).
     * */
    $x = ImageSX($image);
    $y = ImageSY($image);
    $cornermask = ImageCreateTrueColor($x * 2, $y * 2);
    $cornermask3 = ImageCreateTrueColor($radius * 6, $radius * 6);
    $color_transparent = ImageColorAllocate($cornermask3, 255, 255, 255);
    ImageFilledEllipse($cornermask3, $radius * 3, $radius * 3, $radius * 4, $radius * 4, $color_transparent);
    ImageFilledRectangle($cornermask, 0, 0, $x * 2, $y * 2, $color_transparent);
    ImageCopyResampled($cornermask, $cornermask3, 0, 0, $radius, $radius, $radius, $radius, $radius * 2, $radius * 2);
    ImageCopyResampled($cornermask, $cornermask3, 0, $y * 2 - $radius, $radius, $radius * 3, $radius, $radius, $radius * 2, $radius * 2);
    ImageCopyResampled($cornermask, $cornermask3, $x * 2 - $radius, $y * 2 - $radius, $radius * 3, $radius * 3, $radius, $radius, $radius * 2, $radius * 2);
    ImageCopyResampled($cornermask, $cornermask3, $x * 2 - $radius, 0, $radius * 3, $radius, $radius, $radius, $radius * 2, $radius * 2);
    ImageDestroy($cornermask3);
    ApplyMask($image, $cornermask);
    ImageDestroy($cornermask);
}