PHP Conference Japan 2024

imagechar

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

imagechar水平繪製字元

說明

imagechar(
    GdImage $image,
    GdFont|int $font,
    int $x,
    int $y,
    字串 $char,
    整數 $color
): 布林值

imagechar() 函式會在由 image 指定的影像中,繪製 char 的第一個字元,其左上角位於 xy 座標(左上角為 0, 0),並使用 color 指定的顏色。

參數

image

一個 GdImage 物件,由其中一個影像建立函式(例如 imagecreatetruecolor())所返回。

font

可以是 1、2、3、4、5,代表使用 latin2 編碼的內建字型(數字越大,字型越大),或是由 imageloadfont() 返回的 GdFont 實例。

x

起始的 x 座標。

y

起始的 y 座標。

char

要繪製的字元。

color

使用 imagecolorallocate() 建立的顏色識別碼。

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.1.0 font 參數現在接受 GdFont 實例和 整數;先前只接受 整數
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源

範例

範例 #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);

?>

上述範例會輸出類似以下的結果

Output of example : imagechar()

另請參閱

新增註解

使用者貢獻的註解 2 則註解

sw at profilschmiede dot de
19 年前
為了完整性,這裡提供一個 imagechar 的範例。
基礎圖像會自動調整大小和給定字串的高度。使用 rand() 函式,每個字元的 y 位置會在每次迴圈執行時略有變化。您可以輕鬆地重寫腳本以使用隨機產生的字串 - 這裡提供的只是一個範例。

<?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);
?>
liam dot wiltshire at lineone dot net
15 年前
一個可以根據字串自動產生多行圖片的快速函式,圖片大小會根據字串本身自動計算。

<?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