imagecopyresized();
我在使用 GD函数显示带有换行符的 textarea 文本时遇到问题。
<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在文本区域,您在这里输入,文本在图像上完美设置!
有人对此有任何想法吗?
编辑:
为此需要帮助,因为我感到困惑。我找到了这个:
<?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;
}
?>
我现在如何使用它来处理我的代码?谢谢