《PHP中添加文字水印函数封装》

来源:互联网 发布:info.php探针漏洞 编辑:程序博客网 时间:2024/06/05 16:52
先给大家看一下效果图吧,关于文字的位置,大家可以根据自己的需要进行调整,我这里直接给文字放在了左上角
<?php/***@param [string] 传过来的参数是文件名字符串*@return [array] 返回值是一个数组,包含图片宽度、高度、创建和输出的字符串以及扩展名*/function getImageInfo($filename){if(@!$info=getimagesize($filename)){//getimagesize()函数可以得到文件信息,//还可以判断图片是否为真实的图片类型,详细功能见PHP手册exit('文件不是真实图片');}//print_r($info);可以查手册看一下这个数组中每个参数的含义$fileInfo['width']=$info[0];$fileInfo['height']=$info[1];$mime=image_type_to_mime_type($info[2]);//info[2]这个是图片类型对应的数字,此函数可以根据该数字返回出文件对应的MIME类型,详细见手册//echo $mime;看一下这个$mime的输出格式,你就会理解下面为什么会这么做了$createFun=str_replace('/', 'createfrom', $mime);//将$mime中的'/'替换成'createfrom',//因为后边要用到imagecreatefromjpeg/jpg/png 这个函数,这样就可以动态使用不同的函数了$outFun=str_replace('/', '', $mime);$fileInfo['createFun']=$createFun;$fileInfo['outFun']=$outFun;$fileInfo['ext']=strtolower(image_type_to_extension($info[2]));//image_type_to_extension()是得到文件后缀名函数return $fileInfo;//返回文件信息}function waterText($filename,$fontfile='fonts/SIMYOU.TTF',$text='书旅',$dest='images/waterText',$pre='waterText_',$red=255,$green=0,$blue=0,$alpha=60,$size=30,$angle=0,$x=0,$y=30,$delSource=false){// $filename='images/2.jpg'; 文件名// $red=255;  这三个参数是调整水印文字颜色// $green=0;// $blue=0;// $alpha=60;文字透明度// $size=30; 文字大小// $angle=0;文字倾斜度// $x=0;   这两个参数为文件位置// $y=30;// $fontfile='fonts/SIMYOU.TTF';  字体类型// $text="书旅";   添加的文字$fileInfo=getImageInfo($filename);$image=$fileInfo['createFun']($filename);$color=imagecolorallocatealpha($image, $red, $green, $blue, $alpha);//查看手册看此函数作用imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);//$dest='waterText';if($dest && !file_exists($dest)){mkdir($dest,0777,true);}$pre='waterText_';//默认前缀$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;}/*$filename="images/4.jpg";waterText($filename);*/?>


这个写的不是特别完整,也希望大神给出意见,对代码进行修修改改


还是要提醒小伙伴儿们注意路径问题


0 0