PluginProbe
Plus WebP or AVIF / 1.07
Plus WebP or AVIF v1.07
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 / class-pluswebp.php

class-pluswebp.php in Plus WebP or AVIF 1.07, at lib/class-pluswebp.php

289 lines 8.7 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 /** ==================================================
25 * Class Main function
26 *
27 * @since 1.00
28 */
29 class PlusWebp {
30
31 /** ==================================================
32 * Dir
33 *
34 * @var $upload_dir DIR.
35 */
36 private $upload_dir;
37 /** ==================================================
38 * Dir
39 *
40 * @var $upload_url URL.
41 */
42 private $upload_url;
43 /** ==================================================
44 * Dir
45 *
46 * @var $upload_path PATH.
47 */
48 private $upload_path;
49
50 /** ==================================================
51 * Construct
52 *
53 * @since 1.00
54 */
55 public function __construct() {
56
57 list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path();
58
59 add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 );
60
61 }
62
63 /** ==================================================
64 * Webp generate
65 *
66 * @param array $metadata metadata.
67 * @param int $attachment_id ID.
68 * @return array $metadata metadata.
69 * @since 1.00
70 */
71 public function generate_webp( $metadata, $attachment_id ) {
72
73 $pluswebp_settings = get_option( 'pluswebp' );
74 $replace = $pluswebp_settings['replace'];
75
76 $mime_type = get_post_mime_type( $attachment_id );
77 if ( 'image/jpeg' === $mime_type || 'image/png' === $mime_type || 'image/bmp' === $mime_type || 'image/gif' === $mime_type ) {
78 $metadata_webp = $metadata;
79 $file_webp = $this->change_ext( $metadata['file'], 'webp' );
80 $metadata_webp['file'] = $file_webp;
81 foreach ( (array) $metadata['sizes'] as $key => $value ) {
82 $file_thumb = $value['file'];
83 $file_thumb_webp = $this->change_ext( $file_thumb, 'webp' );
84 $path = $this->upload_dir . '/' . dirname( $file_webp ) . '/';
85 $url = $this->upload_url . '/' . dirname( $file_webp ) . '/';
86 $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp );
87 if ( $ret ) {
88 $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
89 $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp';
90 if ( $replace ) {
91 unlink( $path . $file_thumb );
92 $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
93 }
94 }
95 }
96
97 $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp );
98 if ( $ret ) {
99 if ( $replace ) {
100 $up_post = array(
101 'ID' => $attachment_id,
102 'guid' => $this->upload_url . '/' . $file_webp,
103 'post_mime_type' => 'image/webp',
104 );
105 wp_update_post( $up_post );
106 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
107 /* for bulk generate */
108 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
109 /* delete org file */
110 unlink( $this->upload_dir . '/' . $metadata['file'] );
111 /* Replace */
112 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
113 /* for hook */
114 $metadata = $metadata_webp;
115 } else {
116 $post = get_post( $attachment_id );
117 $title = get_the_title( $post );
118 $attachment = array(
119 'guid' => $this->upload_url . '/' . $file_webp,
120 'post_mime_type' => 'image/webp',
121 'post_title' => $title,
122 'post_content' => '',
123 'post_status' => 'inherit',
124 );
125 $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
126 wp_update_attachment_metadata( $attach_id, $metadata_webp );
127
128 $author = get_userdata( $post->post_author );
129 $userid = $author->ID;
130 $postdate = get_the_date( 'Y-m-d H:i:s', $attachment_id );
131 $postdategmt = get_gmt_from_date( $postdate );
132 $up_post = array(
133 'ID' => $attach_id,
134 'post_author' => $userid,
135 'post_date' => $postdate,
136 'post_date_gmt' => $postdategmt,
137 'post_modified' => $postdate,
138 'post_modified_gmt' => $postdategmt,
139 );
140 wp_update_post( $up_post );
141 }
142 }
143 }
144
145 return $metadata;
146
147 }
148
149 /** ==================================================
150 * Webp create
151 *
152 * @param string $filename input filename for pictures.
153 * @param string $mime_type mimetype.
154 * @param string $filename_webp output filename for webp.
155 * @return bool $ret create bool.
156 * @since 1.00
157 */
158 private function create_webp( $filename, $mime_type, $filename_webp ) {
159
160 if ( ! file_exists( $filename ) ) {
161 return false;
162 }
163 if ( file_exists( $filename_webp ) ) {
164 return false;
165 }
166
167 $pluswebp_settings = get_option( 'pluswebp' );
168 @set_time_limit( 60 );
169
170 $ret = false;
171 switch ( $mime_type ) {
172 case 'image/jpeg':
173 $src = imagecreatefromjpeg( $filename );
174 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
175 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
176 imagealphablending( $img, true );
177 break;
178 case 'image/png':
179 $src = imagecreatefrompng( $filename );
180 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
181 imagealphablending( $img, false );
182 imagesavealpha( $img, true );
183 break;
184 case 'image/bmp':
185 $src = imagecreatefrombmp( $filename );
186 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
187 break;
188 case 'image/gif':
189 $src = imagecreatefromgif( $filename );
190 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
191 $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
192 imagefill( $img, 0, 0, $bgcolor );
193 imagecolortransparent( $img, $bgcolor );
194 break;
195 }
196
197 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
198 imagedestroy( $src );
199 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
200 imagedestroy( $img );
201
202 return $ret;
203
204 }
205
206 /** ==================================================
207 * Change ext
208 *
209 * @param string $before_file_name before_file_name.
210 * @param string $ext ext.
211 * @return array $after_file_name after_file_name.
212 * @since 1.00
213 */
214 private function change_ext( $before_file_name, $ext ) {
215
216 $exts = explode( '.', $before_file_name );
217 $before_ext = '.' . end( $exts );
218 $after_ext = '.' . $ext;
219 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
220
221 return $after_file_name;
222
223 }
224
225 /** ==================================================
226 * Change DB
227 *
228 * @param string $before_url before_url.
229 * @param string $after_url after_url.
230 * @since 1.00
231 */
232 private function change_db( $before_url, $after_url ) {
233
234 global $wpdb;
235
236 /* Replace */
237 $wpdb->query(
238 $wpdb->prepare(
239 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
240 $before_url,
241 $after_url
242 )
243 );
244
245 }
246
247 /** ==================================================
248 * Upload Path
249 *
250 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
251 * @since 1.00
252 */
253 public function upload_dir_url_path() {
254
255 $wp_uploads = wp_upload_dir();
256
257 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
258 if ( $relation_path_true > 0 ) {
259 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
260 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
261 $upload_url = $this->realurl( $basepath, $relationalpath );
262 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
263 } else {
264 $upload_url = $wp_uploads['baseurl'];
265 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
266 }
267
268 if ( is_ssl() ) {
269 $upload_url = str_replace( 'http:', 'https:', $upload_url );
270 }
271
272 if ( $relation_path_true > 0 ) {
273 $upload_path = $relationalpath;
274 } else {
275 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
276 }
277
278 $upload_dir = untrailingslashit( $upload_dir );
279 $upload_url = untrailingslashit( $upload_url );
280 $upload_path = untrailingslashit( $upload_path );
281
282 return array( $upload_dir, $upload_url, $upload_path );
283
284 }
285
286 }
287
288
289