企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 图片+水印文字 ~~~ <?php /** * 图片水印 * Created by PhpStorm. * User: Administrator * Date: 2017/1/14 0014 * Time: 下午 2:27 */ class ImageWater { /** 处理完成保存到原路径 * @param $filename 图片的路径 * @param string $waterstring 水印文字 */ public function waterText($filename, $waterstring = 'hello world') { $imgquality = 100; //图片质量0-100,值最大图片质量愈好,图片的大小也越大 *推荐90-100 太小图片会出现模糊现象 $image_size = getimagesize($filename); $upimgw = $image_size[0]; //上传图片的宽度 $upimgh = $image_size[1]; //上传图片的高度 $iinfo = getimagesize($filename); $nimage = imagecreatetruecolor($image_size[0], $image_size[1]); $white = imagecolorallocate($nimage, 118, 196, 234); imagefill($nimage, 0, 0, $white); switch ($iinfo[2]) { case 1: $simage = imagecreatefromgif($filename); break; case 2: $simage = imagecreatefromjpeg($filename); break; case 3: $simage = imagecreatefrompng($filename); break; case 6: $simage = imagecreatefromwbmp($filename); break; default: die("不支持的文件类型"); exit; } imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]); $waterstart_x = $upimgw / 2 - 50; //echo $upimgh; $waterstart_y = $upimgh - 20; imagestring($nimage, 6, $waterstart_x, $waterstart_y, $waterstring, $white); //int imagestring(int im, int font, int x, int y, string s, int col);返回值: 整数 函数种类: 图形处理b 内容说明:本函数在图片上绘出水平的横式字符串。参数 font 为字形,设为 1 到 5 表示使用默认字形。参数 x、y 为字符串起点坐标。字符串的内容放在参数 s 上。参数 col 表示字符串的颜色。 switch ($iinfo[2]) { case 1: //imagegif($nimage, $destination); imagejpeg($nimage, $filename, $imgquality); break; case 2: imagejpeg($nimage, $filename, $imgquality); break; case 3: imagepng($nimage, $filename, $imgquality); break; case 6: imagewbmp($nimage, $filename, $imgquality); //imagejpeg($nimage, $destination); break; } //覆盖原上传文件 imagedestroy($nimage); imagedestroy($simage); } } ~~~ #### 调用案例 ~~~ <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/1/14 0014 * Time: 下午 2:41 */ require_once 'ImageWater.php'; $imageWater=new ImageWater(); $filename='00107.jpg'; echo "<img src='".$filename."'/>"; $imageWater->waterText($filename,'hdghagd'); echo "<img src='".$filename."'/>"; ~~~