php - 合并两个PNG透明图像

标签 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);

我不知道,我应该在哪里设置 alpha 设置,请帮忙。 谢谢你的每一个建议!

最佳答案

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

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

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

关于php - 合并两个PNG透明图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268813/

相关文章:

php - 从图像中剪切任何形状(Imagik/Gd)

PHP-EWS - 订阅/推送通知 - 完整示例

php - 单元测试调用具体子方法的抽象类方法

PHP 依赖注入(inject) - Pimple 等。 - 为什么使用关联数组与 getter?

image - 有没有办法使用 ffmpeg 将 GIF 的所有帧保存到 PNG 中?

php - 我如何让 php5-gd 工作?

php - 如何对评论进行投票

python - 如何将使用 Pillow 修改的 .png 图像添加到 OpenCV 视频中?

java - 什么是降低 PNG 位深度的最佳方法

php - 用户名作为水印