当两个图像都具有Alpha值

时间:2015-04-23 10:14:12

标签: php image gd

我正在尝试为图像添加带圆角的边框,并且我已经让外边的角落正常工作了,但我对内部角色有一些问题。

当前方法在右侧产生输出,我希望它看起来像左侧。

enter image description here

private function addInnerRoundCorners($radius, $borderColour, $borderWidth){


    $radius = max(array(5, $radius));

    $quality = ($radius <= 5 ? 5 : 10);

    $newWidth = $this->getOverlayWidth() * $quality;
    $newHeight = $this->getOverlayHeight() * $quality;

    $tmpImage = imagecreatetruecolor($newWidth, $newHeight);


    $transparency = imagecolorallocatealpha($tmpImage, 0, 0, 0, 127);
    imagefill($tmpImage, 0, 0, $transparency);
    imagecopyresampled($tmpImage, $this->getImage(), 0, 0, 0, 0, $newWidth, $newHeight, $this->getOverlayWidth(), $this->getOverlayHeight());
    imagealphablending($tmpImage, false);
    imagesavealpha($tmpImage, true);

    $newBorderWidth = $borderWidth * $quality;
    $newRadius = $radius * $quality;



    // Top Left
    $corner = $this->getInnerCornerShape(0, $newRadius, $borderColour);     
    imagecopymerge($tmpImage, $corner, $newBorderWidth, $newBorderWidth, 0, 0, $newRadius, $newRadius, 100);


    // Top Right
    $corner = $this->getInnerCornerShape(270, $newRadius, $borderColour);
    imagecopy($tmpImage, $corner, $newWidth - $newBorderWidth - $newRadius, $newBorderWidth, 0, 0, $newRadius, $newRadius);


    // Bottom Left
    $corner = $this->getInnerCornerShape(90, $newRadius, $borderColour);
    imagecopyresampled($tmpImage, $corner, $newBorderWidth, $newHeight - $newBorderWidth - $newRadius, 0, 0, $newRadius, $newRadius, $newRadius, $newRadius);


    // Bottom Right
    $corner = $this->getInnerCornerShape(180, $newRadius, $borderColour);
    imagecopyresampled($tmpImage, $corner, $newWidth - $newBorderWidth - $newRadius, $newHeight - $newBorderWidth - $newRadius, 0, 0, $newRadius, $newRadius, $newRadius, $newRadius);


    imagecopyresampled($this->getImage(), $tmpImage, 0, 0, 0, 0, $this->getOverlayWidth(), $this->getOverlayHeight(), $newWidth, $newHeight);   


}

/**
 *
 */

private $innerCornerImage = null;
private function getInnerCornerShape($angle = 0, $width, $colour){

    $trans = null;

    if($this->innerCornerImage == null){
        $width2 = 250;
        $height = 250;

        $image = imagecreatetruecolor($width2, $height);

        $trans = imagecolorallocatealpha($image, 68, 68, 68, 127);

        imagealphablending($image, false);
        imagesavealpha($image, true);

        imagefill($image, 0, 0, $trans);

        imagearc($image, $width2, $height, $width2 * 2 + 1, $height * 2 + 1, 0, 0, $colour);
        imagefilltoborder($image, 1, 1, $colour, $colour);

        // Image will be a square, so use width for height too.
        $newImage = imagecreatetruecolor($width, $width);

        imagealphablending($newImage, false);
        imagesavealpha($newImage, true);

        imagefill($newImage, 0, 0, $trans);

        imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $width, $width2, $height);

        $this->innerCornerImage = $newImage;

    } else {
        $trans = imagecolorallocatealpha($this->innerCornerImage, 0, 0, 0, 127);
    }

    $image = imagerotate($this->innerCornerImage, $angle, $trans);

    return $image;
}   

它创建一个内角形状,然后只是旋转它。它具有完全透明的背景,但正如您所见,它无法正确合并。

任何人都有任何想法如何让他们正确合并?

同样,下面是预期结果(左)。与当前结果相对应(右)。

enter image description here

0 个答案:

没有答案