PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.8.0
TinyPNG – JPEG, PNG & WebP image compression v3.8.0
3.8.0 3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-helpers.php
tiny-compress-images / src Last commit date
compatibility 2 months ago config 2 months ago css 7 months ago data 4 years ago images 4 years ago js 1 week ago vendor 6 months ago views 1 week ago class-tiny-apache-rewrite.php 2 months ago class-tiny-bulk-optimization.php 2 months ago class-tiny-cli.php 2 months ago class-tiny-compress-client.php 1 week ago class-tiny-compress-fopen.php 2 months ago class-tiny-compress.php 1 week ago class-tiny-conversion.php 4 months ago class-tiny-diagnostics.php 7 months ago class-tiny-exception.php 7 months ago class-tiny-helpers.php 2 months ago class-tiny-image-size.php 1 week ago class-tiny-image.php 1 week ago class-tiny-logger.php 2 months ago class-tiny-migrate.php 2 months ago class-tiny-notices.php 2 months ago class-tiny-php.php 2 months ago class-tiny-picture.php 2 months ago class-tiny-plugin.php 1 week ago class-tiny-settings.php 1 week ago class-tiny-source-base.php 1 week ago class-tiny-source-image.php 7 months ago class-tiny-source-picture.php 7 months ago class-tiny-wp-base.php 2 months ago
class-tiny-helpers.php
169 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2018 Tinify B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 class Tiny_Helpers {
22
23
24 /**
25 * truncate_text will truncate a string to a given length.
26 * When text is longer than the given length, the string will be truncated and
27 * the last characters will be replaced with an ellipsis.
28 *
29 * We can use mb_strlen & mb_substr as WordPress provides a compat function for
30 * it if mbstring php module is not installed.
31 *
32 * @param string $text the text
33 * @param integer $length the maximum length of the string
34 * @return string the truncated string
35 */
36 public static function truncate_text( $text, $length ) {
37 if ( mb_strlen( $text ) > $length ) {
38 return mb_substr( $text, 0, $length - 3 ) . '...';
39 }
40 return $text;
41 }
42
43 /**
44 * Will replace the file extension with the specified mimetype.
45 *
46 * @param string $mimetype The format to replace the extension with.
47 * Currently supports 'image/avif' or 'image/webp'
48 * @param string $filepath The full path to replace the extension in, ex /home/user/image.png
49 *
50 * @return string The full path to the file with the new extension, ex /home/user/image.avif
51 */
52 public static function replace_file_extension( $mimetype, $filepath ) {
53 $parts = pathinfo( $filepath );
54
55 if ( ! isset( $parts['extension'] ) ) {
56 return $filepath;
57 }
58
59 $extension_new = self::mimetype_to_extension( $mimetype );
60 if ( null === $extension_new ) {
61 return $filepath;
62 }
63
64 $dir = $parts['dirname'];
65 $name = $parts['filename'];
66 $sep = DIRECTORY_SEPARATOR;
67
68 if ( '.' === $dir ) {
69 return $name . '.' . $extension_new;
70 }
71
72 if ( $dir === $sep ) {
73 return $sep . $name . '.' . $extension_new;
74 }
75
76 $dir = rtrim( $dir, '/\\' );
77 return $dir . $sep . $name . '.' . $extension_new;
78 }
79
80 private static function mimetype_to_extension( $mimetype ) {
81 switch ( $mimetype ) {
82 case 'image/jpeg':
83 return 'jpg';
84 case 'image/png':
85 return 'png';
86 case 'image/webp':
87 return 'webp';
88 case 'image/avif':
89 return 'avif';
90 default:
91 return null;
92 }
93 }
94
95 /**
96 * Will return the mimetype of the file
97 *
98 * ref: https://www.php.net/manual/en/class.finfo.php
99 *
100 * @param string $input The file contents
101 * @return string The mimetype of the file
102 */
103 public static function get_mimetype( $input ) {
104 if ( class_exists( 'finfo' ) ) {
105 $finfo = new finfo( FILEINFO_MIME_TYPE );
106 $mime = $finfo->buffer( $input );
107 return $mime;
108 } else {
109 throw new Exception( 'finfo extension is not available.' );
110 }
111 }
112
113
114 /**
115 * Gets or initializes the WordPress filesystem instance.
116 *
117 * Returns the global WP_Filesystem instance, initializing it if necessary.
118 * This helper prevents repeated initialization code throughout the plugin.
119 *
120 * @since 3.7.0
121 *
122 * @return WP_Filesystem_Base The WP_Filesystem instance.
123 * @throws Exception If the filesystem cannot be initialized.
124 */
125 public static function get_wp_filesystem() {
126 global $wp_filesystem;
127
128 if ( $wp_filesystem instanceof WP_Filesystem_Base ) {
129 return $wp_filesystem;
130 }
131
132 // Initialize the filesystem only if the function isn't available yet.
133 if ( ! function_exists( 'WP_Filesystem' ) ) {
134 require_once ABSPATH . 'wp-admin/includes/file.php';
135 }
136 WP_Filesystem();
137
138 if ( ! ( $wp_filesystem instanceof WP_Filesystem_Base ) ) {
139 throw new Exception( 'Unable to initialize WordPress filesystem.' );
140 }
141
142 return $wp_filesystem;
143 }
144
145 /**
146 * Polyfill for `str_starts_with()` function added in PHP 8.0.
147 *
148 * Performs a case-sensitive check indicating if
149 * the haystack begins with needle.
150 *
151 * @since 5.9.0
152 *
153 * @param string $haystack The string to search in.
154 * @param string $needle The substring to search for in the `$haystack`.
155 * @return bool True if `$haystack` starts with `$needle`, otherwise false.
156 */
157 public static function str_starts_with( $haystack, $needle ) {
158 if ( function_exists( 'str_starts_with' ) ) {
159 return str_starts_with( $haystack, $needle );
160 }
161
162 if ( '' === $needle ) {
163 return true;
164 }
165
166 return 0 === strpos( $haystack, $needle );
167 }
168 }
169