Skip to content

Commit 4ef6659

Browse files
committed
getimagesize support HEIF/HEIC
Signed-off-by: Benstone Zhang <benstonezhang@gmail.com>
1 parent 39cf276 commit 4ef6659

9 files changed

+54
-3
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@
646646
* @cvalue IMAGE_FILETYPE_AVIF
647647
*/
648648
const IMAGETYPE_AVIF = UNKNOWN;
649+
/**
650+
* @var int
651+
* @cvalue IMAGE_FILETYPE_HEIF
652+
*/
653+
const IMAGETYPE_HEIF = UNKNOWN;
649654
/**
650655
* @var int
651656
* @cvalue IMAGE_FILETYPE_UNKNOWN

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/image.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
5151
PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
5252
PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
5353
PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
54+
PHPAPI const char php_sig_ftyp[4] = {'f', 't', 'y', 'p'};
55+
PHPAPI const char php_sig_mif1[4] = {'m', 'i', 'f', '1'};
56+
PHPAPI const char php_sig_heic[4] = {'h', 'e', 'i', 'c'};
57+
PHPAPI const char php_sig_heix[4] = {'h', 'e', 'i', 'x'};
5458

5559
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
5660
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
@@ -1254,6 +1258,8 @@ PHPAPI char * php_image_type_to_mime_type(int image_type)
12541258
return "image/webp";
12551259
case IMAGE_FILETYPE_AVIF:
12561260
return "image/avif";
1261+
case IMAGE_FILETYPE_HEIF:
1262+
return "image/heif";
12571263
default:
12581264
case IMAGE_FILETYPE_UNKNOWN:
12591265
return "application/octet-stream"; /* suppose binary format */
@@ -1339,6 +1345,10 @@ PHP_FUNCTION(image_type_to_extension)
13391345
case IMAGE_FILETYPE_AVIF:
13401346
imgext = ".avif";
13411347
break;
1348+
case IMAGE_FILETYPE_HEIF:
1349+
imgext = ".heif";
1350+
break;
1351+
break;
13421352
}
13431353

13441354
if (imgext) {
@@ -1423,6 +1433,11 @@ PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetyp
14231433
return IMAGE_FILETYPE_JP2;
14241434
}
14251435

1436+
if (twelve_bytes_read && !memcmp(filetype + 4, php_sig_ftyp, 4) &&
1437+
(!memcmp(filetype + 8, php_sig_mif1, 4) || !memcmp(filetype + 8, php_sig_heic, 4) || !memcmp(filetype + 8, php_sig_heix, 4))) {
1438+
return IMAGE_FILETYPE_HEIF;
1439+
}
1440+
14261441
if (!php_stream_rewind(stream) && php_is_image_avif(stream)) {
14271442
return IMAGE_FILETYPE_AVIF;
14281443
}
@@ -1515,6 +1530,11 @@ static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *
15151530
case IMAGE_FILETYPE_AVIF:
15161531
result = php_handle_avif(stream);
15171532
break;
1533+
case IMAGE_FILETYPE_HEIF:
1534+
if (!php_stream_rewind(stream)) {
1535+
result = php_handle_avif(stream);
1536+
}
1537+
break;
15181538
default:
15191539
case IMAGE_FILETYPE_UNKNOWN:
15201540
break;

ext/standard/php_image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef enum
4444
IMAGE_FILETYPE_ICO,
4545
IMAGE_FILETYPE_WEBP,
4646
IMAGE_FILETYPE_AVIF,
47+
IMAGE_FILETYPE_HEIF,
4748
/* WHEN EXTENDING: PLEASE ALSO REGISTER IN basic_function.stub.php */
4849
IMAGE_FILETYPE_COUNT
4950
} image_filetype;

ext/standard/tests/image/getimagesize.phpt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GetImageSize()
2323
var_dump($result);
2424
?>
2525
--EXPECT--
26-
array(17) {
26+
array(18) {
2727
["test-1pix.bmp"]=>
2828
array(6) {
2929
[0]=>
@@ -216,6 +216,23 @@ array(17) {
216216
["mime"]=>
217217
string(9) "image/gif"
218218
}
219+
["test4pix.heic"]=>
220+
array(7) {
221+
[0]=>
222+
int(54)
223+
[1]=>
224+
int(84)
225+
[2]=>
226+
int(20)
227+
[3]=>
228+
string(22) "width="54" height="84""
229+
["bits"]=>
230+
int(8)
231+
["channels"]=>
232+
int(3)
233+
["mime"]=>
234+
string(10) "image/heif"
235+
}
219236
["test4pix.iff"]=>
220237
array(6) {
221238
[0]=>

ext/standard/tests/image/image_type_to_mime_type.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ image_type_to_mime_type()
2424
var_dump($result);
2525
?>
2626
--EXPECT--
27-
array(17) {
27+
array(18) {
2828
["test-1pix.bmp"]=>
2929
string(9) "image/bmp"
3030
["test12pix.webp"]=>
@@ -49,6 +49,8 @@ array(17) {
4949
string(10) "image/webp"
5050
["test4pix.gif"]=>
5151
string(9) "image/gif"
52+
["test4pix.heic"]=>
53+
string(10) "image/heif"
5254
["test4pix.iff"]=>
5355
string(9) "image/iff"
5456
["test4pix.png"]=>

ext/standard/tests/image/image_type_to_mime_type_basic.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ $image_types = array (
2222
IMAGETYPE_WBMP,
2323
IMAGETYPE_JPEG2000,
2424
IMAGETYPE_XBM,
25-
IMAGETYPE_WEBP
25+
IMAGETYPE_WEBP,
26+
IMAGETYPE_HEIF
2627
);
2728

2829
foreach($image_types as $image_type) {
@@ -51,5 +52,6 @@ string(18) "image/vnd.wap.wbmp"
5152
string(24) "application/octet-stream"
5253
string(9) "image/xbm"
5354
string(10) "image/webp"
55+
string(10) "image/heif"
5456

5557
Done image_type_to_mime_type() test

ext/standard/tests/image/image_type_to_mime_type_variation3.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@ string\(10\) "image\/webp"
7575
string\(10\) "image\/avif"
7676

7777
-- Iteration 20 --
78+
string\(10\) "image\/heif"
79+
80+
-- Iteration 21 --
7881
string\(24\) "application\/octet-stream"
6.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)