Question

I'm programming a script that inserts a text on an image, it works but not entirely, ie if a user adds a line break in the textarea is fine, but if all the text is on one line looks bad . This is my code

$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);

Look at this for understand http://app.xskarx.com

PS: sorry for my bad english

Was it helpful?

Solution

What you want to do, is to split the string into smaller block or chunks, for that, you have chunk_split in PHP.

You will use it this way:

// 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;
}

The main problem you may have, is that the string will get split on random places, at least random from the perspective of the user, and so, the fragments may not make much sense. Try to improve the user interface by informing that they should use line breaks.

OTHER TIPS

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", "");
}

This function accepts the ID of the textarea as its parameter and whenever there is word wrap, it pushes a new line break into the textarea. Run the function in the form submit and you will get the text with proper line breaks in the server side code.

This way the text will appear exactly as the user sees it before the image is made. There is no need for the user to guess how the lines will break.

This function was found within another answer: finding "line-breaks" in textarea that is word-wrapping ARABIC text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top