Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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-在多行上转换单行字符串,然后插入到图像中_Php_Image_Text_Gd_Multiline - Fatal编程技术网

php-在多行上转换单行字符串,然后插入到图像中

php-在多行上转换单行字符串,然后插入到图像中,php,image,text,gd,multiline,Php,Image,Text,Gd,Multiline,我正在编写一个脚本,在图像上插入一个文本,它可以工作,但不能完全工作,也就是说,如果用户在文本区域中添加一个换行符就可以了,但是如果所有文本都在一行上,则看起来很糟糕。这是我的密码 $str="this is a string inserted by an user"; $img_width= 500; $img_height= 500; $font_size = 1; $txt_max_width = intval(0.6 * $img_width); do { $font_size

我正在编写一个脚本,在图像上插入一个文本,它可以工作,但不能完全工作,也就是说,如果用户在文本区域中添加一个换行符就可以了,但是如果所有文本都在一行上,则看起来很糟糕。这是我的密码

$str="this is a string inserted by an user";
$img_width= 500;
$img_height= 500;
$font_size = 1; 
$txt_max_width = intval(0.6 * $img_width);
do {
    $font_size++;
    $p = imagettfbbox($font_size,0,$font,$str);
    $txt_width=$p[2]-$p[0];
    } while ($txt_width <= $txt_max_width);
    $y = $img_height * 0.4;
$x = ($img_width - $txt_width) / 2;
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, $font_size, 0, $x, $y, $white, $font, $str);
imagepng($img);
imagedestroy($img);
看看这个,明白吗

PS:对不起,我的英语不好

function ApplyLineBreaks(strTextAreaId) {
var oTextarea = document.getElementById(strTextAreaId);
if (oTextarea.wrap) {
    oTextarea.setAttribute("wrap", "off");
}
else {
    oTextarea.setAttribute("wrap", "off");
    var newArea = oTextarea.cloneNode(true);
    newArea.value = oTextarea.value;
    oTextarea.parentNode.replaceChild(newArea, oTextarea);
    oTextarea = newArea;
}

var strRawValue = oTextarea.value;
oTextarea.value = "";
var nEmptyWidth = oTextarea.scrollWidth;

function testBreak(strTest) {
    oTextarea.value = strTest;
    return oTextarea.scrollWidth > nEmptyWidth;
}
function findNextBreakLength(strSource, nLeft, nRight) {
    var nCurrent;
    if(typeof(nLeft) == 'undefined') {
        nLeft = 0;
        nRight = -1;
        nCurrent = 64;
    }
    else {
        if (nRight == -1)
            nCurrent = nLeft * 2;
        else if (nRight - nLeft <= 1)
            return Math.max(2, nRight);
        else
            nCurrent = nLeft + (nRight - nLeft) / 2;
    }
    var strTest = strSource.substr(0, nCurrent);
    var bLonger = testBreak(strTest);
    if(bLonger)
        nRight = nCurrent;
    else
    {
        if(nCurrent >= strSource.length)
            return null;
        nLeft = nCurrent;
    }
    return findNextBreakLength(strSource, nLeft, nRight);
}

var i = 0, j;
var strNewValue = "";
while (i < strRawValue.length) {
    var breakOffset = findNextBreakLength(strRawValue.substr(i));
    if (breakOffset === null) {
        strNewValue += strRawValue.substr(i);
        break;
    }
    var nLineLength = breakOffset - 1;
    for (j = nLineLength - 1; j >= 0; j--) {
        var curChar = strRawValue.charAt(i + j);
        if (curChar == ' ' || curChar == '-' || curChar == '+') {
            nLineLength = j + 1;
            break;
        }
    }
    strNewValue += strRawValue.substr(i, nLineLength) + "\n";
    i += nLineLength;
}
oTextarea.value = strNewValue;
oTextarea.setAttribute("wrap", "");
}
此函数接受textarea的ID作为其参数,每当有换行符时,它都会在textarea中插入一个新的换行符。在表单submit中运行函数,您将获得服务器端代码中带有适当换行符的文本

这样,文本将完全按照用户在制作图像之前看到的方式显示。用户无需猜测这些线将如何断开


这个函数是在另一个答案中找到的:

您想要做的是将字符串分割成更小的块或块,为此,您可以使用PHP

您可以这样使用它:

// initialize variables
$final_string = false;
$size_of_the_chunk = 20;
$str = "_this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string__this_is_a_very_long_string_";
$length_of_string = strlen( $str );

// work only if necessary
if ( $length_of_string > $size_of_the_chunk ) {
    $final_string = chunk_split( $str, $size_of_the_chunk );
} else {
    $final_string = $str;
}

您可能会遇到的主要问题是,字符串将在随机位置被拆分,至少从用户的角度来看是随机的,因此,片段可能没有多大意义。尝试通过通知用户应该使用换行符来改进用户界面。

谢谢@Kyle,但我认为在服务器端包装文本比在客户端包装文本更好,问题是idk如何做,我还是会尝试的