Skip to content

imagefontwidth and imagefontheight fail on valid GDFont #13082

Closed
@jonaskohl

Description

@jonaskohl

The following code:

<?php

const FONT_BUILTIN = 5;
const FONT_EXTERNAL = __DIR__ . "/MiscTT_12x20_LE.gdf";

// Uncomment to download sample font:
// copy("http://www.danceswithferrets.org/lab/gdfs/MiscTT_12x20_LE.gdf", FONT_EXTERNAL);
// Note: This is just a sample font. This problem occurs with every valid gdf file.

echo strval(imagefontwidth(FONT_BUILTIN)) . PHP_EOL;
echo strval(imagefontheight(FONT_BUILTIN)) . PHP_EOL;

$font = imageloadfont(FONT_EXTERNAL);
if ($font === false) die("Failed to load font" . PHP_EOL);
if (!($font instanceof GdFont)) die("Font is not of type " . GdFont::class . PHP_EOL);

var_dump($font);

$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);

echo strval($fontWidth) . PHP_EOL;
echo strval($fontHeight) . PHP_EOL;

Resulted in this output:

9
15
object(GdFont)#1 (0) {
}
PHP Fatal error:  Uncaught TypeError: imagefontwidth(): Argument #1 ($font) must be of type GdFont|int, GdFont given in /Users/jonas/Desktop/sample.php:19
Stack trace:
#0 /Users/jonas/Desktop/sample.php(19): imagefontwidth(Object(GdFont))
#1 {main}
  thrown in /Users/jonas/Desktop/sample.php on line 19

Fatal error: Uncaught TypeError: imagefontwidth(): Argument #1 ($font) must be of type GdFont|int, GdFont given in /Users/jonas/Desktop/sample.php:19
Stack trace:
#0 /Users/jonas/Desktop/sample.php(19): imagefontwidth(Object(GdFont))
#1 {main}
  thrown in /Users/jonas/Desktop/sample.php on line 19

But I expected this output instead:

9
15
object(GdFont)#1 (0) {
}
12
20

The weird thing is that calls to imagestring work as expected. I'm also unable to reproduce this on non-mac systems. But the people over at MacPorts told me to report this here, so...

PHP Version

8.3.1

Operating System

macOS 14.2.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions