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

yipeiwu_com5年前PHP代码库
文字水印:
复制代码 代码如下:

$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

相关文章

使用PHP json_decode可能遇到的坑与解决方法

前言 最近在做网站 的时候用到了json_decode函数,发现了一个问题,现在总结分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧。 场景: 某项目客户反馈,输出的结果 JS...

分享PHP计算两个日期相差天数的代码

本文实例讲述了php计算两个日期相差天数的方法。分享给大家供大家参考。具体实现方法如下: <?php $date1 = date( 'Y-m-d' ); $date2...

分享8个最佳的代码片段在线测试网站

分享8个最佳的代码片段在线测试网站

有时候,我们需要测试一些代码片段,而电脑中没有安装针对该语言的运行环境,没关系,你可以在线测试它们。  本文为你带来 8 款非常好用的代码片段在线工具,帮助你快速、方便地测试、...

PHP的Socket通信之UDP通信实例

本文实例讲述了PHP的Socket通信之UDP通信方法。分享给大家供大家参考。具体如下: 1.创建一简单的UDP服务器 //服务器信息 $server = 'udp://127.0...

PHP程序漏洞产生的原因分析与防范方法说明

滥用include 1.漏洞原因: Include是编写PHP网站中最常用的函数,并且支持相对路径。有很多PHP脚本直接把某输入变量作为Include的参数,造成任意引用脚本、绝对路...