PHP GD ttftext中心对齐 [英] PHP GD ttftext center alignment

查看:89
本文介绍了PHP GD ttftext中心对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 imagettftext 制作一个条形图和每个栏的顶部我想要放置



每个栏的以下变量(其实是矩形)

$ x1
$ y1
$ x2
$ y2
$ imagesx
$ imagesy
$ font_size



此外, fontsize应该随着字符串长度增加。



解决方案

像这样做。请记住将字体文件arial.ttf放在当前目录中:

 <?php 
// Create一个650x150的图像并创建两种颜色
$ im = imagecreatetruecolor(650,150);
$ white = imagecolorallocate($ im,255,255,255);
$ black = imagecolorallocate($ im,0,0,0);

//设置背景为白色
imagefilledrectangle($ im,0,0,649,149,$ white);

//我们字体文件的路径
$ font ='./arial.ttf';

//测试它
($ i = 2; $ i <10; $ i ++)
WriteTextForMe($ im,$ font,str_repeat($ i,$ i),-140 +($ i * 80),70 + rand(-30,30),-160 +(($ i + 1)* 80),150,$ black);

//此函数执行魔术
函数WriteTextForMe($ im,$ font,$ text,$ x1,$ y1,$ x2,$ y2,$ allocatedcolor)
{
//拉杆
imagesetthickness($ im,2);
imagerectangle($ im,$ x1,$ y1,$ x2,$ y2,imagecolorallocate($ im,100,100,100));

//用动态伸缩绘制文本
$ maxwidth = $ x2 - $ x1;
($ size = 1; true; $ size + = 1)
{
$ bbox = imagettfbbox($ size,0,$ font,$ text);
$ width = $ bbox [2] - $ bbox [0];
if($ width - $ maxwidth> 0)
{
$ drawsize = $ size - 1;
$ drawX = $ x1 + $ lastdifference / 2;
休息;
}
$ lastdifference = $ maxwidth - $ width;
}
$ size--;
imagettftext($ im,$ drawsize,0,$ drawX,$ y1 - 2,$ allocatedcolor,$ font,$ text);
}

//输出到浏览器
header('Content-type:image / png');

imagepng($ im);
imagedestroy($ im);
?>

它使用 imagettfbbox 函数来获取宽度文本,然后循环显示字体大小以获得正确的大小,将其居中并显示出来。



因此,它会输出以下内容:




I'm using imagettftext to make a bar graph and at the top of each bar I want to put the value.

I have the following variables for each bar (which are really rectangles)

$x1 $y1 $x2 $y2 $imagesx $imagesy $font_size

Also, The fontsize should decrease as the string length increases.

解决方案

Do it like this. Remember to place the font file "arial.ttf" in current directory:

<?php
// Create a 650x150 image and create two colors
$im = imagecreatetruecolor(650, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Set the background to be white
imagefilledrectangle($im, 0, 0, 649, 149, $white);

// Path to our font file
$font = './arial.ttf';

//test it out
for($i=2;$i<10;$i++)
    WriteTextForMe($im, $font, str_repeat($i, $i), -140 + ($i*80), 70 + rand(-30, 30), -160 + (($i+1)*80), 150, $black);

//this function does the magic
function WriteTextForMe($im, $font, $text, $x1, $y1, $x2, $y2, $allocatedcolor)
{
    //draw bars
    imagesetthickness($im, 2);
    imagerectangle($im, $x1, $y1, $x2, $y2, imagecolorallocate($im, 100,100,100));

    //draw text with dynamic stretching
    $maxwidth = $x2 - $x1;
    for($size = 1; true; $size+=1)
    {
        $bbox = imagettfbbox($size, 0, $font, $text);
        $width = $bbox[2] - $bbox[0];
        if($width - $maxwidth > 0)
        {
            $drawsize = $size - 1;
            $drawX = $x1 + $lastdifference / 2;
            break;
        }
        $lastdifference = $maxwidth - $width;
    }
    $size--;
    imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
}

// Output to browser
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

It uses imagettfbbox function to get width of the text, then loops over the font size to get correct size, centers it and displays it.

So, it outputs the following:

这篇关于PHP GD ttftext中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆