PHP 图片处理PNG颜色丢失

时间:2023-03-10 07:00:54
PHP 图片处理PNG颜色丢失

根据需求做一个用户点击测试桃花运的小程序。在开发中需要使用PHP进行开发,原理是将用户的姓名通过php的图片处理写入图片中,此处遇到一巨坑。

就是png图片在调用 imagecolorallocate() 方法时会存在颜色丢失问题,造成用户写入名字时为透明色。

解决办法:

  去你妹的PNG!!!用JPG去。

处理图片代码:

<?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;
}
?>