当前位置: 移动技术网 > IT编程>开发语言>PHP > php图片加水印原理(超简单的实例代码)

php图片加水印原理(超简单的实例代码)

2019年04月14日  | 移动技术网IT编程  | 我要评论
文字水印:
复制代码 代码如下:

$w = 80;
$h = 20;
$im = imagecreatetruecolor($w,$h);
$textcolor = imagecolorallocate($im, 123, 12, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $grey); //画一矩形并填充

// 把字符串写在图像左上角
imagestring($im, 3, 2, 3, "hello world!", $textcolor);

// 输出图像
header("content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);

图片水印

$groundimg = "dsc05940.jpeg";
$groundinfo = getimagesize($groundimg);
$ground_w = $groundinfo[0];
//print_r($groundinfo);
$ground_h = $groundinfo[1];
switch($groundinfo[2]){
case 1:
$ground_im = imagecreatefromgif($groundimg);
break;
case 2:
$ground_im = imagecreatefromjpeg($groundimg);
break;
case 3:
$ground_im = imagecreatefrompng($groundimg);
break;
}

$waterimg = "dsc05949.jpeg";
$imginfo =getimagesize($waterimg);
$water_w = $imginfo[0];
$water_w = $imginfo[1];

switch($imginfo[2]){
case 1:
$water_im = imagecreatefromgif($waterimg);
break;
case 2:
$water_im = imagecreatefromjpeg($waterimg);
break;
case 3:
$water_im = imagecreatefrompng($waterimg);
break;
}
imagecopy($ground_im,$water_im,100,100,0,0,500,500);
header("content-type: image/jpeg");

imagejpeg($ground_im);

合并图片php提供了很多函数:例如:imagecopymerge,imagecopyresized

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网