基本信息
源码名称:php GD库使用(给png图片增加水印文字)
源码大小:8.84M
文件格式:.zip
开发语言:PHP
更新时间:2018-08-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

php的GD库使用技巧


原始图片为:

加上水印文字后如下图:




<?php

function generateImg($source, $text1, $font = './msyhbd.ttf') {  
    $date = '' . date ( 'Ymd' ) . '/';  
    $img = $date . md5 ( $source . $text1) . '.jpg';  
    if (file_exists ( './' . $img )) {  
        return $img;  
    }  
  
    $main = imagecreatefrompng ( $source );  
  
    $width = imagesx ( $main );  
    $height = imagesy ( $main );  
  
    $target = imagecreatetruecolor ( $width, $height );  
  
    $white = imagecolorallocate ( $target, 255, 255, 255 );  
    imagefill ( $target, 0, 0, $white );  
  
    imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );  
  
    $fontSize = 40;//磅值字体  
    $fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB颜色  
    $fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质  
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), ceil(($height-$fontBox[1])/2), $fontColor, $font, $text1 ); 
  
    //imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用  
  
    //imagefilledpolygon ( $target, array (10   0, 0   142, 0, 12   142, 20   0, 12   142), 3, $fontColor );//画三角形  
    //imageline($target, 100, 200, 20, 142, $fontColor);//画线  
    //imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形  
  
    //bof of 合成图片  
    //$child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );  
    //imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );  
    //eof of 合成图片  
  
    @mkdir ( './' . $date );  
    imagejpeg ( $target, './' . $img, 95 );  
  
    imagedestroy ( $main );  
    imagedestroy ( $target );  
    imagedestroy ( $child1 );  
    return $img;  
}
generateImg ( 'https://img01.haolizi.net/2018/08/09/4c/b/8/4cb8e929fc944633830c557bad3d7863.png','好例子网');  
exit ();