PHP:imagettftext文本右对齐(RTL)
我想文本是“文本对齐权利”PHP:imagettftext文本右对齐(RTL),php,right-to-left,imagettftext,Php,Right To Left,Imagettftext,我想文本是“文本对齐权利” $url=“#”; $input=@file\u get\u contents($url)或die('Fehler!'); 如果(preg_match_all('~\s*(.*?\s*~si',$input,$item_name)); $image=imagecreatefrompng(“bg.png”); imagesavealpha($image,true); imagealphablending($image,true); $finalImage=imagecr
$url=“#”;
$input=@file\u get\u contents($url)或die('Fehler!');
如果(preg_match_all('~\s*(.*?\s*~si',$input,$item_name));
$image=imagecreatefrompng(“bg.png”);
imagesavealpha($image,true);
imagealphablending($image,true);
$finalImage=imagecreatetruecolor(800200);
$font='../arial.ttf';
$color=imagecolorallocate($finalImage,0,0,0);
$color\u time=imagecolorallocate($finalImage,100100100);
imagettftext($image,10,0,23,15,$color,$font,$item_name[1][0]);
imagettftext($image,10,0,22,33,$color,$font,$item_name[1][1]);
imagettftext($image,10,0,22,51,$color,$font,$item_name[1][2]);
imagettftext($image,10,0,22,69,$color,$font,$item_name[1][3]);
imagettftext($image,10,0,22,87,$color,$font,$item_name[1][4]);
imagettftext($image,10,0,22,105,$color,$font,$item_name[1][5]);
imagettftext($image,10,0,22,123,$color,$font,$item_name[1][6]);
imagettftext($image,10,0,22,141,$color,$font,$item_name[1][7]);
imagettftext($image,10,0,22,159,$color,$font,$item_name[1][8]);
imagettftext($image,10,0,22,177,$color,$font,$item_name[1][9]);
imagettftext($image,10,0,22195,$color,$font,$item_name[1][10]);
标题('Content-type:image/png');
imagepng($image);
你知道怎么做吗?
因为我不明白。
我已经谷歌了很多。
你们需要知道,我不是真正的PHP高手,这就是我需要帮助的原因。
如果你有问题,请提问 非常清晰的func版本,带有示例
<?php
function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) {
/************
simple function that calculates the *exact* bounding box (single pixel precision).
The function returns an associative array with these keys:
left, top: coordinates you will pass to imagettftext
width, height: dimension of the image you have to create
*************/
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
// Example usage - gif image output
$text_string = "Hullo World";
$font_ttf = "./fonts/arial.ttf";
$font_size = 22;
$text_angle = 0;
$text_padding = 10; // Img padding - around text
$the_box = calculateTextBox($text_string, $font_ttf, $font_size, $text_angle);
$imgWidth = $the_box["width"] + $text_padding;
$imgHeight = $the_box["height"] + $text_padding;
$image = imagecreate($imgWidth,$imgHeight);
imagefill($image, imagecolorallocate($image,200,200,200));
$color = imagecolorallocate($image,0,0,0);
imagettftext($image,
$font_size,
$text_angle,
$the_box["left"] + ($imgWidth / 2) - ($the_box["width"] / 2),
$the_box["top"] + ($imgHeight / 2) - ($the_box["height"] / 2),
$color,
$font_ttf,
$text_string);
header("Content-Type: image/gif");
imagegif($image);
imagedestroy($image);
?>
对于水平对齐,我们需要从图像的宽度中减去“文本框”的宽度{$tb[2]或$tb[4]},然后减去2
如果你有一个200像素宽的图像,你可以这样做:
<?php
$x = ceil((200 - $tb[2]) / 2); // lower left X coordinate for text
imagettftext($im, 17, 0, $x, $y, $tc, '../arial.ttf', 'Hello world!'); // write text to image
?>
这将为您的文本提供完美的水平居中对齐,给或取1像素。玩得开心
gd只将文本放在您指定的位置。它没有“左对齐”或“右对齐”。你需要自己实现:@MarcB好的,怎么做?阅读链接。学会使用你得到的工具。摆脱“查找函数
准确地做我需要的东西()
”的思维方式,进入使用()的思维方式;简单工具();提供();完成;复杂的事物代码>这没有多大帮助。
<?php
$tb = imagettfbbox(17, 0, 'airlock.ttf', 'Hello world!');
?>
Array
(
[0] => 0 // lower left X coordinate
[1] => -1 // lower left Y coordinate
[2] => 198 // lower right X coordinate
[3] => -1 // lower right Y coordinate
[4] => 198 // upper right X coordinate
[5] => -20 // upper right Y coordinate
[6] => 0 // upper left X coordinate
[7] => -20 // upper left Y coordinate
)
<?php
$x = ceil((200 - $tb[2]) / 2); // lower left X coordinate for text
imagettftext($im, 17, 0, $x, $y, $tc, '../arial.ttf', 'Hello world!'); // write text to image
?>