0

我有这个 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 用于在图像中写入的字体。我怎样才能做到这一点??我尝试但我做不到。

4

2 回答 2

2

你可以使用 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); 
于 2013-07-02T10:30:51.037 回答
2

假设你的意思是字体:

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

http://php.net/manual/en/function.imagettftext.php如果您使用 ttf 字体,这可能会有所帮助

于 2013-07-02T10:31:05.053 回答