2

phpで画像を作りたいです。背景画像があります。その上に、テキストを書きたい。テキストは中央に配置する必要があります (垂直方向の配置)。また、テキストが境界線からはみ出す場合は、テキストが壊れる必要があります。

どうやってやるの?

この文字列がある場合:

$text = "This is a sample text. This is a second sample text.";

これは結果になるはずです:

私が何を必要としているのか理解していただければ幸いです。


編集:

これは私が作ったものです。中央と真ん中にあります。しかし、テキストは途切れません。これどうやってするの?

<?php

header("Content-type: image/png");

//settings
$text = 'This is a sample text. This is a second sample text.';
$width = 200;
$height = 200;

//create image
$im = imagecreate($width, $height);

//colors
$colorWhite = imagecolorallocate($im, 255, 255, 255);
$colorBlack = imagecolorallocate($im, 0, 0, 0);
$colorGrey = imagecolorallocate($im, 207, 199, 199);

//border
imagerectangle($im, 0, 0, $width - 1, $height - 1, $colorGrey);

//fontsize
$fontSize = 3;
$font_width = imagefontwidth($fontSize);
$font_height = imagefontheight($fontSize);

//text size
$text_width = $font_width * strlen($text);
$text_height = $font_height;

//align: center 
$position_center = ceil(($width - $text_width) / 2);

//valign: middle
$position_middle = ceil(($height - $text_height) / 2);

imagestring($im, $fontSize, $position_center, $position_middle, $text, $colorBlack);
imagepng($im);

?>

これはコードの結果です:

結果

4

1 に答える 1