Code Modernization: Use file_get_contents() in wp_get_webp_info().

`file_get_contents()` is faster than `fread()`, because the PHP core can decide how to best read the remaining file; it could decide to issue just one `read()` call or `mmap()` the file first.

Per the PHP manual, `file_get_contents()` or `stream_get_contents()` is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by the OS to enhance performance.

Reference: [https://www.php.net/manual/en/function.file-get-contents.php PHP Manual: file_get_contents()].

Follow-up to [50810], [52696], [52698], [52701].

Props maxkellermann.
See #55069.

git-svn-id: https://develop.svn.wordpress.org/trunk@52718 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-02-12 13:12:22 +00:00
parent 81ed194ef0
commit 6f4bb2fbeb

View File

@ -5235,15 +5235,12 @@ function wp_get_webp_info( $filename ) {
return compact( 'width', 'height', 'type' );
}
$handle = fopen( $filename, 'rb' );
$magic = file_get_contents( $filename, false, null, 0, 40 );
if ( false === $handle ) {
if ( false === $magic ) {
return compact( 'width', 'height', 'type' );
}
$magic = fread( $handle, 40 );
fclose( $handle );
// Make sure we got enough bytes.
if ( strlen( $magic ) < 40 ) {
return compact( 'width', 'height', 'type' );