python PIL库和php编辑图片

发布于 2020-04-15  414 次阅读


急切网 http://jiqie.zhenbi.com/
这里面就有就是这么个算法

方法!

python版本

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font = ImageFont.truetype('msyh.ttf',35)
imageFile = './back.jpg'
im1=Image.open(imageFile)
draw = ImageDraw.Draw(im1)
draw.text((230,470),"13663348000",font = font, fill = (255, 255, 255))
draw.text((230,525),"857793686",font = font, fill = (255, 255, 255))
draw.text((230,575),"yy899999995",font = font, fill = (255, 255, 255))
draw.text((230,630),"857793686@qq.com",font = font, fill = (255, 255, 255))
draw = ImageDraw.Draw(im1)

im1.save("ok.jpg")

php版本

<?php
header("Content-type:text/html;charset=utf-8");
if($_GET['telephone']){
$telephone=$_GET['telephone'];
$QQ=$_GET['QQ'];
$wechart=$_GET['wechart'];
$email=$_GET['email'];
$number=$_GET['number'];
$name=$_GET['name'];
$png='.png';
$res=$number . "" . $png;
$image=imagecreatefrompng('back.png');//文件如res.png
imagealphablending($image, true);
$red=imagecolorallocate($image, 255, 255, 255);
imagefttext($image, 55, 0, 92, 99, $red, '/msyh.ttf', $name);
imagefttext($image, 20, 0, 240, 150, $red, '/msyh.ttf', "工号:"."".$number);
imagefttext($image, 35, 0, 230, 515, $red, '/msyh.ttf', $telephone);
imagefttext($image, 35, 0, 230, 565, $red, '/msyh.ttf', $QQ);
imagefttext($image, 35, 0, 230, 615, $red, '/msyh.ttf', $wechart);
imagefttext($image, 35, 0, 230, 670, $red, '/msyh.ttf', $email);
$filename=$res;
imagepng($image,$filename);
imagedestroy($image);
echo "<img src=$res>";

}

最终的效果呢,就是在下边这张图的对应位置插入对应文字,代码very言简意赅

附加知识
php文件中,字符串合并方法:
不是JAVA或者python中简单的+号链接。

$a=“hello”;
$b=“world”;
$a. " " . $b;

这样才是把字符串a和b链接得到hello world


不会写珂朵莉树的废柴ACMer