| @@ -1,31 +1,57 @@ | ||
| 1 | -<?php | |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); | |
| 1 | +<?php | |
| 2 | +defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | - * Round UP to nearest half integer | |
| 5 | + * Round UP to nearest half integer. | |
| 6 | 6 | * |
| 7 | - * @since 1.0 | |
| 7 | + * @since 1.0 | |
| 8 | 8 | * @source http://stackoverflow.com/a/13526408 |
| 9 | 9 | * |
| 10 | - * @param int $number The number to round up | |
| 11 | - * @return int $number The formatted number | |
| 10 | + * @param int|float|string $number The number to round up. | |
| 11 | + * @return float The formatted number. | |
| 12 | 12 | */ |
| 13 | 13 | function imagify_round_half_five( $number ) { |
| 14 | - $number = strval( $number ); | |
| 15 | - $number = explode( '.', $number ); | |
| 16 | - | |
| 17 | - if ( ! isset( $number[1] ) ) { | |
| 18 | - return $number[0]; | |
| 19 | - } | |
| 20 | - | |
| 21 | - $decimal = floatval( '0.' . substr( $number[1], 0 , 2 ) ); // cut only 2 number | |
| 22 | - if ( $decimal > 0 ) { | |
| 23 | - if( $decimal <= 0.5 ) { | |
| 24 | - return floatval( $number[0] ) + 0.5; | |
| 25 | - } elseif ( $decimal > 0.5 && $decimal <= 0.99 ) { | |
| 26 | - return floatval( $number[0]) + 1; | |
| 27 | - } | |
| 28 | - } else { | |
| 29 | - return floatval( $number ); | |
| 30 | - } | |
| 31 | -} | |
| 14 | + $number = strval( $number ); | |
| 15 | + $number = explode( '.', $number ); | |
| 16 | + | |
| 17 | + if ( ! isset( $number[1] ) ) { | |
| 18 | + return $number[0]; | |
| 19 | + } | |
| 20 | + | |
| 21 | + $decimal = floatval( '0.' . substr( $number[1], 0, 2 ) ); // Cut only 2 numbers. | |
| 22 | + | |
| 23 | + if ( $decimal > 0 ) { | |
| 24 | + if ( $decimal <= 0.5 ) { | |
| 25 | + return floatval( $number[0] ) + 0.5; | |
| 26 | + } | |
| 27 | + if ( $decimal <= 0.99 ) { | |
| 28 | + return floatval( $number[0] ) + 1; | |
| 29 | + } | |
| 30 | + return 1; | |
| 31 | + } | |
| 32 | + | |
| 33 | + return floatval( $number ); | |
| 34 | +} | |
| 35 | + | |
| 36 | +/** | |
| 37 | + * Convert number of bytes largest unit bytes will fit into. | |
| 38 | + * This is a clone of size_format(), but with a non-breaking space. | |
| 39 | + * | |
| 40 | + * @since 1.7 | |
| 41 | + * @since 1.8.1 Automatic $decimals. | |
| 42 | + * @author Grégory Viguier | |
| 43 | + * | |
| 44 | + * @param int|string $bytes Number of bytes. Note max integer size for integers. | |
| 45 | + * @param int $decimals Optional. Precision of number of decimal places. | |
| 46 | + * If negative or not an integer, $decimals value is "automatic": 0 if $bytes <= 1GB, or 1 if > 1GB. | |
| 47 | + * @return string|false False on failure. Number string on success. | |
| 48 | + */ | |
| 49 | +function imagify_size_format( $bytes, $decimals = -1 ) { | |
| 50 | + | |
| 51 | + if ( $decimals < 0 || ! is_int( $decimals ) ) { | |
| 52 | + $decimals = $bytes > pow( 1024, 3 ) ? 1 : 0; | |
| 53 | + } | |
| 54 | + | |
| 55 | + $bytes = @size_format( $bytes, $decimals ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 56 | + return str_replace( ' ', ' ', $bytes ); | |
| 57 | +} | |