imagechar

(PHP 3, PHP 4, PHP 5)

imagechar -- 水平地画一个字符

说明

bool imagechar ( resource image, int font, int x, int y, string c, int color )

imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 xy(图像左上角为 0, 0),颜色为 color。如果 font 是 1,2,3,4 或 5,则使用内置的字体(更大的数字对应于更大的字体)。

例子 1. imagechar() 例子

<?php

$im
= imagecreatetruecolor(100,100);

$string = 'PHP';

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

// prints a black "P" in the top left corner
imagechar($im, 1, 0, 0, $string, $black);

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

?>

参见 imagecharup()imageloadfont()


add a note add a note User Contributed Notes
sw at profilschmiede dot de
20-Jun-2005 11:35
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);
?>