尝试将文本添加到图像中

时间:2014-07-24 05:14:52

标签: php png

我正在尝试添加2行文字。一个是人的名字,另一个是氏族的名字。

你能帮我解决我做错的事吗?我没有收到任何错误消息或任何内容,图像正常加载。

这是我的代码:

  //Ribbons Image
$ribbons = array(
    "ribbons/1STAID.png",
    "ribbons/BMT.png",
    "ribbons/BRA1A.png",
    "ribbons/CCMD.png",
    "ribbons/CCMDV2.png",
    "ribbons/donator.png",
    "ribbons/FTR.png",
    "ribbons/GC.png",
    "ribbons/GRA1A.png",
    "ribbons/IOTP.png",
    "ribbons/MEDIC.png",
    "ribbons/PHA1A.png",
    "ribbons/PILOT.png",
    "ribbons/RCT1A.png",
    "ribbons/RCT2A.png",
    "ribbons/RCT3A.png",
    "ribbons/RCT4A.png",
    "ribbons/SRA1A.png",
    "ribbons/SVR1A.png",
    "ribbons/SVR2A.png",
    "ribbons/SVR3A.png",
    "ribbons/SVR4A.png",
    "ribbons/SVR-ALTIS.png",
    "ribbons/XOCMD.png",
    "ribbons/XOCMDV2.png");


//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");

//Background
$frame = imagecreatefrompng("sign3.png");

//imagecopymergy(output,image,x,y,0,0,w,h,100)

//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);


//Trying to add the text on the image
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Test...';
$font = 'arial.ttf';
imagettftext($im, 20, 0, 10, 20, $white, $font, $text);




//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
    $z++;
    for ($x = 0; $x <= 150; $x += 50) {
        if ($i <= 24) {
            $rib = imagecreatefromjpeg($ribbons[$i]);
            imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
        }
        $i++;
    }
}

//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

工作代码:

//Ribbons Image
$ribbons = array(
    "ribbons/1STAID.png",
    "ribbons/BMT.png",
    "ribbons/BRA1A.png",
    "ribbons/CCMD.png",
    "ribbons/CCMDV2.png",
    "ribbons/donator.png",
    "ribbons/FTR.png",
    "ribbons/GC.png",
    "ribbons/GRA1A.png",
    "ribbons/IOTP.png",
    "ribbons/MEDIC.png",
    "ribbons/PHA1A.png",
    "ribbons/PILOT.png",
    "ribbons/RCT1A.png",
    "ribbons/RCT2A.png",
    "ribbons/RCT3A.png",
    "ribbons/RCT4A.png",
    "ribbons/SRA1A.png",
    "ribbons/SVR1A.png",
    "ribbons/SVR2A.png",
    "ribbons/SVR3A.png",
    "ribbons/SVR4A.png",
    "ribbons/SVR-ALTIS.png",
    "ribbons/XOCMD.png",
    "ribbons/XOCMDV2.png");


//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");

//Background
$frame = imagecreatefrompng("sign3.png");

//imagecopymergy(output,image,x,y,0,0,w,h,100)

//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);


//Trying to add the text on the image
$im = imagecreatetruecolor(50, 30);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0, 82, 255);
imagefilledrectangle($im, 4, 4, 50, 25, $white);
$text = 'Nato Military Corp';
$font = 'arial.ttf';

imagettftext($frame, 12, 0, 450, 220, $white, $font, $text);
//imagecopymerge($frame, $im, 10, 20, 0, 0, 10, 25, 100);


//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
    $z++;
    for ($x = 0; $x <= 150; $x += 50) {
        if ($i <= 24) {
            $rib = imagecreatefromjpeg($ribbons[$i]);
            imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
        }
        $i++;
    }
}

//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');