Skip to content

Memory leak #2101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5920,16 +5920,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Worksheet/Iterator.php

-
message: "#^Parameter \\#1 \\$im of function imagesx expects resource, GdImage\\|resource given\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/MemoryDrawing.php

-
message: "#^Parameter \\#1 \\$im of function imagesy expects resource, GdImage\\|resource given\\.$#"
count: 1
path: src/PhpSpreadsheet/Worksheet/MemoryDrawing.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\PageSetup\\:\\:\\$pageOrder has no typehint specified\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ parameters:
- '~^Return typehint of method .* has invalid type GdImage\.$~'
- '~^Property .* has unknown class GdImage as its type\.$~'
- '~^Parameter .* of method .* has invalid typehint type GdImage\.$~'
- '~^Parameter \#1 \$im of function (imagedestroy|imageistruecolor|imagealphablending|imagesavealpha|imagecolortransparent|imagecolorsforindex|imagesavealpha|imagesx|imagesy) expects resource, GdImage\|resource given\.$~'
- '~^Parameter \#2 \$src_im of function imagecopy expects resource, GdImage\|resource given\.$~'
# Accept a bit anything for assert methods
- '~^Parameter \#2 .* of static method PHPUnit\\Framework\\Assert\:\:assert\w+\(\) expects .*, .* given\.$~'
- '~^Method PhpOffice\\PhpSpreadsheetTests\\.*\:\:test.*\(\) has parameter \$args with no typehint specified\.$~'
Expand Down
10 changes: 9 additions & 1 deletion src/PhpSpreadsheet/Reader/Security/XmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ class XmlScanner

private static $libxmlDisableEntityLoaderValue;

/**
* @var bool
*/
private static $shutdownRegistered = false;

public function __construct($pattern = '<!DOCTYPE')
{
$this->pattern = $pattern;

$this->disableEntityLoaderCheck();

// A fatal error will bypass the destructor, so we register a shutdown here
register_shutdown_function([__CLASS__, 'shutdown']);
if (!self::$shutdownRegistered) {
self::$shutdownRegistered = true;
register_shutdown_function([__CLASS__, 'shutdown']);
}
}

public static function getInstance(Reader\IReader $reader)
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ public function __destruct()
{
$this->disconnectWorksheets();
$this->calculationEngine = null;
$this->cellXfCollection = [];
$this->cellStyleXfCollection = [];
}

/**
Expand Down
66 changes: 64 additions & 2 deletions src/PhpSpreadsheet/Worksheet/MemoryDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Worksheet;

use GdImage;
use PhpOffice\PhpSpreadsheet\Exception;

class MemoryDrawing extends BaseDrawing
{
Expand All @@ -21,7 +22,7 @@ class MemoryDrawing extends BaseDrawing
/**
* Image resource.
*
* @var GdImage|resource
* @var null|GdImage|resource
*/
private $imageResource;

Expand Down Expand Up @@ -60,10 +61,71 @@ public function __construct()
parent::__construct();
}

public function __destruct()
{
if ($this->imageResource) {
imagedestroy($this->imageResource);
$this->imageResource = null;
}
}

public function __clone()
{
parent::__clone();
$this->cloneResource();
}

private function cloneResource(): void
{
if (!$this->imageResource) {
return;
}

$width = imagesx($this->imageResource);
$height = imagesy($this->imageResource);

if (imageistruecolor($this->imageResource)) {
$clone = imagecreatetruecolor($width, $height);
if (!$clone) {
throw new Exception('Could not clone image resource');
}

imagealphablending($clone, false);
imagesavealpha($clone, true);
} else {
$clone = imagecreate($width, $height);
if (!$clone) {
throw new Exception('Could not clone image resource');
}

// If the image has transparency...
$transparent = imagecolortransparent($this->imageResource);
if ($transparent >= 0) {
$rgb = imagecolorsforindex($this->imageResource, $transparent);
if ($rgb === false) {
throw new Exception('Could not get image colors');
}

imagesavealpha($clone, true);
$color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
if ($color === false) {
throw new Exception('Could not get image alpha color');
}

imagefill($clone, 0, 0, $color);
}
}

//Create the Clone!!
imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height);

$this->imageResource = $clone;
}

/**
* Get image resource.
*
* @return GdImage|resource
* @return null|GdImage|resource
*/
public function getImageResource()
{
Expand Down
27 changes: 15 additions & 12 deletions src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,21 @@ private function writeImageInCell(Worksheet $pSheet, $coordinates)
$drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' .
$imageData . '" alt="' . $filedesc . '" />';
} elseif ($drawing instanceof MemoryDrawing) {
ob_start(); // Let's start output buffering.
imagepng($drawing->getImageResource()); // This will normally output the image, but because of ob_start(), it won't.
$contents = ob_get_contents(); // Instead, output above is saved to $contents
ob_end_clean(); // End the output buffer.

$dataUri = 'data:image/jpeg;base64,' . base64_encode($contents);

// Because of the nature of tables, width is more important than height.
// max-width: 100% ensures that image doesnt overflow containing cell
// width: X sets width of supplied image.
// As a result, images bigger than cell will be contained and images smaller will not get stretched
$html .= '<img alt="' . $filedesc . '" src="' . $dataUri . '" style="max-width:100%;width:' . $drawing->getWidth() . 'px;" />';
$imageResource = $drawing->getImageResource();
if ($imageResource) {
ob_start(); // Let's start output buffering.
imagepng($imageResource); // This will normally output the image, but because of ob_start(), it won't.
$contents = ob_get_contents(); // Instead, output above is saved to $contents
ob_end_clean(); // End the output buffer.

$dataUri = 'data:image/jpeg;base64,' . base64_encode($contents);

// Because of the nature of tables, width is more important than height.
// max-width: 100% ensures that image doesnt overflow containing cell
// width: X sets width of supplied image.
// As a result, images bigger than cell will be contained and images smaller will not get stretched
$html .= '<img alt="' . $filedesc . '" src="' . $dataUri . '" style="max-width:100%;width:' . $drawing->getWidth() . 'px;" />';
}
}
}

Expand Down