0

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-->

コードは機能し、透過テキストは完璧です。しかし、テキストエリアにどれだけ長く入力しても、最終的な画像にテキストが1行で印刷されます! 改行を取得する唯一の方法は、「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;
}
?>

これを使用してコードを操作するにはどうすればよいですか? ありがとうございました

4

0 に答える 0