PHP文本覆盖图像-文本不会显示在保存的图像上

PHP文本覆盖图像-文本不会显示在保存的图像上,php,image,Php,Image,我有这段代码,它在PHP中将文本置于图像之上,但是当我将生成的图像保存到我的计算机上时,只有图像显示,而文本不显示 以下是我正在使用的代码: <?php header("Content-type: image/png"); $imgPath = 'template4.png'; $image = imagecreatefrompng($imgPath); $color = imagecolorallocate($image, 255, 0, 0); error_r

我有这段代码,它在PHP中将文本置于图像之上,但是当我将生成的图像保存到我的计算机上时,只有图像显示,而文本不显示

以下是我正在使用的代码:

<?php
header("Content-type: image/png");
    $imgPath = 'template4.png';
    $image = imagecreatefrompng($imgPath);
    $color = imagecolorallocate($image, 255, 0, 0);

error_reporting(0);
if(isset($_POST['submit']))  

{
    $GT = $_POST['GT'];


    $Gamertag = preg_replace('/ /', "+", $GT);



    $content = file_get_contents("http://www.ea.com/uk/football/profile/{$Gamertag}/360");


    if ($content == false) {
        echo "Cant find this Gamertag";
        exit(); 
    }


    // Titles Won

    preg_match('#<div class="stat">
                Titles Won                <span>([0-9\.]*)<span class="sprite13 goalImage cup"></span></span>#', $content, $titleswon);
}


$string = $titleswon[1];
$fontSize = 3;
$x = 5;
$y = 5;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagepng($image);
?>

如注释中所述,这是regexp问题,而不是文本到图像的问题

首先,您应该检查preg_match与boolean false的结果,以查看匹配是否成功。 将模式中的任何空格替换为
\s+
\s*

至少执行var_dump以查看匹配结果。

使用以下命令直接保存图像:

imagepng($Image, The path to save the file to.)

所以这是一个regexp问题…如何保存图像?只需右键单击+保存如果直接使用imagepng($image,保存文件的路径)保存会发生什么情况。我将它作为一个awnser,以便您可以接受:)文本确实显示在图像上,因此preg_匹配必须工作,只有当我保存图像时它才不显示