【教程】php在图片上添加文字

生成拆迁协议赔偿金额(游戏),可在朋友圈转发,先上图

SYY_1517183298_3967.jpg

上图中的小写金额,大写金额,姓名签字,日期,都是使用php代码生成的,金额随机,姓名是使用一个表格提交上来的

html代码

<form name="form1" action="test2.php" method="post">
    输入姓名生成拆迁合同:<input type="text" name="fname">
    <input type="submit" name="submit1" value="提交">
</form>

php代码

function HBImg($path_2){
    $path_1 = "a1.jpg";
    $image_1 = imagecreatefromjpeg($path_1);
    $image_2 = imagecreatefromjpeg($path_2);//可以是http://xxx.vv的资源图片
    $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
    imagecopymerge($image_3,$image_1,0,0,0,0,imagesx($image_1),imagesy($image_1),100);
    list($width,$height)=getimagesize($path_2);
    imagecopyresampled($image_3,$image_2,40,550,0,0,230,230,$width,$width);
    $pic_name="fv".time()."_".rand(1000,9999).".jpg";
    imagejpeg($image_3,"../xx/".$pic_name,50);
    imagedestroy($image_3);
    $path="http://xxx.xx/".$pic_name;
    return $path;
}
    
    
    
function hebingImg($path_1,$path_2,$f1,$f2,$f3){//加文字
    $image_1 = imagecreatefromjpeg($path_1);
    
    $textcolor = imagecolorallocate($image_1, 0, 0,0); //设置水印字体颜色  
    $font = 'a1.ttf'; //定义字体  
    imagettftext($image_1, 20, 0, 490, 204, $textcolor, $font, $f1);//将文字写到图片中  
    imagettftext($image_1, 20, 0, 200, 244, $textcolor, $font, $f2);//将文字写到图片中  
    if(strlen_utf8($f3)==3){
        imagettftext($image_1, 30, 0, 540, 784, $textcolor, $font, $f3);//将文字写到图片中  
    }
    else if(strlen_utf8($f3)==2){
        imagettftext($image_1, 30, 0, 580, 784, $textcolor, $font, $f3);//将文字写到图片中  
    }
    else{
        imagettftext($image_1, 30, 0, 580, 784, $textcolor, $font, $f3);//将文字写到图片中  
    }
    date_default_timezone_set('PRC'); //设置中国时区 
    imagettftext($image_1, 20, 0, 450, 872, $textcolor, $font, date("Y"));//将年写到图片中  
    imagettftext($image_1, 20, 0, 540, 872, $textcolor, $font, date("n"));//将月写到图片中  
    imagettftext($image_1, 20, 0, 580, 872, $textcolor, $font, date("d"));//将日写到图片中        
            
    $image_2 = imagecreatefromjpeg($path_2);
    $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
    imagecopymerge($image_3,$image_1,0,0,0,0,imagesx($image_1),imagesy($image_1),100);
    list($width,$height)=getimagesize($path_2);
    // bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h ,int $src_w , int $src_h )

    // $dst_image:新建的图片

    // $src_image:需要载入的图片

    // $dst_x:设定需要载入的图片在新图中的x坐标

    // $dst_y:设定需要载入的图片在新图中的y坐标

    // $src_x:设定载入图片要载入的区域x坐标

    // $src_y:设定载入图片要载入的区域y坐标

    // $dst_w:设定载入的原图的宽度(在此设置缩放)

    // $dst_h:设定载入的原图的高度(在此设置缩放)

    // $src_w:原图要载入的宽度

    // $src_h:原图要载入的高度
    imagecopyresampled($image_3,$image_2,130,575,0,0,90,90,$width,$height);
    $pic_name="SYY_".time()."_".rand(1000,9999).".jpg";
    imagejpeg($image_3,"img/".$pic_name,100);
    imagedestroy($image_3);
    $path=$pic_name;
    return $path;
}
    
    
    
    
    
function https_post($url, $data) {
    $curl = curl_init ();
    curl_setopt ( $curl, CURLOPT_URL, $url );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
    if (! empty ( $data )) {
    curl_setopt ( $curl, CURLOPT_POST, 1 );
    curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
    }
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    $output = curl_exec ( $curl );
    curl_close ( $curl );
    return $output;
    }
    function https_get($url) {
    $output = file_get_contents($url);;
    return $output;
}

/**
 * 计算 UTF-8 字符串长度(忽略字节的方案)
 *
 * @param string $str
 * @return int
 */
function strlen_utf8($str)
{
    $i = 0;
    $count = 0;
    $len = strlen($str);
    while ($i < $len)
    {
        $chr = ord($str[$i]);
        $count++;
        $i++;
        if ($i >= $len)
        {
            break;
        }
        if ($chr & 0x80)
        {
            $chr <<= 1;
            while ($chr & 0x80)
            {
                $i++;
                $chr <<= 1;
            }
        }
    }
 
    return $count;
}
/**
* 数字转换为中文
* @param  string|integer|float  $num  目标数字
* @param  integer $mode 模式[true:金额(默认),false:普通数字表示]
* @param  boolean $sim 使用小写(默认)
* @return string
*/
function number2chinese($num,$mode = true,$sim = true){
    if(!is_numeric($num)) return '含有非数字非小数点字符!';
    $char    = $sim ? array('零','一','二','三','四','五','六','七','八','九')
    : array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
    $unit    = $sim ? array('','十','百','千','','万','亿','兆')
    : array('','拾','佰','仟','','萬','億','兆');
    $retval  = $mode ? '万元整':'点';
    //小数部分
    if(strpos($num, '.')){
        list($num,$dec) = explode('.', $num);
        $dec = strval(round($dec,2));
        if($mode){
            $retval .= "{$char[$dec['0']]}角{$char[$dec['1']]}分";
        }else{
            for($i = 0,$c = strlen($dec);$i < $c;$i++) {
                $retval .= $char[$dec[$i]];
            }
        }
    }
    //整数部分
    $str = $mode ? strrev(intval($num)) : strrev($num);
    for($i = 0,$c = strlen($str);$i < $c;$i++) {
        $out[$i] = $char[$str[$i]];
        if($mode){
            $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '';
                if($i>1 and $str[$i]+$str[$i-1] == 0){
                $out[$i] = '';
            }
                if($i%4 == 0){
                $out[$i] .= $unit[4+floor($i/4)];
            }
        }
    }
    $retval = join('',array_reverse($out)) . $retval;
    return $retval;
}
$fname=$_REQUEST["fname"];
$max=100;
$min=990;
$rand=rand($min,$max);
$c=number2chinese($rand,true,false);
$img1=hebingImg("a1.jpg","a2.jpg",$rand."0000",$c,$fname);
echo("<img src='img/".$img1."' title='长按保存图片'>");

其中,定义了一个字体,可在网上随意找一个手写体下载下来使用即可。

想要协议底图,或字体的童鞋,留下邮箱,我可以发给你

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hifhf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值