使用GD库在其上创建包含动态文本的图像

时间:2014-03-14 17:32:25

标签: php image-processing gd

我正在使用GD Library创建文本水印。问题是,当我给它们一个短文本时,它会生成一个正常的图像,但是当文本较大时,图像会变小。请告诉我如何根据文本创建图像,以便它很好地适应。这是我到目前为止使用的代码:

 if(isset($_GET['text']) && !empty($_GET['text']) ){

         $text = $_GET['text'];
         $font     = './NOTTB___.TTF';
         $fontsize = 20;
         $bbox   = imageftbbox($fontsize, 0, $font, $text);
         $width = $bbox[2] - $bbox[6];
         $height = $bbox[3] - $bbox[7];


         $my_img = imagecreate( $width, $height );
         $background = imagecolorallocate( $my_img, 0xff,0xff,0xff );
         $text_colour = imagecolorallocate( $my_img, 0x00,0x00,0x00 );
         imagecolortransparent($my_img, $background);

         //imagettftext($my_img, $fontsize, 0, 30, 25, $text_colour, $font, $text);
         imagefttext($my_img,$fontsize,0,-$bbox[6], -$bbox[7],$text_colour,$font,$text);

         header( "Content-type: image/png" );
         imagepng( $my_img );
         imagecolordeallocate( $text_colour );
         imagecolordeallocate( $background );
         imagedestroy( $my_img );

 }

0 个答案:

没有答案