在php gd中缩放字体大小

时间:2014-04-25 06:05:13

标签: php image-processing gd

我正在缩放图像中文本的字体大小,以便文本字符串填充图像宽度。我这样做是通过使用imagettfbbox来确定当前文本框的宽度。这就是我现在正在做的事情:

//original image width and height
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];

$font     ='font/arialbd.ttf';
$fontsize = 12;
$bbox   = imageftbbox($fontsize, 0, $font, $Watermarktext);
$tbwidth = $bbox[2];
$factor = round(($size_x / $tbwidth), 0, PHP_ROUND_HALF_DOWN);

// increase the font size to fit into the image
$fontsize = $fontsize * $factor;


$bbox   = imageftbbox($fontsize, 0, $font, $Watermarktext);             
$watermark_x = $bbox[2] - $bbox[6];
$watermark_y = $bbox[3] - $bbox[7]; 


$watermark = imagecreate( $watermark_x , $watermark_y );

通过使用此代码,我创建的水印仍然不准确。在小图像上,文本仍然不能完全覆盖所有宽度。

为了更好地理解,这是完整的代码:

$font     ='font/arialbd.ttf';
$fontsize = 12;
$bbox   = imageftbbox($fontsize, 0, $font, $Watermarktext);
$tbwidth = $bbox[2];
$factor = round(($size_x / $tbwidth), 0, PHP_ROUND_HALF_DOWN);

// increase the font size to fit into the image
$fontsize = $fontsize * $factor;


$bbox   = imageftbbox($fontsize, 0, $font, $Watermarktext);             
$watermark_x = $bbox[2] - $bbox[6];
$watermark_y = $bbox[3] - $bbox[7]; 


$watermark = imagecreate( $watermark_x , $watermark_y );
imagesavealpha($watermark, true);
imagealphablending($watermark, false);
$transperant = imagecolorallocatealpha($watermark, 255, 255, 255, 127);
imagefill($watermark, 0, 0, $transperant);

$background = imagecolorallocate( $watermark, 32,255,255 );
$text_colour = imagecolorallocatealpha( $watermark, 132,15,153,1);
imagecolortransparent($watermark, $background);
//apply string to watermark image         
imagefttext($watermark,$fontsize,0,-$bbox[6], -$bbox[7],$text_colour,$font,$Watermarktext);

// apply watermark to image
imagecopymerge($image, $watermark, 0, 0, 0, 0, $watermark_x, $watermark_y);

为什么相同的字符串不能覆盖每个宽度?

0 个答案:

没有答案