用PHP透明圆裁剪图像

Lar*_*rry 4 php gd image-processing

我想使用PHP裁剪圆形图像,但似乎我的新图像有一些透明像素.当然,我只希望椭圆的外部区域具有背景透明

我的代码如下:

        $image = imagecreatetruecolor($this->dst_w, $this->dst_h);
        imagealphablending($image,true);
        imagecopy ( $image , $image_s , 0, 0, $this->src_x, $this->src_y, $this->dst_w, $this->dst_h );
        $mask = imagecreatetruecolor($this->src_x, $this->src_y);
        $mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
        $transparent = imagecolorallocate($mask, 255, 0, 0);
        imagecolortransparent($mask, $transparent);
        imagefilledellipse($mask, $this->dst_w/2, $this->dst_h/2, $this->dst_w, $this->dst_h, $transparent);
        $red = imagecolorallocate($mask, 0, 0, 0);
        imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
        imagecolortransparent($image, $red);
        imagefill($image,0,0, $red);

        if ($ext=="jpg" || $ext=="jpeg") {
            imagejpeg($image, $this->croppedImage);
        } else if ($ext=="png") {
            imagepng($image, $this->croppedImage);
        }           
        imagedestroy($image);
        imagedestroy($mask);
        // <------- END generate cropped Image ------->

        // <------- START generate transparent Image ------->               
        $this->generateTransparentImage('circle');
Run Code Online (Sandbox Code Playgroud)

......

这里是实际生成图像的示例: 在此输入图像描述

编辑:generateTransparentImage函数与上面列出的代码无关; 此函数生成此图像:http: //s7.postimage.org/byybq9163/Koala7_500x375_c_transparent.png

Ala*_*blo 7

有几点需要注意:

正如@DainisAbols建议的那样,为你的透明度采取不寻常的颜色应该更好.在这里,您使用的是黑色:

    $red = imagecolorallocate($mask, 0, 0, 0);
    imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
    imagecolortransparent($image, $red);
Run Code Online (Sandbox Code Playgroud)

即使你的var被称为红色,你的RGB值也是0-0-0.不寻常的颜色包括华而不实蓝(0-0-255),华而不实的绿色(0-255-0),华丽的黄色(255-255-0),华而不实的青色(0-255-255)和花哨的粉色(255-0- 255).红色在各处都很常见,并不是那么华丽,所以我把它从那些特殊颜色中排除.

然后,即使这里的图像都是真彩色图像,为每个图像分配颜色也是一种很好的做法.在上面的示例中,您创建了一个$red包含黑色的变量$mask,但您将其用作透明度颜色$image.

最后,您绘制的椭圆与图像大小的半径相同,因此您需要imagefill图像的每个角,而不仅仅是左上角的角.在你的例子中它可以工作,但这只是因为你选择黑色作为透明颜色.

这是一个完整的实现.

<?php

class CircleCrop
{

    private $src_img;
    private $src_w;
    private $src_h;
    private $dst_img;
    private $dst_w;
    private $dst_h;

    public function __construct($img)
    {
        $this->src_img = $img;
        $this->src_w = imagesx($img);
        $this->src_h = imagesy($img);
        $this->dst_w = imagesx($img);
        $this->dst_h = imagesy($img);
    }

    public function __destruct()
    {
        if (is_resource($this->dst_img))
        {
            imagedestroy($this->dst_img);
        }
    }

    public function display()
    {
        header("Content-type: image/png");
        imagepng($this->dst_img);
        return $this;
    }

    public function reset()
    {
        if (is_resource(($this->dst_img)))
        {
            imagedestroy($this->dst_img);
        }
        $this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h);
        imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h);
        return $this;
    }

    public function size($dstWidth, $dstHeight)
    {
        $this->dst_w = $dstWidth;
        $this->dst_h = $dstHeight;
        return $this->reset();
    }

    public function crop()
    {
        // Intializes destination image
        $this->reset();

        // Create a black image with a transparent ellipse, and merge with destination
        $mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
        $maskTransparent = imagecolorallocate($mask, 255, 0, 255);
        imagecolortransparent($mask, $maskTransparent);
        imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent);
        imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100);

        // Fill each corners of destination image with transparency
        $dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255);
        imagefill($this->dst_img, 0, 0, $dstTransparent);
        imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent);
        imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent);
        imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent);
        imagecolortransparent($this->dst_img, $dstTransparent);

        return $this;
    }

}
Run Code Online (Sandbox Code Playgroud)

演示:

$img = imagecreatefromjpeg("test4.jpg");
$crop = new CircleCrop($img);
$crop->crop()->display();
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述


Peo*_*eon 1

您正在裁剪并删除黑色(或将黑色设置为透明)。由于您的图像中有黑色,因此它也会被删除。

不要删除颜色,而是尝试将外层颜色替换为粉红色,然后将其设置为透明。