Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php gd中字体大小的缩放_Php_Image Processing_Gd - Fatal编程技术网

php gd中字体大小的缩放

php gd中字体大小的缩放,php,image-processing,gd,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 = image

我正在缩放图像中文本的字体大小,以便文本字符串填充图像宽度。我使用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);

为什么同一个字符串不能覆盖所有宽度?

什么是$factor中的$size\u x=round$size\u x/$tbwidth,0,PHP\u round\u HALF\u DOWN$size_x是原始图像的宽度,您可以使用以下代码。