forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid memory exhaustion when cloning worksheet with a drawing
When cloning `BaseDrawing`, its worksheet will be set as null and thus be orphaned. But when cloning the worksheet, it will re-assign itself as the new worksheet for the BaseDrawing. That way we avoid recursive cloning of a Worksheet that would clone a BaseDrawing that would clone a Worksheet etc. Fixes PHPOffice#437 Fixes PHPOffice#613
- Loading branch information
Showing
5 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DrawingTest extends TestCase | ||
{ | ||
public function testCloningWorksheetWithImages() | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$aSheet = $spreadsheet->getActiveSheet(); | ||
|
||
$gdImage = @imagecreatetruecolor(120, 20); | ||
$textColor = imagecolorallocate($gdImage, 255, 255, 255); | ||
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor); | ||
|
||
$drawing = new MemoryDrawing(); | ||
$drawing->setName('In-Memory image 1'); | ||
$drawing->setDescription('In-Memory image 1'); | ||
$drawing->setCoordinates('A1'); | ||
$drawing->setImageResource($gdImage); | ||
$drawing->setRenderingFunction( | ||
MemoryDrawing::RENDERING_JPEG | ||
); | ||
$drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT); | ||
$drawing->setHeight(36); | ||
$drawing->setWorksheet($aSheet); | ||
|
||
$originDrawingCount = count($aSheet->getDrawingCollection()); | ||
$clonedWorksheet = clone $aSheet; | ||
$clonedDrawingCount = count($clonedWorksheet->getDrawingCollection()); | ||
|
||
self::assertEquals($originDrawingCount, $clonedDrawingCount); | ||
self::assertNotSame($aSheet, $clonedWorksheet); | ||
self::assertNotSame($aSheet->getDrawingCollection(), $clonedWorksheet->getDrawingCollection()); | ||
} | ||
} |