imagechar

(PHP 4, PHP 5, PHP 7, PHP 8)

imagechar水平地绘制一个字符

说明

imagechar(
    GdImage $image,
    GdFont|int $font,
    int $x,
    int $y,
    string $char,
    int $color
): bool

imagechar()char 的首个字符绘制在 image 标识的图像中,其左上角位于 xy(图像左上角为 0, 0),颜色为 color

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

font

取值对于内建的 latin2 编码字体可以是:1、2、3、4、5(更高的数字对应更大的字体), 或是通过 imageloadfont() 返回的 GdFont 实例。

x

起点的 x 坐标。

y

起点的 y 坐标。

char

要绘制的字符。

color

颜色标识符使用 imagecolorallocate() 创建。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.1.0 font 参数现在接受 GdFont 实例和 int,之前仅接受 int
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource

示例

示例 #1 imagechar() 示例

<?php

$im
= imagecreate(100, 100);

$string = 'PHP';

$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// 在左上角打印一个黑色“P”
imagechar($im, 1, 0, 0, $string, $black);

header('Content-type: image/png');
imagepng($im);

?>

以上示例的输出类似于:

输出示例:imagechar()

参见

add a note

User Contributed Notes 2 notes

up
3
sw at profilschmiede dot de
19 years ago
For the sake of completeness, here is an example for imagechar.
The base-image automatically adjusts to the size and the height of the given string. Using the rand()-function the y-position of each char is slightly varied with every loop-run. You can easily rewrite the script to use a randomly generated string - the one given here just serves as an example.

<?php

$string
= '1 2 3 4 5 6 7 8 9 A B C D E F G';
$font_size = 5;
$width=imagefontwidth($font_size)*strlen($string);
$height=imagefontheight($font_size)*2;
$img = imagecreate($width,$height);
$bg = imagecolorallocate($img,225,225,225);
$black = imagecolorallocate($img,0,0,0);
$len=strlen($string);

for(
$i=0;$i<$len;$i++)
{
$xpos=$i*imagefontwidth($font_size);
$ypos=rand(0,imagefontheight($font_size));
imagechar($img,$font_size,$xpos,$ypos,$string,$black);
$string = substr($string,1);

}
header("Content-Type: image/gif");
imagegif($img);
imagedestroy($img);
?>
up
-3
liam dot wiltshire at lineone dot net
15 years ago
A quick function to automatically generate a multi line image from a string, with the image size automatically calculated from the string itself.

<?php

function multilineimage($string){

// Probably not the best way of handling newlines, but bar OS9, doesn't really cause a problem
$string = str_replace("\r","",$string);
$string = explode("\n",$string);

$maxlen = 0;
foreach (
$string as $str){
if (
strlen($str) > $maxlen){
$maxlen = strlen($str);
}
}

// Set font size
$font_size = 4;

// Create image width dependant on width of the string
$width = imagefontwidth($font_size)*$maxlen;
// Set height to that of the font
$height = imagefontheight($font_size) * count($string);
// Create the image pallette
$img = imagecreate($width,$height);
// Grey background
$bg = imagecolorallocate($img, 205, 255, 255);
// White font color
$color = imagecolorallocate($img, 0, 0, 0);

$ypos = 0;

foreach (
$string as $str){

$len = strlen($str);
for(
$i=0;$i<$len;$i++){
// Position of the character horizontally
$xpos = $i * imagefontwidth($font_size);
// Draw character
imagechar($img, $font_size, $xpos, $ypos, $str, $color);
// Remove character from string
$str = substr($str, 1);
}
$ypos = $ypos + imagefontheight($font_size);
}

// Return the image
header("Content-Type: image/gif");
imagegif($img);
// Remove image
imagedestroy($img);
}

multilineimage("This is an image
This is line 2\nLine 3
Line 4"
);

?>
To Top