如何使用php语言来给html网站中的图片image加水印logo

下面是图片加水印的案例方法。只需要传入两个参数即可

$oldimage_name//需要加水印的原图名称

$new_image_name//加完水印的图片名称

//图片添加图片水印
function watermark_image($oldimage_name, $new_image_name){
$webdir = dirname(dirname(__FILE__));//根目录地址
$image_path = $webdir.”/watermark/watermark.png”;//水印图片
list($owidth,$oheight) = getimagesize($oldimage_name);
$width=$owidth;////获取图片的宽
$height=$oheight;///获取图片的高
$im = imagecreatetruecolor($width, $height);
//判断图片文件格式
$oldimage_name_str = explode(‘.’,$oldimage_name);
$oldimage_type = end($oldimage_name_str);
if($oldimage_type==’jpg’){//不同图片格式处理方法不同
$img_src = imagecreatefromjpeg($oldimage_name);
}else if($oldimage_type==’gif’){
$img_src = imagecreatefromgif($oldimage_name);
}else if($oldimage_type==’png’){
$img_src = imagecreatefrompng($oldimage_name);
}
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width – $w_width;//水印x坐标(离左边长度)
$pos_y = $height – $w_height;//水印y坐标(离底边长度)
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
if($oldimage_type==’jpg’){//不同图片格式处理方法不同
imagejpeg($im, $new_image_name, 100);
}else if($oldimage_type==’gif’){
imagegif($im, $new_image_name,100);
}else if($oldimage_type==’png’){
imagepng($im, $new_image_name);
}
imagedestroy($im);
unlink($oldimage_name);
return true;
}

(注:水印功能需要php支持GD,去掉php.ini 中;extension=php_gd2.dll前面的“;” )