2012-07-25 68 views
0

我有一個腳本可以創建一個圖像,其高度取決於遞歸樹的深度。預先計算圖像的高度會很痛苦,因爲我需要運行兩次遞歸函數。因此,我想知道是否可以從我的唯一遞歸函數中調整圖像大小。長話短說,我需要調整圖像資源的大小,這有可能嗎?例如,我怎樣才能使下面的代碼工作?謝謝!如何在PHP中調整圖像資源大小?

//Create the image 
$image = imagecreatetruecolor(800, 100); 

//Change the image height from 100 to 1000 pixels 
imagecopyresized($image, $image, 0, 0, 0, 0, 800, 1000, 800, 100); 

//Set the header 
header('Content-Type: image/png'); 

//Output the image 
imagepng($image); 

//Destroy the image 
imagedestroy($image); 
+0

是否有一個原因,你不能運行你的遞歸函數,將數據存儲在另一個數據類型,然後計算你的圖像大小應該基於此? – 2012-07-25 14:32:54

+0

它可以完成,但我想我將不得不運行遞歸函數兩次,首先獲取並存儲數據(例如,在數組中),然後計算圖像大小。但也許不是,我正在考慮它,謝謝! – Sophivorus 2012-07-25 14:45:14

回答

1

你的代碼幾乎是完美的,除非你不給它的源圖像的工作。您需要$source = imagecreatefrompng("someimage.png");,然後在您的imagecopyresize中將源圖像參數設置爲$source

+0

我不知道我明白,不imagecreatetruecolor()返回imagecreatefrompng()將返回相同類型的數據? – Sophivorus 2012-07-25 14:41:32

+0

'imagecreatetruecolor'將創建您的「目標」圖像,'imagecreatefrompng'將獲得您的「源」圖像。 – 2012-07-25 16:30:07

+0

ic ..好吧,我以不同的方式解決了我的問題,但我會將您的答案標記爲正確,因爲它似乎是正確的。謝謝! – Sophivorus 2012-07-25 18:10:40