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() は、画像 im 上の座標 x,y(左上が 0, 0) に color 色で char の最初の文字を描画します。

パラメータ

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 クラスのインスタンスと数値を両方受け入れるようになりました。これより前のバージョンでは、数値のみを受け入れていました。
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
20 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
0
liam dot wiltshire at lineone dot net
16 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