Skip to content

Add support to get size of image from HEIF/HEIC #18940

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@
* @cvalue IMAGE_FILETYPE_AVIF
*/
const IMAGETYPE_AVIF = UNKNOWN;
/**
* @var int
* @cvalue IMAGE_FILETYPE_HEIF
*/
const IMAGETYPE_HEIF = UNKNOWN;
/**
* @var int
* @cvalue IMAGE_FILETYPE_UNKNOWN
Expand Down
1 change: 1 addition & 0 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions ext/standard/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
PHPAPI const char php_sig_ftyp[4] = {'f', 't', 'y', 'p'};
PHPAPI const char php_sig_mif1[4] = {'m', 'i', 'f', '1'};
PHPAPI const char php_sig_heic[4] = {'h', 'e', 'i', 'c'};
PHPAPI const char php_sig_heix[4] = {'h', 'e', 'i', 'x'};

/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
Expand Down Expand Up @@ -1254,6 +1258,8 @@ PHPAPI char * php_image_type_to_mime_type(int image_type)
return "image/webp";
case IMAGE_FILETYPE_AVIF:
return "image/avif";
case IMAGE_FILETYPE_HEIF:
return "image/heif";
default:
case IMAGE_FILETYPE_UNKNOWN:
return "application/octet-stream"; /* suppose binary format */
Expand Down Expand Up @@ -1339,6 +1345,10 @@ PHP_FUNCTION(image_type_to_extension)
case IMAGE_FILETYPE_AVIF:
imgext = ".avif";
break;
case IMAGE_FILETYPE_HEIF:
imgext = ".heif";
break;
break;
}

if (imgext) {
Expand Down Expand Up @@ -1423,6 +1433,11 @@ PHPAPI int php_getimagetype(php_stream *stream, const char *input, char *filetyp
return IMAGE_FILETYPE_JP2;
}

if (twelve_bytes_read && !memcmp(filetype + 4, php_sig_ftyp, 4) &&
(!memcmp(filetype + 8, php_sig_mif1, 4) || !memcmp(filetype + 8, php_sig_heic, 4) || !memcmp(filetype + 8, php_sig_heix, 4))) {
return IMAGE_FILETYPE_HEIF;
}

if (!php_stream_rewind(stream) && php_is_image_avif(stream)) {
return IMAGE_FILETYPE_AVIF;
}
Expand Down Expand Up @@ -1515,6 +1530,11 @@ static void php_getimagesize_from_stream(php_stream *stream, char *input, zval *
case IMAGE_FILETYPE_AVIF:
result = php_handle_avif(stream);
break;
case IMAGE_FILETYPE_HEIF:
if (!php_stream_rewind(stream)) {
result = php_handle_avif(stream);
}
break;
default:
case IMAGE_FILETYPE_UNKNOWN:
break;
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef enum
IMAGE_FILETYPE_ICO,
IMAGE_FILETYPE_WEBP,
IMAGE_FILETYPE_AVIF,
IMAGE_FILETYPE_HEIF,
/* WHEN EXTENDING: PLEASE ALSO REGISTER IN basic_function.stub.php */
IMAGE_FILETYPE_COUNT
} image_filetype;
Expand Down
19 changes: 18 additions & 1 deletion ext/standard/tests/image/getimagesize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ GetImageSize()
var_dump($result);
?>
--EXPECT--
array(17) {
array(18) {
["test-1pix.bmp"]=>
array(6) {
[0]=>
Expand Down Expand Up @@ -216,6 +216,23 @@ array(17) {
["mime"]=>
string(9) "image/gif"
}
["test4pix.heic"]=>
array(7) {
[0]=>
int(54)
[1]=>
int(84)
[2]=>
int(20)
[3]=>
string(22) "width="54" height="84""
["bits"]=>
int(8)
["channels"]=>
int(3)
["mime"]=>
string(10) "image/heif"
}
["test4pix.iff"]=>
array(6) {
[0]=>
Expand Down
4 changes: 3 additions & 1 deletion ext/standard/tests/image/image_type_to_mime_type.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ image_type_to_mime_type()
var_dump($result);
?>
--EXPECT--
array(17) {
array(18) {
["test-1pix.bmp"]=>
string(9) "image/bmp"
["test12pix.webp"]=>
Expand All @@ -49,6 +49,8 @@ array(17) {
string(10) "image/webp"
["test4pix.gif"]=>
string(9) "image/gif"
["test4pix.heic"]=>
string(10) "image/heif"
["test4pix.iff"]=>
string(9) "image/iff"
["test4pix.png"]=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ $image_types = array (
IMAGETYPE_WBMP,
IMAGETYPE_JPEG2000,
IMAGETYPE_XBM,
IMAGETYPE_WEBP
IMAGETYPE_WEBP,
IMAGETYPE_HEIF
);

foreach($image_types as $image_type) {
Expand Down Expand Up @@ -51,5 +52,6 @@ string(18) "image/vnd.wap.wbmp"
string(24) "application/octet-stream"
string(9) "image/xbm"
string(10) "image/webp"
string(10) "image/heif"

Done image_type_to_mime_type() test
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ string\(10\) "image\/webp"
string\(10\) "image\/avif"

-- Iteration 20 --
string\(10\) "image\/heif"

-- Iteration 21 --
string\(24\) "application\/octet-stream"
Binary file added ext/standard/tests/image/test4pix.heic
Binary file not shown.
Loading