根据需求作一个用户点击测试桃花运的小程序。在开发中须要使用PHP进行开发,原理是将用户的姓名经过php的图片处理写入图片中,此处遇到一巨坑。php
就是png图片在调用 imagecolorallocate() 方法时会存在颜色丢失问题,形成用户写入名字时为透明色。小程序
解决办法:测试
去你妹的PNG!!!用JPG去。
spa
处理图片代码:blog
<?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ $name = $_POST['name']; $randNum = rand(1,30); $time = time(); $nameLen = mb_strlen($name,'utf-8'); $imgPath = 'userLuckTest/'.md5($name.date('y-m-d',$time)).'.png'; if(file_exists($imgPath)){ echo $imgPath; }else{ $image = '../img/luckTest/'.$randNum.'.png'; $font = '../img/lishu.ttf'; $image = imagecreatefrompng($image); $num = $nameLen * 55; $color = imagecolorallocate($image, 255,255,255); imagefttext($image,80,0,320-$num,330,$color,$font,$name); imagepng($image,'../'.$imgPath); echo $imgPath; } }else{ echo -1; } ?>