我有一個150x150的單元格,每個單元格中都有一個彩色背景和一個小圖像/符號。此外,每個單元也可以有零個或多個邊界設置。用戶可以更改和添加邊框並更改單元格顏色和單元格。這一切工作得很好,使用jQuery,它只是一個普通的html表格。查找內存使用情況
在用戶體驗結束時,我正在製作此表格的pdf供他們下載。這是一項艱鉅的工作,需要一段時間,現在不是我主要關心的問題。 jQuery將數據收集到一個數組中,並將其發送給php,以使用gd庫將其重新創建爲圖像。
因此,對於每個單元格,我都在大圖像上繪製了正確顏色的矩形,加載符號圖像並將其重新採樣到大圖像上,繪製邊框和imagedestroy符號圖像,但它工作了1分鐘。我改變了我的策略,使一個小的彩色矩形在其上施加圖像並將其緩存在數組中以便再次快速使用。這讓我的時間縮短了30秒,但現在我正在耗盡記憶。
我把表格分成50個單元格塊,這樣每個塊都適合一個頁面,每個塊被製成一個圖像並保存到磁盤,下一個被製作並保存等等。每次gd圖像被銷燬。然後所有的塊都被插入pdf中。
因此,畢竟,我的問題是如何找出內存在哪裏使用,所以我可以嘗試釋放它?我發佈了我認爲導致下面的問題的主要功能。在我的測試中,最多有30個不同的符號/顏色圖像是25pxX25px,這些是緩存在數組中的圖像。
我希望我已經提供了足夠的信息,我的問題已經夠清楚了。
謝謝您的時間, 託德
//make one "block" of stitches returns image file name.
//function makeStitchChartBlock($img, $startX, $startY, $endX, $endY, $caption, $brand){
function makeStitchChartBlock($stitchChartArray, $startX, $startY, $endX, $endY, $caption, $brand,$blockNumber){
global $threadColours;
$stitchCache=array();
$saveTo = 'result'.$blockNumber.'.jpeg';
// calculate size of block
$numRows=($endY-$startY);
$numColumns=($endX-$startX);
$heightOfBlock = $numRows*SYMBOL_SIZE; //in pixels --- used to determine size of image to make for block
$widthOfBlock = $numColumns*SYMBOL_SIZE; //in pixels
//----plus any extra for captions grid lines
$heightOfBlock += (($endY-$startY)+1); //each stitch has a grid line before it and the last one also has on after it
$widthOfBlock += (($endX-$startX)+1);
// create image size of block to put stitches in
$newBlockImage = imagecreatetruecolor($widthOfBlock,$heightOfBlock);
$backStitchColor = imagecolorallocate($newBlockImage, 0, 0, 0);
// insert caption????
//draw grid lines
//$newBlockImage = addGridToImage($newBlockImage);
//keep track of where to start next cell top left
$blockX=0;
$blockY=0;
for($y = $startY; $y < $endY; $y++){ //for each pixel in height, move down 1 "row" each iteration
//echo "<tr>";
for($x = $startX; $x < $endX; $x++){ // "draws" a row (for each y pixel)
//rgb(75, 90, 60)
//x and y are column and row #'s
list($r, $g, $b) = getRGBs($stitchChartArray[$y][$x][0]); //get the rgb values for the cell
$stitchColor = imagecolorallocate($newBlockImage, $r, $g, $b);
//calculate x & y start positons
$stitchStartX=($blockX*SYMBOL_SIZE)+$blockX+1; //account for each previous stitch and the grid line, then add one for new grid line
$stitchStartY=($blockY*SYMBOL_SIZE)+$blockY+1;
$stitchEndX=$stitchStartX+(SYMBOL_SIZE);
$stitchEndY=$stitchStartY+(SYMBOL_SIZE);
/* make a symbol cell image with/without color and save it in the cache */
if(!isset($stitchCache[$r][$g][$b]))
{
//create new image
$stitchCache[$r][$g][$b] = imagecreatetruecolor(SYMBOL_SIZE,SYMBOL_SIZE);
$stitchCacheColor = imagecolorallocate($stitchCache[$r][$g][$b], $r, $g, $b);
//draw colored rectangle
imagefilledrectangle($stitchCache[$r][$g][$b], 0, 0, SYMBOL_SIZE-1, SYMBOL_SIZE-1, $stitchCacheColor);
//add the symbol
$symbolFile=$stitchChartArray[$y][$x][1];
if($symbolFile){
$symbolImage = imagecreatefrompng($symbolFile);
imagecopyresampled ($stitchCache[$r][$g][$b],$symbolImage,0,0,0,0,SYMBOL_SIZE-1,SYMBOL_SIZE-1,imagesx($symbolImage), imagesy($symbolImage));
imagedestroy($symbolImage);
}
}
//add image from cache to the block image
imagecopyresampled ($newBlockImage,$stitchCache[$r][$g][$b],$stitchStartX, $stitchStartY,0,0,SYMBOL_SIZE,SYMBOL_SIZE,SYMBOL_SIZE,SYMBOL_SIZE);
//add the backstitch lines(borders)
if($stitchChartArray[$y][$x][2]>1) //top
{
imagefilledrectangle($newBlockImage, $stitchStartX, $stitchStartY, $stitchEndX, $stitchStartY+1, $backStitchColor);
}
if($stitchChartArray[$y][$x][3]>1) //right
{
imagefilledrectangle($newBlockImage, $stitchEndX-1, $stitchStartY, $stitchEndX, $stitchEndY, $backStitchColor);
}
if($stitchChartArray[$y][$x][4]>1) //bottom
{
imagefilledrectangle($newBlockImage, $stitchStartX, $stitchEndY-1, $stitchEndX, $stitchEndY, $backStitchColor);
}
if($stitchChartArray[$y][$x][5]>1) //left
{
imagefilledrectangle($newBlockImage, $stitchStartX, $stitchStartY, $stitchStartX+1, $stitchEndY, $backStitchColor);
}
//advance x position
$blockX++;
}
//advance y position
//reset x
$blockX=0;
$blockY++;
}
imagejpeg($newBlockImage, $saveTo);
imagedestroy($newBlockImage);
//dump stitch cache
foreach($stitchCache as $r)
{
foreach($r as $g)
{
foreach($g as $b=>$data)
{
imagedestroy($data);
}
}
}
return $saveTo;
}
這將是一個非常好的開始。並感謝全球的建議......正式注意並改變了。 – maddogandnoriko 2012-02-18 14:38:28
感謝您的幫助,我找到了問題。我通過$ post傳遞一個大數組到頁面,內存的頂部是13124128.我可以通過拋棄$ _POST ['stitchChart'] var來釋放一些內存嗎? – maddogandnoriko 2012-02-18 15:08:04
是的,你可以做到這一點。或者您可以增加僅限該腳本的內存限制。你可能也想看看爲什麼$ _POST這麼大。這是你發佈的很多數據。 – liquorvicar 2012-02-18 15:36:54