繁体   English   中英

如何使用 PHP GD 将文本写入不同大小的图像

[英]How to write text to images with different sizes using PHP GD

我正在创建不同大小的图像。 如何在这些图像上书写文字,使文字始终适合图像?

$text = "some text as example";
$font = "arialbd.ttf"
$fontsize = 26;
offset_x = 0;
offset_y = 0;

$image01 = imagecreate( 1120 , 900 );
$image02 = imagecreate( 400 , 300 ); 
$image03 = imagecreate( 1120 , 900 );

我知道有imagefttext函数可以将文本应用于图像,但使用该函数我只能更改字体大小以使文本更大。 我怎样才能知道它适合我的形象?

如果您希望缩放字体大小以便文本字符串填充图像宽度,请尝试此操作。 使用 imagettfbbox,确定当前文本框的宽度:

 $bbox = imagettfbbox($fontsize,0,$fontpath,$string);
 $tbwidth = $bbox[2];

然后将图像宽度除以 $tbwidth 得到一个因子

 $factor = round(($imgwidth / $tbwidth), 0, PHP_ROUND_HALF_DOWN); //ie. 800/240=3

将 $factor 乘以 $fontsize 以获得近似增加。

 $newfontsize = $fontsize * $factor; //ie. 12(pt) * 3 = 36

请记住,如果您使用的是 GD 2.0,则字体大小以磅为单位而不是像素。 您的算法将计算默认字体大小文本框(表示为文本框宽度)与图像宽度之间的差异。

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(800, 600);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font_file = 'arial.ttf';
$font_size = '15';

$bbox   = imageftbbox($font_size, 0, $font_file, $text);

$width  = $bbox[2] - $bbox[6];
$height = $bbox[3] - $bbox[7];

// Add the text
imagettftext($im, $font_size, 0, 10, 20, $black, $font_file, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

使用 imageftbbox 函数获取文本边界框的大小。 然后,您可以调整文本大小以完全适合图像的大小。

http://www.php.net/manual/en/function.imageftbbox.php

我最近在透明背景下遇到了同样的情况,但当前的例子不是解决方案而是线索,或者是一个不起作用的解决方案,所以如果有人需要的话,这里是一个组合和有效的解决方案。

function imagecreater($width = 600, $height = 600) {

    //Create an empty transparent image
    $img = imagecreatetruecolor($width, $height);
    imagealphablending($img, false);
    imagesavealpha($img, true);
    $transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $transparent);

    //Text information
    $text = "some text as example";
    $font = "arialbd.ttf"
    $fontsize = 26; //default font to be altered later


    //simulate a complete text box and get the width
    $bbox = imageftbbox($fontsize, 0, $font, $text);
    $tbwidth = $bbox[2]; 

    //Calculate different between our transparent image and text box
    //I've added a little padding (20px) since the text sometimes crossing the edge.. 
    $factor = (($width - 20) / $tbwidth);

    //Alter font size with difference to fit fully image
    $newfontsize = $fontsize * $factor;

    //Find the horisontal center
    $bbox = imageftbbox($newfontsize, 0, $font, $text);
    $newheight = ($height / 2) + (($bbox[3] - $bbox[7]) / 2);

    //Set Color of text
    $color = imagecolorallocate($img, 200, 0, 0);

    //Produce our image
    imagettftext($img, $newfontsize, 0, 0, $newheight, $color, $font, $text);

    //Copy image to file and free the cached image
    $target = "testimage.png";
    imagepng($img, $target);
    imagedestroy($img);
}

正如rwhite35在这里提到的,请记住 GD 2.0 写入字体大小以为单位而不是像素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM