textarea与GD库断行

时间:2013-09-14 20:01:16

标签: javascript php html

我在使用tex imagecopyresized();函数处理使用换行符的文本时遇到问题。

<form action="" method="post"> <!--Form Start-->
<textarea name="description">
</textarea>
<input type="submit" value="submit">
                                 <!--php Start-->
<?php 
$image = imagecreatefrompng('bg.png'); //Open background png image

//Setting "text" from textarea,"font" from ttf file,"font size" with int
$text = $_POST['description']; 
$font = 'font.ttf';
$size = 10;

// Create textbox and coordinates
$bbox = imageftbbox($size,0,$font,$text);

$w = $bbox[2]+1 -$bbox[6]+2;
$h = $bbox[3]+1 -$bbox[7]+2;

//Creating new blank image sized by the textbox
$im = imagecreatetruecolor($w,$h);

//Adding transparent background color for the new image
$black = imagecolorallocate($im,0,0,0);
$bg = imagecolortransparent($im,$black);

//Creating new image using also the transparent color for the text
//The resault is a transparent image 
imagefttext($im,$size,0,-$bbox[6]+1,-$bbox[7]+1,$bg,$font,$text]);

//Outputing the image as a png file
imagepng($im,'text_size_image.png',9);

//Open the new file... Again!!
$im2 = imagecreatefrompng('text_size_image.png');

//Setting the alpha settings
imagealphablending($im2,false);
imagesavealpha($im2,true);

//Settin colors for the saved-open image with transparent background
$black2 = imagecolorallocate($im2,0,0,0);
imagecolortransparent($im2,$black2);
$almost_black = imagecolorallocate($im2,43,43,43);

//Creating the text again with visuable color $almost_black
imagefttext($im2,$size,0,-$bbox[6]+1,-$bbox[7]+1,$almost_black,$font,$text);

//Setting a resizer code
if ($w > 272 || $h > 53) {
$new_width = $w -272;
$final_width = 272 + $new_width;

$new_height = $h - 53;
$final_height = $h - $new_height;
}else {
$new_width = 272 - $w;
$final_width = $new_width + $w;

$new_height = 53 - $h;
$final_height = $new_height + $h;
}

//Merging the two images by resizing the image with the text
imagecopyresized($image,$im2,30,384,1,20,$new_width,$new_height,272,53);

//Outputing the final resault
imagepng($image,'final.png',9);

//Free ram memory
imagedestroy($image);
imagedestroy($im);
imagedestroy($im2);
?>
</form> <!--The end of the form-->

<img src="final.png"> <!--outputing the image on the browser-->

代码有效,透明文字完美无瑕。但无论我在textarea上打字多久,最终图片上的文字打印都在一行!获得换行符的唯一方法是输入“enter”。我该如何解决?

例如,看看这里:http://yugiohcardmaker.net在textarea这里输入,文字在图像上设置完美!

有任何想法吗?

编辑:

需要帮助这个原因我感到困惑。我发现了这个:

<?php
function wrap($fontSize, $angle, $fontFace, $string, $width){

    $ret = "";

    $arr = explode(' ', $string);

foreach ( $arr as $word ){

    $teststring = $ret.' '.$word;
    $testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
    if ( $testbox[2] > $width ){
        $ret.=($ret==""?"":"\n").$word;
    } else {
        $ret.=($ret==""?"":' ').$word;
    }
}

return $ret;
}
?>

我现在如何使用它来处理我的代码?谢谢

0 个答案:

没有答案