Changeset 2876008
- Timestamp:
- 03/07/2023 03:46:29 PM (3 years ago)
- Location:
- fastware-webpavif/trunk
- Files:
-
- 4 edited
-
fastware-webpavif.php (modified) (2 diffs)
-
includes/ImageConverter.php (modified) (9 diffs)
-
readme.txt (modified) (2 diffs)
-
templates/.htaccess (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
fastware-webpavif/trunk/fastware-webpavif.php
r2874818 r2876008 3 3 * Plugin Name: WebP/AVIF image optimizer 4 4 * Description: Fast & simple plugin to automatically serve WebP & AVIF images to browsers which support those formats (with fallback to original format). 5 * Version: 1.0. 55 * Version: 1.0.6 6 6 * License: GPLv3 7 7 * Author: Fastware … … 113 113 114 114 // redirect to current url after scheduling convert 115 wp_redirect($redirectUrl );115 wp_redirect($redirectUrl.'?skip'); 116 116 exit(0); 117 117 } -
fastware-webpavif/trunk/includes/ImageConverter.php
r2874818 r2876008 15 15 16 16 // skip webp 17 if ($this->supportsWebp()) 18 touch($this->getWorkingPath($path,'webp') . '.skip'); 17 $webpPath = $this->getWorkingPath($path,'webp'); 18 if ($this->supportsWebp() && !is_file($webpPath)) 19 touch($webpPath . '.skip'); 19 20 20 21 // skip avif 21 if ($this->supportsAvif()) 22 touch($this->getWorkingPath($path,'avif') . '.skip'); 22 $avifPath = $this->getWorkingPath($path,'avif'); 23 if ($this->supportsAvif() && !is_file($avifPath)) 24 touch($avifPath . '.skip'); 23 25 24 26 // add to queue … … 31 33 */ 32 34 public function convertQueuedJpgPngToWebpAvif(): void { 35 // clear stat cache first 36 clearstatcache(); 37 38 // get lock (timeout lock after 5 minutes) 39 $lockPath = $this->getWorkingDir() . '/process.lock'; 40 if (is_file($lockPath) && filemtime($lockPath) > time()-300) 41 return; 42 touch($lockPath); 43 33 44 $loopCount = 1; 34 45 do { … … 36 47 $fileList = glob($this->getWorkingDir() . '/*.queued'); 37 48 foreach ($fileList as $path) { 49 // reset timeout counter to keep process running - max 300 seconds per image 50 set_time_limit(min(300,(int) ini_get('max_execution_time'))); 51 38 52 $imagePath = trim(file_get_contents($path)); 39 53 if ($this->validatePath($imagePath, ['.jpeg', '.jpg', '.png'])) { 40 54 $this->convertJpgPngToWebpAvif($imagePath); 41 55 } 42 unlink($path); 56 @unlink($path); 57 58 // keep lock alive 59 touch($lockPath); 43 60 } 44 61 } while ($loopCount++ < 10 && count($fileList) > 0); 62 63 @unlink($lockPath); 45 64 } 46 65 … … 108 127 return; 109 128 129 // get convert path 130 $convertPath = $this->getWorkingPath($path, $format); 131 132 // add skip file (serve original file while converting) 133 $skipPath = $convertPath . '.skip'; 134 touch($skipPath); 135 136 // skip conversion when converted file already exists, and its size > 0 137 if (is_file($convertPath) && filesize($convertPath) > 0) { 138 @unlink($skipPath); 139 return; 140 } 141 142 // keep script running until conversion is done 143 ignore_user_abort( true ); 144 110 145 // @temporary: clean up previously cached files which were generated in plugin version < 1.0.5 (to remove in future) 111 146 foreach (['','.skip','.tmp'] as $suffix) { 112 147 $removePath = $path . '.' . $format . $suffix; 113 148 if (is_file($removePath)) { 114 unlink($removePath); 115 } 116 } 117 118 // get convert path 119 $convertPath = $this->getWorkingPath($path, $format); 120 121 // skip conversion when converted file already exists, and its size > 0 122 if (is_file($convertPath) && filesize($convertPath) > 0) 123 return; 124 125 // keep script running until conversion is done 126 ignore_user_abort( true ); 127 128 // add skip file (serve original file while converting) 129 $skipPath = $convertPath . '.skip'; 130 touch($skipPath); 149 @unlink($removePath); 150 } 151 } 131 152 132 153 // initialize … … 206 227 } 207 228 229 // clear stat cache before checking filesize 230 clearstatcache(); 208 231 if ($convertResult 209 232 && ($filesize = filesize($tmpPath)) > 0 … … 212 235 213 236 // unlink skip path after successful conversion 214 unlink($skipPath);237 @unlink($skipPath); 215 238 } else { 216 239 // remove temp file when conversion failed 217 240 if (is_file($tmpPath)) 218 unlink($tmpPath);241 @unlink($tmpPath); 219 242 } 220 243 } … … 241 264 $removePath = $this->getWorkingPath($path, $extension) . $suffix; 242 265 if (is_file($removePath)) { 243 unlink($removePath);266 @unlink($removePath); 244 267 } 245 268 } … … 268 291 $this->rmdir($path); 269 292 if (is_file($path)) 270 unlink($path);293 @unlink($path); 271 294 } 272 295 } … … 327 350 // write htaccess 328 351 if (strlen($htaccessContent) === 0) 329 unlink($htaccessPath);352 @unlink($htaccessPath); 330 353 else 331 354 file_put_contents($htaccessPath, $htaccessContent); -
fastware-webpavif/trunk/readme.txt
r2875519 r2876008 2 2 Contributors: fastware 3 3 Tags: webp avif image optimize pagespeed 4 Stable tag: 1.0. 54 Stable tag: 1.0.6 5 5 Requires at least: 5.9 6 6 Tested up to: 6.1 … … 47 47 48 48 == Changelog == 49 - 1.0.6 [07-03-2023] Fixed redirect bug on servers without avif support 49 50 - 1.0.5 [04-03-2023] Moved working directory, to improve cleanup after uninstall 50 51 - 1.0.4 [27-02-2023] Added support for PHP 7.4, fixed issue with scheduled converts -
fastware-webpavif/trunk/templates/.htaccess
r2874818 r2876008 9 9 RewriteRule .* - [T=image/%1,L] 10 10 11 ## IF AVIF SUPPORTED ###11 ### IF AVIF SUPPORTED ### 12 12 # serve avif instead of jpg|jpeg|png? 13 13 RewriteCond %{HTTP_ACCEPT} image/avif
Note: See TracChangeset
for help on using the changeset viewer.