PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.6.2
TinyPNG – JPEG, PNG & WebP image compression v3.6.2
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 1 year ago config 4 years ago css 1 year ago data 4 years ago images 4 years ago js 1 year ago vendor 1 year ago views 10 months ago class-tiny-bulk-optimization.php 1 year ago class-tiny-cli.php 10 months ago class-tiny-compress-client.php 1 year ago class-tiny-compress-fopen.php 1 year ago class-tiny-compress.php 1 year ago class-tiny-exception.php 4 years ago class-tiny-helpers.php 1 year ago class-tiny-image-size.php 1 year ago class-tiny-image.php 1 year ago class-tiny-notices.php 10 months ago class-tiny-php.php 4 years ago class-tiny-picture.php 10 months ago class-tiny-plugin.php 10 months ago class-tiny-settings.php 10 months ago class-tiny-wp-base.php 10 months ago
class-tiny-helpers.php
112 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 * truncate_text will truncate a string to a given length.
25 * When text is longer than the given length, the string will be truncated and
26 * the last characters will be replaced with an ellipsis.
27 *
28 * We can use mb_strlen & mb_substr as WordPress provides a compat function for
29 * it if mbstring php module is not installed.
30 *
31 * @param string $text the text
32 * @param integer $length the maximum length of the string
33 * @return string the truncated string
34 */
35 public static function truncate_text( $text, $length ) {
36 if ( mb_strlen( $text ) > $length ) {
37 return mb_substr( $text, 0, $length - 3 ) . '...';
38 }
39 return $text;
40 }
41
42 /**
43 * Will replace the file extension with the specified mimetype.
44 *
45 * @param string $mimetype The format to replace the extension with.
46 * Currently supports 'image/avif' or 'image/webp'
47 * @param string $filepath The full path to replace the extension in, ex /home/user/image.png
48 *
49 * @return string The full path to the file with the new extension, ex /home/user/image.avif
50 */
51 public static function replace_file_extension( $mimetype, $filepath ) {
52 $parts = pathinfo( $filepath );
53
54 if ( ! isset( $parts['extension'] ) ) {
55 return $filepath;
56 }
57
58 $extension_new = self::mimetype_to_extension( $mimetype );
59 if ( null === $extension_new ) {
60 return $filepath;
61 }
62
63 $dir = $parts['dirname'];
64 $name = $parts['filename'];
65 $sep = DIRECTORY_SEPARATOR;
66
67 if ( '.' === $dir ) {
68 return $name . '.' . $extension_new;
69 }
70
71 if ( $dir === $sep ) {
72 return $sep . $name . '.' . $extension_new;
73 }
74
75 $dir = rtrim( $dir, '/\\' );
76 return $dir . $sep . $name . '.' . $extension_new;
77 }
78
79 private static function mimetype_to_extension( $mimetype ) {
80 switch ( $mimetype ) {
81 case 'image/jpeg':
82 return 'jpg';
83 case 'image/png':
84 return 'png';
85 case 'image/webp':
86 return 'webp';
87 case 'image/avif':
88 return 'avif';
89 default:
90 return null;
91 }
92 }
93
94 /**
95 * Will return the mimetype of the file
96 *
97 * ref: https://www.php.net/manual/en/class.finfo.php
98 *
99 * @param string $input The file contents
100 * @return string The mimetype of the file
101 */
102 public static function get_mimetype( $input ) {
103 if ( class_exists( 'finfo' ) ) {
104 $finfo = new finfo( FILEINFO_MIME_TYPE );
105 $mime = $finfo->buffer( $input );
106 return $mime;
107 } else {
108 throw new Exception( 'finfo extension is not available.' );
109 }
110 }
111 }
112