6

我正在尝试创建一个函数来调整透明 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 设置,请帮忙。感谢您的每一个建议!

4

1 回答 1

3

imagesavealpha函数需要应用于您要粘贴到您创建的图像上的图像,所以要$image代替$image_p

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

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

于 2012-09-10T10:42:57.057 回答