php中合并图片并添加水印,php 图片水印(图片合并原理)

$tmpfile = $_FILES['Filedata']['tmp_name'];     $arr = getimagesize($tmpfile);

switch ($arr["mime"]) {

case "image/jpeg":

$type = "jpeg"; //jpeg file

$imgsrc = imagecreatefromjpeg($tmpfile);

break;

case "image/gif":

$type = "gif"; //gif file

$imgsrc = imagecreatefromgif($tmpfile);

break;

case "image/png":

$type = "png"; //png file

$imgsrc = imagecreatefrompng($tmpfile);

break;

default:

$type = false;

break;

}

$img = imagecreatetruecolor($arr[0], $arr[1]);

imagecopyresampled($img, $imgsrc, 0, 0, 0, 0, $arr[0], $arr[1], $arr[0], $arr[1]);

//水印图像

$imgco=$this->vdata['cdnUrl'].'img/ADSS_logo.png';

$imgco_src=  imagecreatefrompng($imgco);

$imgco_info=  getimagesize($imgco);

//水印透明度

$alpha = 100;

//合并水印图片

imagecopymerge($img,$imgco_src,($arr[0]-$imgco_info[0])/2,($arr[1]-$imgco_info[1])/2,0,0,$imgco_info[0],$imgco_info[1],$alpha);

$func = 'image' . $type;

if($type=='png')

$result = $func($img, $filePath, 9);

else

$result = $func($img, $filePath, 100);

imagedestroy($img);

实际生产的时候,原png图片为透明的,合并后,为黑色的背景,所以把

//合并水印图片

imagecopymerge($img,$imgco_src,($arr[0]-$imgco_info[0])/2,($arr[1]-$imgco_info[1])/2,0,0,$imgco_info[0],$imgco_info[1],$alpha);

改为

imagecopymerge_alpha($img,$imgco_src,($arr[0]-$imgco_info[0])/2,($arr[1]-$imgco_info[1])/2,0,0,$imgco_info[0],$imgco_info[1],$alpha);

就可以解决该问题

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){         $opacity=$pct;         // getting the watermark width         $w = imagesx($src_im);         // getting the watermark height         $h = imagesy($src_im);                       // creating a cut resource         $cut = imagecreatetruecolor($src_w, $src_h);         // copying that section of the background to the cut         imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);         // inverting the opacity         //$opacity = 100 - $opacity;                       // placing the watermark now         imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);         imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值