PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.0
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / classes / file-utils.php

file-utils.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.0.0, at classes/file-utils.php

66 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimizer\Classes;
4
5 class File_Utils {
6 public static function get_extension( string $path ): string {
7 return pathinfo( $path, PATHINFO_EXTENSION );
8 }
9
10 public static function get_basename( string $path ): string {
11 return pathinfo( $path, PATHINFO_BASENAME );
12 }
13
14 public static function replace_extension( string $path, string $new_extension, bool $unique_filename = false ): string {
15 $path = pathinfo( $path );
16 $basename = sprintf( '%s.%s', $path['filename'], $new_extension );
17
18 if ( $unique_filename ) {
19 $basename = wp_unique_filename( $path['dirname'], $basename );
20 }
21
22 return sprintf( '%s/%s', $path['dirname'], $basename );
23 }
24
25 public static function get_unique_path( string $path ): string {
26 $path = pathinfo( $path );
27 $basename = sprintf( '%s.%s', $path['filename'], $path['extension'] );
28
29 return sprintf( '%s/%s', $path['dirname'], wp_unique_filename( $path['dirname'], $basename ) );
30 }
31
32 public static function get_relative_upload_path( string $path ): string {
33 return _wp_relative_upload_path( $path );
34 }
35
36 public static function get_url_from_path( string $full_path ): string {
37 $upload_info = wp_upload_dir();
38 $url_base = $upload_info['baseurl'];
39
40 $parts = preg_split(
41 '/\/wp-content\/uploads/',
42 $full_path
43 );
44
45 return $url_base . $parts[1];
46 }
47
48 public static function format_file_size( int $file_size_in_bytes, $decimals = 2 ): string {
49 $sizes = [
50 __( '%s Bytes', 'image-optimizer' ),
51 __( '%s Kb', 'image-optimizer' ),
52 __( '%s Mb', 'image-optimizer' ),
53 __( '%s Gb', 'image-optimizer' ),
54 ];
55
56 if ( ! $file_size_in_bytes ) {
57 return sprintf( $sizes[0], 0 );
58 }
59
60 $current_scale = floor( log( $file_size_in_bytes ) / log( 1024 ) );
61 $formatted_value = number_format( $file_size_in_bytes / pow( 1024, $current_scale ), $decimals );
62
63 return sprintf( $sizes[ $current_scale ], $formatted_value );
64 }
65 }
66