[英]PHP-GD make line height compatible across all fonts on text wrap
好的,所以我正在使用jquery開發一個簡單的生成器。 用戶輸入他/她選擇的文本,選擇字體,字體顏色和字體大小。 所有這些屬性都實時(實時預覽)顯示在單獨的div上。
現在,我需要將生成的預覽另存為圖片。 為此,我使用php GD庫。 一切正常,但帶有某些字體,一切都搞砸了。
在第一個圖像中,一切看起來都不錯,但是在第二個圖像中,行高剛剛被弄亂了。
這是我用來處理屬性的PHP腳本
<?php
//Width and height of desired image
$width = 320;
$height= 320;
//Create an image with specified height and width
$main_img = ImageCreate($width, $height);
$mx = imagesx($main_img);
$my = imagesy($main_img);
//Capture values from form
$main_text = $_POST['rtext'];
$main_text_size = $_POST['rfsize'];
$color = $_POST['rcolor'];
$mt_f = $_POST['rfont'];
$main_text_x = ($mx/2);
// more code here
//wrap text if text too long
$words = explode(' ', $main_text);
$lines = array($words[0]);
$currentLine = 0;
for($i = 1; $i < count($words); $i++)
{
$lineSize = imagettfbbox($main_text_size, 0, $mt_f, $lines[$currentLine] . ' ' . $words[$i]);
if($lineSize[2] - $lineSize[0] < $mx)
{
$lines[$currentLine] .= ' ' . $words[$i];
}
else
{
$currentLine++;
$lines[$currentLine] = $words[$i];
}
}
$line_count = 1;
// Loop through the lines and place them on the image
foreach ($lines as $line)
{
$line_box = imagettfbbox($main_text_size, 0, $mt_f, "$line");
$line_width = $line_box[0]+$line_box[2];
$line_height = $line_box[1]-$line_box[7];
$line_margin = ($mx-$line_width)/2;
$line_y = (($line_height+12) * $line_count);
imagettftext($main_img, $main_text_size, 0, $line_margin, $line_y, $main_text_color, $mt_f, $line);
// Increment Y so the next line is below the previous line
$line_count ++;
}
header("Content-type: image/png");
//code to download the image
?>
有沒有一種方法可以修改我在其中包裹文本以適應所有字體的代碼部分? 喜歡根據字體自動計算線高嗎?
謝謝,任何幫助將不勝感激
我在phpclasses imagefittext.class.php
http://www.phpclasses.org/browse/file/41869.html上找到了一個非常有用的類。 我還找到了使用類http://www.phpclasses.org/browse/file/41870.html實現的示例腳本。 這正是我想要的。
完美地工作!!! 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.