更改ImageString使用的字体

时间:2013-07-02 10:26:54

标签: php

我有这个PHP代码:

<?php 

//Other part of code


Header("Content-type: image/png");  
$im = imagecreatefromPng("./images/signatures/background.png");  


$red = ImageColorAllocate($im, 255, 0, 0); 
$black = ImageColorAllocate($im, 0, 0, 0); 


ImageString($im, 5, 15, 5, "$callsign", $black); 
ImageString($im, 5, 15, 20, "$name $surname", $black); 
ImageString($im, 5, 15, 35, "Location: $location", $black); 
ImageString($im, 5, 15, 50, "HUB: $hub", $black); 
ImageString($im, 5, 15, 65, "Hours: $hours", $black); 


$font_width = ImageFontWidth(5);  

ImagePng($im);  
?>

我想更改PHP用于在图像中写入的字体。我怎样才能做到这一点??我尝试但我不能。

2 个答案:

答案 0 :(得分:3)

您可以使用imageloadfont();

语法:

 int imageloadfont ( string $file )

示例:

// Create a new image instance
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Make the background white
imagefilledrectangle($im, 0, 0, 49, 19, $white);

// Load the gd font and write 'Hello'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);

// Output to browser
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);

所以请将您的代码更改为:

//Other part of code


Header("Content-type: image/png");  
$im = imagecreatefromPng("./images/signatures/background.png");  
$font = imageloadfont('./fonts/arial.gdf');//change the parameter based on your font file name

$red = ImageColorAllocate($im, 255, 0, 0); 
$black = ImageColorAllocate($im, 0, 0, 0); 


ImageString($im, $font, 15, 5, "$callsign", $black); 
ImageString($im, $font, 15, 20, "$name $surname", $black); 
ImageString($im, $font, 15, 35, "Location: $location", $black); 
ImageString($im, $font, 15, 50, "HUB: $hub", $black); 
ImageString($im, $font, 15, 65, "Hours: $hours", $black); 


$font_width = ImageFontWidth(5);  

ImagePng($im); 

答案 1 :(得分:2)

假设你的意思是字体:

http://www.php.net/manual/en/function.imageloadfont.php

http://php.net/manual/en/function.imagettftext.php如果您使用ttf字体

,这可能会有所帮助