GD库创建带有动态文字的图像

问题描述 投票:1回答:1

我想把从sql选择动态生成的文本放在用GD库创建的图片上。我用它来创建图像并放置一些文本,但我想把变量$users和sql选择数据放置到图像中。

$query = "SELECT id, name FROM users WHERE .... ORDER BY id DESC";
while ($line = mysql_fetch_assoc($query)) {

  $users .=  "<img src='https://www.example.com/" . $line['id'] . "/photo'/>&nbsp;" . $line['name'] . "<br />";
}



function  create_image(){
    $im = @imagecreate(550, 600)or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($im, 255, 255, 0);  // yellow
    $red = imagecolorallocate($im, 255, 0, 0);      // red

    imagestring($im, 1, 5,  10, $users);
    imagestring($im, 2, 5,  50, "Text 2", $red);

    imagepng($im,"image.png");
    imagedestroy($im);
}

create_image();
print "<img src=image.png?".date("U").">";

但$user变量中的文字没有出现,我该怎么做?

请教

php sql image gd image-generation
1个回答
1
投票

下面是一个如何在png图片上绘制文字的例子。

$img = imagecreatefrompng($image_path); //$image_path -> on which text to be drawn
        @imagealphablending($img, true);
        @imagesavealpha($img, true);
$textColor = imagecolorallocate($img, 100, 100, 98);
$font = '../fonts/Arial.ttf';

imagettftext($img, 18, 0, 140, 285,$textColor,$font, $name); // $name -> dynamic text to be drawn on image
$path = "path where you want to save created image";

$image = imagejpeg($img, $path);
imagedestroy($img);

完成...

© www.soinside.com 2019 - 2024. All rights reserved.