imagechar
(PHP 4, PHP 5)
imagechar — Draw a character horizontally
Description
$image
, int $font
, int $x
, int $y
, string $c
, int $color
)
imagechar() draws the first character of
c
in the image identified by
image
with its upper-left at
x
,y
(top left is 0,
0) with the color color
.
Parameters
-
image
-
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
-
font
-
Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().
-
x
-
x-coordinate of the start.
-
y
-
y-coordinate of the start.
-
c
-
The character to draw.
-
color
-
A color identifier created with imagecolorallocate().
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 imagechar() example
<?php
$im = imagecreate(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);
?>
The above example will output something similar to:
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Обработка и генерация изображений
- Обработка изображений и GD
- gd_info
- getimagesize
- getimagesizefromstring
- image_type_to_extension
- image_type_to_mime_type
- image2wbmp
- imageaffine
- imageaffinematrixconcat
- imageaffinematrixget
- imagealphablending
- imageantialias
- imagearc
- imagechar
- imagecharup
- imagecolorallocate
- imagecolorallocatealpha
- imagecolorat
- imagecolorclosest
- imagecolorclosestalpha
- imagecolorclosesthwb
- imagecolordeallocate
- imagecolorexact
- imagecolorexactalpha
- imagecolormatch
- imagecolorresolve
- imagecolorresolvealpha
- imagecolorset
- imagecolorsforindex
- imagecolorstotal
- imagecolortransparent
- imageconvolution
- imagecopy
- imagecopymerge
- imagecopymergegray
- imagecopyresampled
- imagecopyresized
- imagecreate
- imagecreatefromgd2
- imagecreatefromgd2part
- imagecreatefromgd
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromwbmp
- imagecreatefromwebp
- imagecreatefromxbm
- imagecreatefromxpm
- imagecreatetruecolor
- imagecrop
- imagecropauto
- imagedashedline
- imagedestroy
- imageellipse
- imagefill
- imagefilledarc
- imagefilledellipse
- imagefilledpolygon
- imagefilledrectangle
- imagefilltoborder
- imagefilter
- imageflip
- imagefontheight
- imagefontwidth
- imageftbbox
- imagefttext
- imagegammacorrect
- imagegd2
- imagegd
- imagegif
- imagegrabscreen
- imagegrabwindow
- imageinterlace
- imageistruecolor
- imagejpeg
- imagelayereffect
- imageline
- imageloadfont
- imagepalettecopy
- imagepalettetotruecolor
- imagepng
- imagepolygon
- imagepsbbox
- imagepsencodefont
- imagepsextendfont
- imagepsfreefont
- imagepsloadfont
- imagepsslantfont
- imagepstext
- imagerectangle
- imagerotate
- imagesavealpha
- imagescale
- imagesetbrush
- imagesetinterpolation
- imagesetpixel
- imagesetstyle
- imagesetthickness
- imagesettile
- imagestring
- imagestringup
- imagesx
- imagesy
- imagetruecolortopalette
- imagettfbbox
- imagettftext
- imagetypes
- imagewbmp
- imagewebp
- imagexbm
- iptcembed
- iptcparse
- jpeg2wbmp
- png2wbmp
Коментарии
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);
?>
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");
?>