好的,所以過去幾天我一直在爲這件事而頭疼。while while循環訪問一個數組,並在此期間向前移動然後向前移動?
所以我有這三個串
$string_one = 'Some text';
$string_two = 'Some more text';
$string_three = 'Even more text';
哪個正在經歷imagettfbbox()
設置其高度和寬度。
我想我的GD圖像的輸出是Some text Some more text Even more text
,所以基本上我希望他們都在一行上。所以我試圖根據以前的字符串長度來計算每個字符串的x位置,這必須是相對於和位置的。
目前,我正在努力做到以下幾點:
- 添加兩個(或更多)寬度一起
- 負荷蘭國際集團從
$x
的答案除以總,這給出了線上第一個字符串的起點。 - 要設置第二個字符串的起點,我將第一個字符串的長度添加到起點。
但是,爲了做到這一點,我不得不回到先前的值,獲取寬度,將其添加到當前寬度併除以二,然後設置前一個和當前數組的值變量。因此,每當while循環,我想要移回數組指針,獲取寬度值,確定x位置,然後向前移動數組指針並設置x位置。所以我試圖用prev()
和next()
遍歷數組。
我做了以下內容:
$gb
是根據for循環會在哪個字符串設定。所以如果是第二種情況,它會回到第一個。如果它是第五回3,但在這裏並不重要。
$o
是,在while循環的結束迭代,這兩個for循環坐在計數器
$text
是一個2維陣列啓動是這樣的:
Array
(
[ven-pub] => Array
(
[value] =>
[font-size] => 30
)
[More array values]....
)
但我m目的是爲了看起來像這樣到底
Array
(
[ven-pub] => Array
(
[value] =>
[font-size] => 30
[h] => 38
[w] => 514
[x] => 1514
[y] => 1611
)
[More array values]....
)
我會在代碼中評論我想要做的,但假設這是循環遍歷第二個數組變量。
$gb = 1;//In this instance $gb is going back once.
$o = 1;//Set the counter to one, because count will not include 0, right?
$arr_c = count($text);//Set the maximum number of times we should loop.
while($o <= $arr_c){//Meanwhile...
$key = key($text);//Getting the current key
$text[$key]['font-size'] = $text[$key]['font-size']*3;//Text related stuff
//Getting the height and width of my string
$dimensions = imagettfbbox($text[$key]['font-size'], 0, $font, $text[$key]['value']);
$text[$key]['h'] = ($dimensions[7]-$dimensions[1])*-1;//Setting the height...
$text[$key]['w'] = ($dimensions[2]-$dimensions[0]);//...and width
for($gb;$gb<=$o;$gb++){
$key = key($text);//I've found getting the key again helps..
/* Here I'm looping backward just once using prev()
and adding the widths into one variable */
$widths[] = $text[$key]['w'];
prev($text);
}
$total_width = array_sum($widths);
$x = ($new_x - ($total_width/2)); //Dividing it by two to get my start position.
/*Setting the current $key's ['x'] position, which should be
the first array variable, not the second.*/
$text[$key]['x'] = $x;
$gb = $cgb;//Resets $gb to its original number.
$combined_widths[] = $text[$key]['w'];//Saving the starting width for the next array variable
for($gb;$gb<=$o;$gb++){
next($text);//Moving forwards to the next array variable.
/* Adding the previous array variables width to $x to generate the
second array variables ['x'] position.
*/
$text[$key]['x'] = $x+array_sum($combined_widths);
$combined_widths[] = $text[$key]['w'];//Adding the new variables width to the previous
}
//By here, the array pointer should be back where it started for this loop.
next($text)//Move the array pointer forward ready for the next loop.
$o++
}
我的問題出現時,它開始要麼回到很遠,要麼告訴我索引是未定義的。由此可笑地困惑。
嗨,對不起,我不是那麼簡潔。 '$ new_x'是繪圖區域的中間部分;所以我在該圖像上有一個圖像和一個盒子,專門用於寫文本,'$ new_x'是位於盒子中心的座標,我想寫我的文本。因爲我不知道該行的總長度,因爲循環未查看未來變量的寬度,所以我不能給出第一個變量的x位置。 – Gary