【问题标题】:Transparent Circle Cropped Image with PHP使用 PHP 的透明圆形裁剪图像
【发布时间】:2012-10-18 11:11:05
【问题描述】:

我想使用 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');

......

实际生成图像的示例如下:

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

【问题讨论】:

  • 如果你想让别人玩你的代码,你也应该分享原始图像。另外generateTransparentImage的代码是隐藏的,所以其他人也无法复制。
  • PHP - Mask polygon over image 的可能副本 - 如果您不想使用库,Wideimage 的源代码是免费软件,因此您可以将其移植到您的应用程序中。否则,有很多现有的问题展示了如何做到这一点的各种其他方法,请免费查看:stackoverflow.com/search?q=%5Bphp%5D+%5Bgd%5D+mask

标签: php image-processing gd


【解决方案1】:

有几点需要注意:

正如@DainisAbols 建议的那样,最好采用不寻常的颜色来提高透明度。在这里,您使用的是黑色:

    $red = imagecolorallocate($mask, 0, 0, 0);
    imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
    imagecolortransparent($image, $red);

即使你的 var 被称为 red,你的 R-G-B 值也是 0-0-0。不常见的颜色包括闪亮的蓝色 (0-0-255)、闪亮的绿色 (0-255-0)、闪亮的黄色 (255-255-0)、闪亮的青色 (0-255-255) 和闪亮的粉红色 (255-0- 255)。红色在任何地方都很常见,而且不是那么华丽,所以我将它排除在那些特殊颜色之外。

那么,即使您的图片都是真彩色的,最好为每张图片分配颜色。在上面的示例中,您为$mask 创建了一个包含黑色的$red 变量,但您在$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;
    }

}

演示:

$img = imagecreatefromjpeg("test4.jpg");
$crop = new CircleCrop($img);
$crop->crop()->display();

结果:

【讨论】:

  • 它不透明,背景为白色
  • 你好,我刚刚检查过,我确认透明度在这里有效
  • proof:home.fuz.org/tests/test-circle-crop.php(使用任意背景色检查生成的图片是否透明)
  • 尽力而为,但无法正常工作,请在 Photoshop 中检查生成的图像。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我也无法用 Alain 的代码完成它。在了解了每一行代码的作用之后,这是我的修复..

        //this creates a pink rectangle of the same size
        $mask = imagecreatetruecolor($imgwidth, $imgheight);
        $pink = imagecolorallocate($mask, 255, 0, 255);
        imagefill($mask, 0, 0, $pink);
        //this cuts a hole in the middle of the pink mask
        $black = imagecolorallocate($mask, 0, 0, 0);
        imagecolortransparent($mask, $black);
        imagefilledellipse($mask, $imgwidth/2, $imgheight/2, $imgwidth, $imgheight, $black);
        //this merges the mask over the pic and makes the pink corners transparent
        imagecopymerge($img, $mask, 0, 0, 0, 0, $imgheight, $imgheight);
        imagecolortransparent($img, $pink);
        imagepng($img, "my_circle.png");
    

    【讨论】:

    • imagecopymerge 在此示例中不起作用,因为缺少最后一个参数 pct。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2014-12-12
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多