PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / functions / formatting.php

formatting.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/functions/formatting.php

101 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
4 /**
5 * Round UP to nearest half integer.
6 *
7 * @since 1.0
8 * @source http://stackoverflow.com/a/13526408
9 *
10 * @param int|float|string $number The number to round up.
11 * @return float The formatted number.
12 */
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 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 * @author Grégory Viguier
42 *
43 * @param int|string $bytes Number of bytes. Note max integer size for integers.
44 * @param int $decimals Optional. Precision of number of decimal places. Default 0.
45 * @return string|false False on failure. Number string on success.
46 */
47 function imagify_size_format( $bytes, $decimals = 0 ) {
48 $bytes = @size_format( $bytes, $decimals );
49 return str_replace( ' ', ' ', $bytes );
50 }
51
52 /**
53 * Get the Imagify attachment class name depending to a context.
54 *
55 * @since 1.5
56 * @since 1.6.6 $attachment_id and $identifier have been added.
57 * @author Jonathan Buttigieg
58 *
59 * @param string $context The context to determine the class name.
60 * @param int $attachment_id The attachment ID.
61 * @param string $identifier An identifier.
62 * @return string The Imagify attachment class name.
63 */
64 function get_imagify_attachment_class_name( $context, $attachment_id, $identifier ) {
65 $context = $context ? $context : 'wp';
66
67 if ( 'wp' !== $context && 'wp' === strtolower( $context ) ) {
68 $context = 'wp';
69 }
70
71 /**
72 * Filter the context used for the optimization.
73 *
74 * @since 1.6.6
75 * @author Grégory Viguier
76 *
77 * @param string $context The context.
78 * @param int $attachment_id The attachment ID.
79 * @param string $identifier An identifier.
80 */
81 $context = apply_filters( 'imagify_optimize_attachment_context', $context, $attachment_id, $identifier );
82
83 return 'Imagify_' . ( 'wp' !== $context ? $context . '_' : '' ) . 'Attachment';
84 }
85
86 /**
87 * Get the Imagify attachment instance depending to a context.
88 *
89 * @since 1.6.13
90 * @author Grégory Viguier
91 *
92 * @param string $context The context to determine the class name.
93 * @param int $attachment_id The attachment ID.
94 * @param string $identifier An identifier.
95 * @return object The Imagify attachment instance.
96 */
97 function get_imagify_attachment( $context, $attachment_id, $identifier ) {
98 $class_name = get_imagify_attachment_class_name( $context, $attachment_id, $identifier );
99 return new $class_name( $attachment_id );
100 }
101