使用php gd将文本写入不同大小的图像

问题描述 投票:1回答:4

我正在创建不同大小的图像如何在每次文本完全适合图像时在图像上书写文本

$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函数可以将文本应用到图像,但在该函数中只增加字体大小我可以使文本更大但我怎么能发现它适合我的图像

php gd
4个回答
1
投票

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

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

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

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

$ fontsize多次使用$ factor来获得近似增加。

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

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


1
投票
// 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);

0
投票

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

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


0
投票

我最近遇到了透明背景的相同情况,但目前的例子要么不是解决方案,要么是线索或解决方案不起作用,因此如果有人需要它,这是一个综合的工作解决方案。

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写字体大小是以点为单位而不是像素。

© www.soinside.com 2019 - 2024. All rights reserved.