PluginProbe
Plus WebP or AVIF / 1.06
Plus WebP or AVIF v1.06
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.06, at lib/class-pluswebp.php

280 lines 8.3 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 $ret = false;
168 switch ( $mime_type ) {
169 case 'image/jpeg':
170 $src = imagecreatefromjpeg( $filename );
171 break;
172 case 'image/png':
173 $src = imagecreatefrompng( $filename );
174 break;
175 case 'image/bmp':
176 $src = imagecreatefrombmp( $filename );
177 break;
178 case 'image/gif':
179 $src = imagecreatefromgif( $filename );
180 break;
181 }
182
183 $pluswebp_settings = get_option( 'pluswebp' );
184 @set_time_limit( 60 );
185
186 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
187 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
188 imagealphablending( $img, true );
189 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
190 imagedestroy( $src );
191 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
192 imagedestroy( $img );
193
194 return $ret;
195
196 }
197
198 /** ==================================================
199 * Change ext
200 *
201 * @param string $before_file_name before_file_name.
202 * @param string $ext ext.
203 * @return array $after_file_name after_file_name.
204 * @since 1.00
205 */
206 private function change_ext( $before_file_name, $ext ) {
207
208 $exts = explode( '.', wp_basename( $before_file_name ) );
209 $file_ext = '.' . end( $exts );
210 $after_file_name = rtrim( $before_file_name, $file_ext ) . '.' . $ext;
211
212 return $after_file_name;
213
214 }
215
216 /** ==================================================
217 * Change DB
218 *
219 * @param string $before_url before_url.
220 * @param string $after_url after_url.
221 * @since 1.00
222 */
223 private function change_db( $before_url, $after_url ) {
224
225 global $wpdb;
226
227 /* Replace */
228 $wpdb->query(
229 $wpdb->prepare(
230 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
231 $before_url,
232 $after_url
233 )
234 );
235
236 }
237
238 /** ==================================================
239 * Upload Path
240 *
241 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
242 * @since 1.00
243 */
244 public function upload_dir_url_path() {
245
246 $wp_uploads = wp_upload_dir();
247
248 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
249 if ( $relation_path_true > 0 ) {
250 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
251 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
252 $upload_url = $this->realurl( $basepath, $relationalpath );
253 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
254 } else {
255 $upload_url = $wp_uploads['baseurl'];
256 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
257 }
258
259 if ( is_ssl() ) {
260 $upload_url = str_replace( 'http:', 'https:', $upload_url );
261 }
262
263 if ( $relation_path_true > 0 ) {
264 $upload_path = $relationalpath;
265 } else {
266 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
267 }
268
269 $upload_dir = untrailingslashit( $upload_dir );
270 $upload_url = untrailingslashit( $upload_url );
271 $upload_path = untrailingslashit( $upload_path );
272
273 return array( $upload_dir, $upload_url, $upload_path );
274
275 }
276
277 }
278
279
280