为了账号安全,请及时绑定邮箱和手机立即绑定

【学习打卡】第17天 PHP进阶篇-GD库图像处理

课程名称: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人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消