课程章节:第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; }