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

274 lines 8.1 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 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
108 unlink( $this->upload_dir . '/' . $metadata['file'] );
109 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
110 } else {
111 $post = get_post( $attachment_id );
112 $title = get_the_title( $post );
113 $attachment = array(
114 'guid' => $this->upload_url . '/' . $file_webp,
115 'post_mime_type' => 'image/webp',
116 'post_title' => $title,
117 'post_content' => '',
118 'post_status' => 'inherit',
119 );
120 $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
121 wp_update_attachment_metadata( $attach_id, $metadata_webp );
122
123 $author = get_userdata( $post->post_author );
124 $userid = $author->ID;
125 $postdate = get_the_date( 'Y-m-d H:i:s', $attachment_id );
126 $postdategmt = get_gmt_from_date( $postdate );
127 $up_post = array(
128 'ID' => $attach_id,
129 'post_author' => $userid,
130 'post_date' => $postdate,
131 'post_date_gmt' => $postdategmt,
132 'post_modified' => $postdate,
133 'post_modified_gmt' => $postdategmt,
134 );
135 wp_update_post( $up_post );
136 }
137 }
138 }
139
140 return $metadata;
141
142 }
143
144 /** ==================================================
145 * Webp create
146 *
147 * @param string $filename input filename for pictures.
148 * @param string $mime_type mimetype.
149 * @param string $filename_webp output filename for webp.
150 * @return bool $ret create bool.
151 * @since 1.00
152 */
153 private function create_webp( $filename, $mime_type, $filename_webp ) {
154
155 if ( ! file_exists( $filename ) ) {
156 return false;
157 }
158 if ( file_exists( $filename_webp ) ) {
159 return false;
160 }
161
162 $ret = false;
163 switch ( $mime_type ) {
164 case 'image/jpeg':
165 $src = imagecreatefromjpeg( $filename );
166 break;
167 case 'image/png':
168 $src = imagecreatefrompng( $filename );
169 break;
170 case 'image/bmp':
171 $src = imagecreatefrombmp( $filename );
172 break;
173 case 'image/gif':
174 $src = imagecreatefromgif( $filename );
175 break;
176 }
177
178 $pluswebp_settings = get_option( 'pluswebp' );
179 @set_time_limit( 60 );
180
181 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
182 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
183 imagealphablending( $img, true );
184 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
185 imagedestroy( $src );
186 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
187 imagedestroy( $img );
188
189 return $ret;
190
191 }
192
193 /** ==================================================
194 * Change ext
195 *
196 * @param string $before_file_name before_file_name.
197 * @param string $ext ext.
198 * @return array $after_file_name after_file_name.
199 * @since 1.00
200 */
201 private function change_ext( $before_file_name, $ext ) {
202
203 $exts = explode( '.', wp_basename( $before_file_name ) );
204 $file_ext = '.' . end( $exts );
205 $after_file_name = rtrim( $before_file_name, $file_ext ) . '.' . $ext;
206
207 return $after_file_name;
208
209 }
210
211 /** ==================================================
212 * Change DB
213 *
214 * @param string $before_url before_url.
215 * @param string $after_url after_url.
216 * @since 1.00
217 */
218 private function change_db( $before_url, $after_url ) {
219
220 global $wpdb;
221
222 // Replace.
223 $sql = $wpdb->prepare(
224 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
225 $before_url,
226 $after_url
227 );
228 $wpdb->query( $sql );
229
230 }
231
232 /** ==================================================
233 * Upload Path
234 *
235 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
236 * @since 1.00
237 */
238 public function upload_dir_url_path() {
239
240 $wp_uploads = wp_upload_dir();
241
242 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
243 if ( $relation_path_true > 0 ) {
244 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
245 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
246 $upload_url = $this->realurl( $basepath, $relationalpath );
247 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
248 } else {
249 $upload_url = $wp_uploads['baseurl'];
250 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
251 }
252
253 if ( is_ssl() ) {
254 $upload_url = str_replace( 'http:', 'https:', $upload_url );
255 }
256
257 if ( $relation_path_true > 0 ) {
258 $upload_path = $relationalpath;
259 } else {
260 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
261 }
262
263 $upload_dir = untrailingslashit( $upload_dir );
264 $upload_url = untrailingslashit( $upload_url );
265 $upload_path = untrailingslashit( $upload_path );
266
267 return array( $upload_dir, $upload_url, $upload_path );
268
269 }
270
271 }
272
273
274