Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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-Lib-imagettftext包装文本_Php_Image Processing_Gd_Word Wrap_Imagettftext - Fatal编程技术网

使用PHP-GD-Lib-imagettftext包装文本

使用PHP-GD-Lib-imagettftext包装文本,php,image-processing,gd,word-wrap,imagettftext,Php,Image Processing,Gd,Word Wrap,Imagettftext,我正在使用GD lib将文本覆盖到图像上。我想在一个边界框中包装一个字符串,并尽可能地获得文本的最佳匹配 以下是到目前为止我得到的信息: //dimension of the image I'm placing the text on $img_w = imagesx($this->img); $img_h = imagesy($this->img); //Get the dimensions of the text bounding box $bbox = imagettfbbo

我正在使用GD lib将文本覆盖到图像上。我想在一个边界框中包装一个字符串,并尽可能地获得文本的最佳匹配

以下是到目前为止我得到的信息:

//dimension of the image I'm placing the text on
$img_w = imagesx($this->img);
$img_h = imagesy($this->img);

//Get the dimensions of the text bounding box
$bbox = imagettfbbox($size, 0, $font, $text);
$w = (abs($bbox[2])+(abs($bbox[0])));
$h = (abs($bbox[5])+(abs($bbox[3])));
接下来我需要做一些检查。如果<代码> $W> $imggw ,那么我想在字符串中间添加一个换行符。然后再次检查
$w>$img\u w
。如果它仍然太大,那么分成三分之一,依此类推,直到它符合图像宽度

我还需要检查每次添加换行符时$h>$img\u h。如果这是真的,那么我已经没有足够的空间来适应这个大小的图像中的文本。所以我需要开始减小文本大小,直到合适为止

你可以在这里看到与我想要实现的目标相同的东西:

我有一个递归方法来检索文本大小,以便在重叠时将其集中在图像上:

    private function get_text_size($size, $font, $text){

        //dimension of the image I'm placing the text on
        $img_w = imagesx($this->img);
        $img_h = imagesy($this->img);

        //Get the dimensions of the text bounding box
        $bbox = imagettfbbox($size, 0, $font, $text);

        //add some space around the text too
        $w = (abs($bbox[2])+(abs($bbox[0]));
        $h = (abs($bbox[5])+(abs($bbox[3]));


        if( $w > $img_w ){

            //split string in half              
            $tmp = explode(' ', $text);
            $word_count = (count($tmp)/2);
            $tmp[$word_count] .= "\n";

            //rebuild the string with the line break(s) and check the size again.
            $text = '';
            foreach($tmp as $word){
                $text .= $word.' ';
            }               

            return $this->get_text_size($size, $font, $text);
        }

        return array($size, $w, $h);
    }
这让我陷入了一个无限循环,就像断线不起作用一样。我检查过类似的问题(,),但没有一个真正解决这个问题


我有点期待有一个简单的函数来实现这一点,但我找不到,也找不出最好的方法。

我遇到了同样的问题,我找到了这个PHP库:

这有助于将文本包装到文本框中,您可以如下设置(从开发人员示例复制和修改):

$background = 'image/background.jpg';
$image = new PHPImage();
$image->setDimensionsFromImage($background );
$image->draw($background );

$image->setFont('font/arial.ttf');
$image->setTextColor(array(255, 255, 255));
$image->setStrokeWidth(1);
$image->setStrokeColor(array(0, 0, 0));
$image->textBox('Auto wrap and scale font size to multiline text box width and height bounds. Vestibulum venenatis risus scelerisque enim faucibus, ac pretium massa condimentum. Curabitur faucibus mi at convallis viverra. Integer nec finibus ligula, id hendrerit felis.', array(
    'width' => 150,
    'height' => 140,
    'fontSize' => 16, // Desired starting font size
    'x' => 50,
    'y' => 150
));
$image->show();
//$image->save("image/temp.jpg");//If you want to save image instead of show it