合并两个PNG透明图像

dea*_*der 6 php png gd watermark

我正在尝试创建一个函数来调整透明PNG图像的大小,并为它们添加透明的PNG水印.我试图放置imagealphablending($ image_p,false); 和imagesavealpha($ image_p,true); 无处不在,但它会使黑色背景或裁剪出第一张图像.这是我的代码:

$newName=$this->filename;
list($OrigWidth, $OrigHeight)=$this->info;
if($OrigHeight>$OrigWidth){
    $pomer=$OrigWidth/$OrigHeight;
    $NewHeight=$h;
    $NewWidth=$NewHeight*$pomer;
}else{
    $pomer=$OrigHeight/$OrigWidth;
    $NewWidth=$w;
    $NewHeight=$NewWidth*$pomer;
}

$image_p=imagecreatetruecolor($NewWidth, $NewHeight);
if($this->ext=="jpg")
$image=imagecreatefromjpeg($newName);
elseif($this->ext=="png")
$image=imagecreatefrompng($newName);
elseif($this->ext=="gif")
$image=imagecreatefromgif($newName);

if($this->ext=="png" or $this->ext=="gif"){ //pr?hlednost
    imagealphablending($image_p, false);
    imagesavealpha($image_p,true);
    $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
    imagefilledrectangle($image_p, 0, 0, $NewWidth, $NewHeight, $transparent);
}

if(($OrigWidth>$w or $OrigHeight>$h) and $w!=0)
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight);
else
    $image_p=$image; //není t?eba zmenšovat

if($vodoznak!=""){ //if watermark is set
    //imagealphablending($image_p, false);
    //imagesavealpha($image_p,true);
    $watermark = imagecreatefrompng($vodoznak);     
    $ww = imagesx($watermark);
    $wh = imagesy($watermark);
    if($umisteni{0}=="0") $x=3; else $x=$OrigWidth-$ww-3;       
    if($umisteni{1}=="0") $y=3; else $y=$OrigHeight-$wh-3;
    imagealphablending($watermark, false);
    imagesavealpha($watermark,true);
    imagecopy($image_p, $watermark, $x, $y, 0, 0, $ww, $wh);
}

if($this->ext=="jpg")
imagejpeg($image_p, $copypath, $komprese);
elseif($this->ext=="png")
imagepng($image_p, $copypath);
elseif($this->ext=="gif")
imagegif($image_p, $copypath);
Run Code Online (Sandbox Code Playgroud)

我不知道,我将在哪里放置alpha设置,请帮忙.谢谢你的建议!

小智 3

imagesavealpha函数需要应用于您要粘贴到您创建的图像上,因此而$image不是$image_p

imagealphablending($image, true);
imagesavealpha($image,true);
Run Code Online (Sandbox Code Playgroud)

就像你对水印所做的那样!