欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-06 19:31:22
文字水印: 复制代码 代码如下: $w = 80; $h = 20; $im = imagecreatetruecolor($w,$h); $textcolor = ima...
文字水印:
复制代码 代码如下:

$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