Gd Library Image Style imagettftext

时间:2012-07-04 17:14:17

标签: php gd

如何制作喜欢这张图片? 我只想学习如何使用GD库获得这样的输出, 谢谢你的帮助..

enter image description here

2 个答案:

答案 0 :(得分:0)

以下是一个例子:

header('Content-Type: image/png'); // setting the content-type

$file   = "yourimage.png";
$image  = imagecreatefrompng($file); // creating the image
$font   = "YourFont.ttf";
$size   = 15; //pixels
$color  = imagecolorallocate($image, 255, 255, 255); //white color
$text   = "Your text here"

imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text

imagepng($image); // outputting the image

有关详情,请参阅imagettftext()

编辑:使用多个imagettftext()

的示例
header('Content-Type: image/png'); // setting the content-type

$file   = "yourimage.png";
$image  = imagecreatefrompng($file); // creating the image
$font   = "YourFont.ttf";
$size   = 15; //pixels
$color  = imagecolorallocate($image, 255, 255, 255); //white color
$text       = "Your text here"

imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text

$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text

$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text

imagepng($image); // outputting the image

答案 1 :(得分:0)

这是您的代码,您可以在google tho上轻松找到它。

<?php
    header ("Content-type: image/png");
    $string = "your text"; // Change this text
    $font = 4; // try changing this as well
    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefrompng("/path/to/yourimagefile");
    $x = imagesx($im) - $width ;
    $y = imagesy($im) - $height;
    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y,  $string, $textColor);
    imagepng($im);
?>