课程名称:PHP进阶篇-GD库图像处理
课程章节:第3章 图像常用操作
主讲老师:king
课程内容:
今天学习的内容包括:
3-6 文字水印的封装及测试
3-7 图片水印的实现
图片水印的封装及测试
课程收获:
我的环境是 php 8.19 nts版本
文字水印函数封装 图片水印函数的封装
//文字水印
/**
* 文字水印
* @param $filename
* @param $fontfile
* @param $text
* @param $dest
* @param $pre
* @param $delsource
* @param $r
* @param $g
* @param $b
* @param $alpha
* @param $size
* @param $angle
* @param $x
* @param $y
* @return string
*/
function water_text($filename,$fontfile,$text = '佳和信息',$dest = 'waterText',$pre = 'waterText_',$delsource=false,$r = 255,$g = 0,$b = 0,$alpha = 60, $size = 30, $angle = 0,$x = 0,$y = 30)
{
//$filename = 'images/1.jpg';
// $r = 255;
// $g = 0;
// $b = 0;
// $alpha = 60;
// $size = 30;
// $angle = 0;
// $x = 0;
// $y = 30;
//$fontfile = 'fonts/kaiti.ttc';
//$text = '佳和信息';
$fileInfo = getImageInfo($filename);
$image = $fileInfo['createFun']($filename);
$color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
//$dest = 'waterText';
//$pre = 'waterText_';
if ($dest && !file_exists($dest)) {
mkdir($dest, 0777, true);
}
$randNum = mt_rand(100000, 999999);
$dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
$destination = $dest ? $dest . '/' . $dstName : $dstName;
$fileInfo['outFun']($image, $destination);
imagedestroy($image);
if ($delsource) {
@unlink($filename);
}
return $destination;
}function water_pic($dstName,$srcName,$pos=0,$dest = 'waterPic',$pre = 'waterPic_',$pct=50,$delsource=false){
// $dstName='1.jpg';
// $srcName='jdlogo.png';
// $pos=0;
// $pct=50;
// $dest = 'waterPic';
// $pre = 'waterPic_';
// $delsource=false;
$dstInfo=getImageInfo($dstName);
$srcInfo=getImageInfo($srcName);
$dst_im=$dstInfo['createFun']($dstName);
$src_im=$srcInfo['createFun']($srcName);
$src_width=$srcInfo['width'];
$src_height=$srcInfo['height'];
$dst_width=$dstInfo['width'];
$dst_height=$dstInfo['height'];
switch ($pos){
case 0:
$x=0;
$y=0;
break;
case 1:
$x=($dst_width-$src_width)/2;
$y=0;
break;
case 2:
$x=$dst_width-$src_width;
$y=0;
break;
case 3:
$x=0;
$y=($dst_height-$src_height)/2;
break;
case 4:
$x=($dst_width-$src_width)/2;
$y=($dst_height-$src_height)/2;
break;
case 5:
$x=$dst_width-$src_width;
$y=($dst_height-$src_height)/2;
break;
case 6:
$x=0;
$y=$dst_height-$src_height;
break;
case 7:
$x=($dst_width-$src_width)/2;
$y=$dst_height-$src_height;
break;
case 8:
$x=$dst_width-$src_width;
$y=$dst_height-$src_height;
break;
default:
$x=0;
$y=0;
break;
}
imagecopymerge($dst_im,$src_im,$x,$y,0,0,$src_width,$src_height,$pct);
if ($dest && !file_exists($dest)) {
mkdir($dest, 0777, true);
}
$randNum = mt_rand(100000, 999999);
$dstName = "{$pre}{$randNum}" . $dstInfo['ext'];
$destination = $dest ? $dest . '/' . $dstName : $dstName;
$dstInfo['outFun']($dst_im,$destination);
imagedestroy($src_im);
imagedestroy($dst_im);
if ($delsource) {
@unlink($dstName);
}
return $destination;
}點擊查看更多內容
2人點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦