为什么图片在线生成出来的文字颜色不对

今天一个网站在线生成图片的功能出错,图片生成出来文字的颜色变了,不是指定的黑色
检查代码

function uploadText($temp , $text)
{
    $saveDir = app()->getRootPath() .  'public/upload/images/'.date('Y') . '/'.date('md');
    if(!is_dir($saveDir)) mkdir($saveDir , 0777,true);
    $filename = '/'.md5(uniqid()).'.png';
    $staticDomain = rtrim(config("site.staticDomain"),'/'). '/';
    $url = $staticDomain.'images/'.date('Y') . '/'.date('md').$filename;


    $dst_path = app()->getRootPath() .  'public/template/images/'.$temp.'.png' ;
    if(!is_file($dst_path)) {
        returnJson("背景图不存在");
    }
    $font = app()->getRootPath() .  'public/template/images/font/hanyifengshanghei65w.ttf' ;
    $image = imagecreatefrompng($dst_path);
    $textcolor = imagecolorallocate($image, 0, 0, 0);
    imagefttext($image, 28, 0, 40, 120, $textcolor, $font, $text);
    imagepng($image,$saveDir.$filename);
    returnJson("success",0,$url);
}
$textcolor = imagecolorallocate($image, 0, 0, 0);这个000就是黑色颜色代码,但是生成出来却是别的颜色,折腾了半天没找到原因。
起因是把底图png图片压缩了一下,我嫌他太大了。
然后把压缩过的png图用photoshop打开发现图像模式变为了索引模式,把图改为RGB颜色模式,覆盖进去生成出来的图片文字颜色就正确了。
Tags : 在线生成

评论