PluginProbe
Plus WebP or AVIF / 1.00
Plus WebP or AVIF v1.00
5.21 5.20 5.12 5.11 trunk 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 2.00 2.01 2.02 2.03 2.04 2.05 All 47 releases
plus-webp / lib / PlusWebp.php

PlusWebp.php in Plus WebP or AVIF 1.00, at lib/PlusWebp.php

239 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plus WebP
4 *
5 * @package Plus WebP
6 * @subpackage PlusWebp Main function
7 /* Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 $pluswebp = new PlusWebp();
23
24 class PlusWebp {
25
26 private $upload_dir;
27 private $upload_url;
28 private $upload_path;
29
30 /* ==================================================
31 * Construct
32 * @since 1.00
33 */
34 public function __construct() {
35
36 list($this->upload_dir, $this->upload_url, $this->upload_path) = $this->upload_dir_url_path();
37
38 add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 );
39
40 }
41
42 /* ==================================================
43 * Webp generate
44 * @param array $metadata
45 * @param int $attachment_id
46 * @return array $metadata
47 * @since 1.00
48 */
49 public function generate_webp( $metadata, $attachment_id ) {
50
51 $replace = get_option('pluswebp_replace');
52
53 $mime_type = get_post_mime_type($attachment_id);
54 if ( $mime_type == 'image/jpeg' || $mime_type == 'image/png' || $mime_type == 'image/bmp' || $mime_type == 'image/gif' ) {
55 $metadata_webp = $metadata;
56 $file_webp = $this->change_ext( $metadata['file'], 'webp' );
57 $metadata_webp['file'] = $file_webp;
58 foreach ( (array)$metadata['sizes'] as $key => $value ) {
59 $file_thumb = $value['file'];
60 $file_thumb_webp = $this->change_ext( $file_thumb, 'webp' );
61 $path = $this->upload_dir.'/'.dirname($file_webp).'/';
62 $url = $this->upload_url.'/'.dirname($file_webp).'/';
63 $ret = $this->create_webp( $path.$file_thumb, $mime_type, $path.$file_thumb_webp );
64 if ( $ret ) {
65 $metadata_webp['sizes'][$key]['file'] = $file_thumb_webp;
66 $metadata_webp['sizes'][$key]['mime-type'] = 'image/webp';
67 if ( $replace ) {
68 unlink( $path.$file_thumb );
69 $this->change_db( $url.$file_thumb, $url.$file_thumb_webp);
70 }
71 }
72 }
73
74 $ret = $this->create_webp( $this->upload_dir.'/'.$metadata['file'], $mime_type, $this->upload_dir.'/'.$file_webp );
75 if ( $ret ) {
76 if ( $replace ) {
77 $up_post = array(
78 'ID' => $attachment_id,
79 'guid' => $this->upload_url.'/'.$file_webp,
80 'post_mime_type' => 'image/webp'
81 );
82 wp_update_post( $up_post );
83 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
84 wp_update_attachment_metadata( $attachment_id, $metadata_webp );
85 unlink( $this->upload_dir.'/'.$metadata['file'] );
86 $this->change_db( $this->upload_url.'/'.$metadata['file'], $this->upload_url.'/'.$file_webp );
87 } else {
88 $post = get_post($attachment_id);
89 $title = get_the_title($post);
90 $attachment = array(
91 'guid' => $this->upload_url.'/'.$file_webp,
92 'post_mime_type' => 'image/webp',
93 'post_title' => $title,
94 'post_content' => '',
95 'post_status' => 'inherit'
96 );
97 $attach_id = wp_insert_attachment( $attachment, $this->upload_dir.'/'.$file_webp );
98 wp_update_attachment_metadata( $attach_id, $metadata_webp );
99
100 $author = get_userdata($post->post_author);
101 $userid = $author->ID;
102 $postdate = get_the_date('Y-m-d H:i:s', $attachment_id);
103 $postdategmt = get_gmt_from_date($postdate);
104 $up_post = array(
105 'ID' => $attach_id,
106 'post_author' => $userid,
107 'post_date' => $postdate,
108 'post_date_gmt' => $postdategmt,
109 'post_modified' => $postdate,
110 'post_modified_gmt' => $postdategmt
111 );
112 wp_update_post( $up_post );
113 }
114 }
115
116 }
117
118 return $metadata;
119
120 }
121
122 /* ==================================================
123 * Webp create
124 * @param string $filename
125 * @param string $mime_type
126 * @param string $filename_webp
127 * @return bool $ret
128 * @since 1.00
129 */
130 private function create_webp( $filename, $mime_type, $filename_webp ) {
131
132 if ( !file_exists($filename) ) return FALSE;
133 if ( file_exists($filename_webp) ) return FALSE;
134
135 $ret = FALSE;
136 switch ($mime_type) {
137 case 'image/jpeg':
138 $img = imagecreatefromjpeg($filename);
139 break;
140 case 'image/png':
141 $img = imagecreatefrompng($filename);
142 break;
143 case 'image/bmp':
144 $img = imagecreatefrombmp($filename);
145 break;
146 case 'image/gif':
147 $img = imagecreatefromgif($filename);
148 break;
149 }
150
151 @set_time_limit(60);
152 $ret = imagewebp($img, $filename_webp);
153 imagedestroy($img);
154
155 return $ret;
156
157 }
158
159 /* ==================================================
160 * Change ext
161 * @param string $before_file_name
162 * @param string $ext
163 * @return array $after_file_name
164 * @since 1.00
165 */
166 private function change_ext( $before_file_name, $ext ) {
167
168 $exts = explode('.', wp_basename($before_file_name));
169 $file_ext = '.'.end($exts);
170 $after_file_name = rtrim($before_file_name, $file_ext).'.'.$ext;
171
172 return $after_file_name;
173
174 }
175
176 /* ==================================================
177 * Change DB
178 * @param string $before_url
179 * @param string $after_url
180 * @since 1.00
181 */
182 private function change_db( $before_url, $after_url ) {
183
184 global $wpdb;
185
186 // Replace
187 $sql = $wpdb->prepare(
188 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
189 $before_url,
190 $after_url
191 );
192 $wpdb->query($sql);
193
194 return;
195
196 }
197
198 /* ==================================================
199 * @param none
200 * @return array $upload_dir, $upload_url, $upload_path
201 * @since 1.00
202 */
203 public function upload_dir_url_path(){
204
205 $wp_uploads = wp_upload_dir();
206
207 $relation_path_true = strpos($wp_uploads['baseurl'], '../');
208 if ( $relation_path_true > 0 ) {
209 $relationalpath = substr($wp_uploads['baseurl'], $relation_path_true);
210 $basepath = substr($wp_uploads['baseurl'], 0, $relation_path_true);
211 $upload_url = $this->realurl($basepath, $relationalpath);
212 $upload_dir = wp_normalize_path(realpath($wp_uploads['basedir']));
213 } else {
214 $upload_url = $wp_uploads['baseurl'];
215 $upload_dir = wp_normalize_path($wp_uploads['basedir']);
216 }
217
218 if(is_ssl()){
219 $upload_url = str_replace('http:', 'https:', $upload_url);
220 }
221
222 if ( $relation_path_true > 0 ) {
223 $upload_path = $relationalpath;
224 } else {
225 $upload_path = str_replace(site_url('/'), '', $upload_url);
226 }
227
228 $upload_dir = untrailingslashit($upload_dir);
229 $upload_url = untrailingslashit($upload_url);
230 $upload_path = untrailingslashit($upload_path);
231
232 return array($upload_dir, $upload_url, $upload_path);
233
234 }
235
236 }
237
238 ?>
239