为什么我的图像不透明,而我使用的是imagecolortransparent()

时间:2017-10-22 13:31:14

标签: php transparent

我想要一张覆盖女人头部的皇冠照片,女人的脸是可见的。帮我 我的代码是:

// First we create our stamp image manually from GD
    $stamp = imagecreatefrompng('http://depictfoll.net/face/php-facedetection-master/Untitled-2_clipped_rev_1 (1).png');
    $black = imagecolorallocate($this->canvas, 0, 0, 0);
    imagecolortransparent($this->canvas, $black);

    // Set the margins for the stamp and get the height/width of the stamp image
    $marge_right = 0;
    $marge_bottom = 0;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);


    // Merge the stamp onto our photo with an opacity of 50%
    imagecopymerge($this->canvas, $stamp, $this->face['x']+$this->face['w'] - $sx - $marge_right, $this->face['y']+ $this->face['w'] - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 100);
    //$color = imagecolorallocate($this->canvas, 33333, 0, 0); //red

    header('Content-type: image/png');
    imagepng($this->canvas);

结果: enter image description here

1 个答案:

答案 0 :(得分:0)

以下是这对我有用的方法:)

首先检查您的新图像是否创建为真彩色:

$this->canvas = imagecreatetruecolor($width, $height);

然后按照新图片进行操作:

imagesavealpha($this->canvas, true);
imagealphablending($this->canvas, false);

$background = imagecolorallocatealpha($this->canvas, 0, 0, 0, 127);
imagefill($this->canvas , 0, 0, $background);

基本上,您可以在此处为新图像设置Alpha通道,并使用透明色填充此通道。然后你可以复制你的东西,保持透明度。