Skip to content

Commit 794ec85

Browse files
committed
Improve PHP 8.5 compatibility
- only use imagedestroy with PHP < 8 - only use xml_parser_free with PHP < 8 - only use curl_close with PHP < 8 - only use setAccessible reflection method with PHP < 8.1 - use http_get_last_response_headers with PHP >= 8.4 - utilize getimagesize support for SVG images - use canonical types for casts - improve array key null safety addresses dompdf#3644
1 parent d38589a commit 794ec85

File tree

10 files changed

+76
-28
lines changed

10 files changed

+76
-28
lines changed

lib/Cpdf.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6050,7 +6050,9 @@ protected function addImagePngAlpha($file, $x, $y, $w, $h, $byte)
60506050
// Cast to 8bit+palette
60516051
$imgalpha_ = @imagecreatefrompng($tempfile_alpha);
60526052
imagecopy($imgalpha, $imgalpha_, 0, 0, 0, 0, $wpx, $hpx);
6053-
imagedestroy($imgalpha_);
6053+
if (PHP_MAJOR_VERSION < 8) {
6054+
imagedestroy($imgalpha_);
6055+
}
60546056
imagepng($imgalpha, $tempfile_alpha);
60556057

60566058
// Make opaque image
@@ -6105,7 +6107,9 @@ protected function addImagePngAlpha($file, $x, $y, $w, $h, $byte)
61056107
// Cast to 8bit+palette
61066108
$imgalpha_ = @imagecreatefrompng($tempfile_alpha);
61076109
imagecopy($imgalpha, $imgalpha_, 0, 0, 0, 0, $wpx, $hpx);
6108-
imagedestroy($imgalpha_);
6110+
if (PHP_MAJOR_VERSION < 8) {
6111+
imagedestroy($imgalpha_);
6112+
}
61096113
imagepng($imgalpha, $tempfile_alpha);
61106114
} else {
61116115
$tempfile_alpha = null;
@@ -6159,7 +6163,9 @@ protected function addImagePngAlpha($file, $x, $y, $w, $h, $byte)
61596163
// extract image without alpha channel
61606164
$imgplain = imagecreatetruecolor($wpx, $hpx);
61616165
imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
6162-
imagedestroy($img);
6166+
if (PHP_MAJOR_VERSION < 8) {
6167+
imagedestroy($img);
6168+
}
61636169

61646170
imagepng($imgalpha, $tempfile_alpha);
61656171
imagepng($imgplain, $tempfile_plain);
@@ -6170,13 +6176,17 @@ protected function addImagePngAlpha($file, $x, $y, $w, $h, $byte)
61706176
// embed mask image
61716177
if ($tempfile_alpha) {
61726178
$this->addImagePng($imgalpha, $tempfile_alpha, $x, $y, $w, $h, true);
6173-
imagedestroy($imgalpha);
6179+
if (PHP_MAJOR_VERSION < 8) {
6180+
imagedestroy($imgalpha);
6181+
}
61746182
$this->imageCache[] = $tempfile_alpha;
61756183
}
61766184

61776185
// embed image, masked with previously embedded mask
61786186
$this->addImagePng($imgplain, $tempfile_plain, $x, $y, $w, $h, false, ($tempfile_alpha !== null));
6179-
imagedestroy($imgplain);
6187+
if (PHP_MAJOR_VERSION < 8) {
6188+
imagedestroy($imgplain);
6189+
}
61806190
$this->imageCache[] = $tempfile_plain;
61816191
}
61826192

@@ -6261,11 +6271,13 @@ function addPngFromFile($file, $x, $y, $w = 0, $h = 0)
62616271
}
62626272

62636273
imagecopy($img, $imgtmp, 0, 0, 0, 0, $sx, $sy);
6264-
imagedestroy($imgtmp);
6274+
if (PHP_MAJOR_VERSION < 8) {
6275+
imagedestroy($imgtmp);
6276+
}
62656277
}
62666278
$this->addImagePng($img, $file, $x, $y, $w, $h);
62676279

6268-
if ($img) {
6280+
if ($img && PHP_MAJOR_VERSION < 8) {
62696281
imagedestroy($img);
62706282
}
62716283
}

src/Adapter/CPDF.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,9 @@ protected function _convert_to_png($image_url, $type)
620620
$filename = "$tmp_name.png";
621621

622622
imagepng($im, $filename);
623-
imagedestroy($im);
623+
if (PHP_MAJOR_VERSION < 8) {
624+
imagedestroy($im);
625+
}
624626
} else {
625627
$filename = null;
626628
}

src/Adapter/GD.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ protected function _output($options = [])
10541054
break;
10551055
}
10561056

1057-
if ($this->_aa_factor != 1) {
1057+
if ($this->_aa_factor != 1 && PHP_MAJOR_VERSION < 8) {
10581058
imagedestroy($dst);
10591059
}
10601060
}

src/Adapter/PDFLib.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,9 @@ protected function _convert_to_png($image_url, $type)
11001100
$filename = "$tmp_name.png";
11011101

11021102
imagepng($im, $filename);
1103-
imagedestroy($im);
1103+
if (PHP_MAJOR_VERSION < 8) {
1104+
imagedestroy($im);
1105+
}
11041106
} else {
11051107
$filename = null;
11061108
}

src/Css/AttributeTranslator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ protected static function _set_hr_align(\DOMElement $node, $value)
503503
$width = "100%";
504504
}
505505

506-
$remainder = 100 - (double)rtrim($width, "% ");
506+
$remainder = 100 - (float)rtrim($width, "% ");
507507

508508
switch ($value) {
509509
case "left":

src/FontMetrics.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ public function getFont($familyRaw, $subtypeRaw = "normal")
461461
{
462462
static $cache = [];
463463

464+
if (!$familyRaw) {
465+
$familyRaw = $this->options->getDefaultFont();
466+
}
467+
if (!$subtypeRaw) {
468+
$subtypeRaw = "normal";
469+
}
470+
464471
if (isset($cache[$familyRaw][$subtypeRaw])) {
465472
return $cache[$familyRaw][$subtypeRaw];
466473
}

src/Helpers.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,22 +803,28 @@ public static function dompdf_getimagesize($filename, $context = null)
803803
{
804804
static $cache = [];
805805

806-
if (isset($cache[$filename])) {
807-
return $cache[$filename];
808-
}
809-
810-
[$width, $height, $type] = @getimagesize($filename);
811-
812806
// Custom types
813807
$types = [
814808
IMAGETYPE_JPEG => "jpeg",
815809
IMAGETYPE_GIF => "gif",
816810
IMAGETYPE_BMP => "bmp",
817811
IMAGETYPE_PNG => "png",
818-
IMAGETYPE_WEBP => "webp",
812+
IMAGETYPE_WEBP => "webp"
819813
];
814+
if (defined('IMAGETYPE_SVG')) {
815+
$types[IMAGETYPE_SVG] = "svg";
816+
}
820817

821-
$type = $types[$type] ?? null;
818+
if (isset($cache[$filename])) {
819+
return $cache[$filename];
820+
}
821+
822+
$parse_result = @getimagesize($filename);
823+
$width = $height = $type = null;
824+
if ($parse_result !== false) {
825+
[$width, $height, $type] = $parse_result;
826+
$type = $types[$type] ?? null;
827+
}
822828

823829
if ($width == null || $height == null) {
824830
[$data] = Helpers::getFileContent($filename, $context);
@@ -1050,6 +1056,7 @@ public static function getFileContent($uri, $context = null, $offset = 0, $maxle
10501056

10511057
try {
10521058
if ($is_local_path || ini_get('allow_url_fopen') && !$can_use_curl) {
1059+
$http_response_header = null;
10531060
if ($is_local_path === false) {
10541061
$uri = Helpers::encodeURI($uri);
10551062
}
@@ -1061,7 +1068,9 @@ public static function getFileContent($uri, $context = null, $offset = 0, $maxle
10611068
if ($result !== false) {
10621069
$content = $result;
10631070
}
1064-
if (isset($http_response_header)) {
1071+
if (version_compare(PHP_VERSION, "8.4.0", ">=")) {
1072+
$headers = \http_get_last_response_headers();
1073+
} elseif (isset($http_response_header)) {
10651074
$headers = $http_response_header;
10661075
}
10671076

@@ -1131,7 +1140,10 @@ public static function getFileContent($uri, $context = null, $offset = 0, $maxle
11311140
break;
11321141
}
11331142
}
1134-
curl_close($curl);
1143+
1144+
if (PHP_MAJOR_VERSION < 8) {
1145+
curl_close($curl);
1146+
}
11351147
}
11361148
} finally {
11371149
restore_error_handler();

src/Image/Cache.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Cache
6262
*/
6363
static function resolve_url($url, $protocol, $host, $base_path, Options $options)
6464
{
65+
$full_url = null;
6566
$tempfile = null;
6667
$resolved_url = null;
6768
$type = null;
@@ -176,7 +177,9 @@ function ($parser, $name, $attributes) use ($options, $parsed_url, $full_url) {
176177
fclose($fp);
177178
xml_parse($parser, "", true);
178179
}
179-
xml_parser_free($parser);
180+
if (PHP_MAJOR_VERSION < 8) {
181+
xml_parser_free($parser);
182+
}
180183
}
181184
} catch (ImageException $e) {
182185
if ($tempfile) {
@@ -186,7 +189,9 @@ function ($parser, $name, $attributes) use ($options, $parsed_url, $full_url) {
186189
list($width, $height, $type) = Helpers::dompdf_getimagesize($resolved_url, $options->getHttpContext());
187190
$message = self::$error_message;
188191
Helpers::record_warnings($e->getCode(), $e->getMessage() . " \n $url", $e->getFile(), $e->getLine());
189-
self::$_cache[$full_url] = $resolved_url;
192+
if ($full_url !== null) {
193+
self::$_cache[$full_url] = $resolved_url;
194+
}
190195
}
191196

192197
return [$resolved_url, $type, $message];

src/Renderer/AbstractRenderer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ protected function _background_image(string $url, float $x, float $y, float $wid
518518
imagealphablending($newSrc, false);
519519
imagesavealpha($newSrc, true);
520520
imagecopyresampled($newSrc, $src, 0, 0, 0, 0, $img_w, $img_h, imagesx($src), imagesy($src));
521-
imagedestroy($src);
521+
if (PHP_MAJOR_VERSION < 8) {
522+
imagedestroy($src);
523+
}
522524
$src = $newSrc;
523525
}
524526

@@ -625,7 +627,9 @@ protected function _background_image(string $url, float $x, float $y, float $wid
625627
print 'Unknown repeat!';
626628
}
627629

628-
imagedestroy($src);
630+
if (PHP_MAJOR_VERSION < 8) {
631+
imagedestroy($src);
632+
}
629633

630634
if ($cpdfFromGd && $this->_canvas instanceof CPDF) {
631635
// Skip writing temp file as the GD object is added directly
@@ -636,7 +640,9 @@ protected function _background_image(string $url, float $x, float $y, float $wid
636640
$tmpFile = "$tmpName.png";
637641

638642
imagepng($bg, $tmpFile);
639-
imagedestroy($bg);
643+
if (PHP_MAJOR_VERSION < 8) {
644+
imagedestroy($bg);
645+
}
640646

641647
Cache::addTempImage($img, $tmpFile, $key);
642648
}
@@ -657,7 +663,7 @@ protected function _background_image(string $url, float $x, float $y, float $wid
657663
// Note: CPDF_Adapter image converts y position
658664
$this->_canvas->get_cpdf()->addImagePng($bg, $cpdfKey, $x, $this->_canvas->get_height() - $y - $height, $width, $height);
659665

660-
if (isset($bg)) {
666+
if (isset($bg) && PHP_MAJOR_VERSION < 8) {
661667
imagedestroy($bg);
662668
}
663669
} else {

tests/Renderer/RendererTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public static function resizeBackgroundImageProvider(): array
9292
protected static function getMethod($name) {
9393
$class = new \ReflectionClass(Renderer::class);
9494
$method = $class->getMethod($name);
95-
$method->setAccessible(true);
95+
if (version_compare(PHP_VERSION, "8.1.0", "<")) {
96+
$method->setAccessible(true);
97+
}
9698
return $method;
9799
}
98100
}

0 commit comments

Comments
 (0)