【问题标题】:GD library create image with dynamic text on itGD 库创建带有动态文本的图像
【发布时间】:2013-04-18 06:01:01
【问题描述】:

我想将通过 sql 选择动态生成的文本放置在使用 GD 库创建的图像上。我正在使用它来创建图像并在其上放置一些文本,但我想将带有 sql 选择数据的变量 $users 放入图像中:

$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】:

    这是一个如何在 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);
    

    完成..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2013-08-06
      • 1970-01-01
      • 2016-05-14
      相关资源
      最近更新 更多