Changeset 2871770
- Timestamp:
- 02/27/2023 03:32:07 PM (3 years ago)
- Location:
- fastware-webpavif/trunk
- Files:
-
- 1 added
- 1 deleted
- 3 edited
-
fastware-webp-avif.php (deleted)
-
fastware-webpavif.php (added)
-
includes/ImageConverter.php (modified) (8 diffs)
-
readme.txt (modified) (2 diffs)
-
templates/.htaccess (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
fastware-webpavif/trunk/includes/ImageConverter.php
r2871086 r2871770 9 9 { 10 10 11 public function skipConvertJpgPngToWebpAvif(string $path): void {11 public function queueConvertJpgPngToWebpAvif(string $path): void { 12 12 $path = wp_normalize_path(sanitize_text_field($path)); 13 13 if (!$this->validatePath($path, ['.jpeg','.jpg','.png'])) … … 23 23 if ($this->supportsAvif()) 24 24 touch($avifSkipPath); 25 26 // get / create working directory 27 $workingDir = $this->getWorkingDir(); 28 if (!is_dir($workingDir)) { 29 wp_mkdir_p($workingDir); 30 } 31 32 // add to queue 33 file_put_contents($workingDir . '/' . md5($path).'.queued', $path); 34 } 35 36 /** 37 * Converts the queued files 38 * @return void 39 */ 40 public function convertQueuedJpgPngToWebpAvif(): void { 41 $loopCount = 1; 42 do { 43 // convert queued files 44 $fileList = glob($this->getWorkingDir() . '/*.queued'); 45 foreach ($fileList as $path) { 46 $imagePath = trim(file_get_contents($path)); 47 if ($this->validatePath($imagePath, ['.jpeg', '.jpg', '.png'])) { 48 $this->convertJpgPngToWebpAvif($imagePath); 49 } 50 unlink($path); 51 } 52 } while ($loopCount++ < 10 && count($fileList) > 0); 53 } 54 55 /** 56 * Converts the given image to png 57 * @param string $path 58 * @return bool 59 */ 60 public function convertWebpToPng(string $path): bool 61 { 62 $path = wp_normalize_path(sanitize_text_field($path)); 63 if (!$this->validatePath($path, ['.webp'])) 64 return false; 65 66 $this->convert($path, 'png'); 67 68 // return whether png file is successfully created 69 return is_file($path . '.png'); 25 70 } 26 71 … … 28 73 * Converts the given image to webp / avif 29 74 * @param string $path 30 * @return bool31 */ 32 p ublicfunction convertJpgPngToWebpAvif(string $path): void75 * @return void 76 */ 77 private function convertJpgPngToWebpAvif(string $path): void 33 78 { 34 79 $path = wp_normalize_path(sanitize_text_field($path)); … … 43 88 if ($this->supportsAvif()) 44 89 $this->convert($path, 'avif'); 45 46 return;47 }48 49 /**50 * Converts the given image to png51 * @param string $path52 * @return bool53 */54 public function convertWebpToPng(string $path): bool55 {56 $path = wp_normalize_path(sanitize_text_field($path));57 if (!$this->validatePath($path, ['.webp']))58 return false;59 60 $this->convert($path, 'png');61 62 // return whether png file is successfully created63 return is_file($path . '.png');64 90 } 65 91 … … 221 247 $htaccessContent = $templateHtaccessContent . "\n\n" . $htaccessContent; 222 248 223 // make sure the .htaccess is writable 224 @chmod($htaccessPath, 0750); 249 // write htaccess 225 250 file_put_contents($htaccessPath, trim($htaccessContent)); 226 251 } … … 255 280 256 281 /** 257 * Removes all converted images 258 * @param string $path 259 * @return void 260 */ 261 public function deleteAllConvertedImages(string $path = WP_CONTENT_DIR): void { 282 * Remove all generated files 283 * @return void 284 */ 285 public function removeGeneratedFiles(): void { 286 // delete working dir 287 /*$filesystem = new \WP_Filesystem_Direct(); 288 $filesystem->delete($this->getWorkingDir(), true);*/ 289 262 290 // @todo: remove converted images 263 291 … … 277 305 $htaccessContent = (string)file_get_contents($htaccessPath); 278 306 $htaccessContent = preg_replace('|### BEGIN Fastware ###.+### END Fastware ###|s', '', $htaccessContent); 279 // make sure the .htaccess is writable 280 @chmod($htaccessPath, 0750); 281 file_put_contents($htaccessPath, trim($htaccessContent)); 282 } 283 284 /** 285 * Loads the image with Imagick 286 * @param string $path 287 * @return \Imagick | null 288 */ 289 private function loadWithImagick(string $path): ?\Imagick 290 { 291 if (!class_exists('\Imagick')) 292 return null; 293 294 try { 295 // load file 296 $imagick = new \Imagick($path); 297 298 // strip image 299 $imagick->stripImage(); 300 301 return $imagick; 302 } catch (\Exception $e) { 303 304 } 305 return null; 307 $htaccessContent = trim($htaccessContent); 308 // write htaccess 309 if (strlen($htaccessContent) === 0) 310 unlink($htaccessPath); 311 else 312 file_put_contents($htaccessPath, $htaccessContent); 306 313 } 307 314 … … 318 325 } 319 326 327 private function getWorkingDir(): string { 328 return wp_normalize_path(WP_CONTENT_DIR . '/fastware-webpavif'); 329 } 330 320 331 } 321 332 } -
fastware-webpavif/trunk/readme.txt
r2871086 r2871770 2 2 Contributors: fastware 3 3 Tags: webp avif image optimize 4 Stable tag: 1.0. 34 Stable tag: 1.0.4 5 5 Requires at least: 5.9 6 6 Tested up to: 6.1 7 Requires PHP: 8.07 Requires PHP: 7.4 8 8 License: GPLv3 9 9 … … 26 26 **Server requirements** 27 27 - Apache 2.x with mod_rewrite enabled 28 - ImageMagick or GDlib with websupport28 - ImageMagick or GDlib with WebP support 29 29 - Optional: ImageMagick with AVIF support (from ImageMagick v7.0.25 and greater) 30 30 31 After activating the plugin, you might notice a slight (temporary) increase of the server load because the images will be converted once upon request.32 33 31 == Changelog == 32 - 1.0.4 [27-02-2023] Added support for PHP 7.4, fixed issue with scheduled converts 34 33 - 1.0.3 [22-02-2023] Adjust code to improve quality 35 34 - 1.0.2 [21-02-2023] Bugfix in GD convert -
fastware-webpavif/trunk/templates/.htaccess
r2871086 r2871770 4 4 RewriteOptions Inherit 5 5 6 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|avif|webp)$ 6 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|avif|webp)$ [NC] 7 7 RewriteCond %{REQUEST_FILENAME} !-f 8 8 RewriteRule ".*" "-" [L] … … 10 10 ### IF AVIF SUPPORTED ### 11 11 # serve avif image 12 RewriteCond %{REQUEST_FILENAME} \.avif$ 13 RewriteRule .* - [ NC,T=image/avif,L]12 RewriteCond %{REQUEST_FILENAME} \.avif$ [NC] 13 RewriteRule .* - [T=image/avif,L] 14 14 15 15 # serve avif instead of jpg|jpeg|png? 16 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ 16 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC] 17 17 RewriteCond %{HTTP_ACCEPT} image/avif 18 18 RewriteCond %{REQUEST_FILENAME}.avif -f 19 RewriteRule .* %{REQUEST_FILENAME}.avif [ NC,L]19 RewriteRule .* %{REQUEST_FILENAME}.avif [L] 20 20 21 21 # generate avif image? 22 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ 22 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC] 23 23 RewriteCond %{HTTP_ACCEPT} image/avif 24 24 RewriteCond %{QUERY_STRING} !skip … … 30 30 ### IF WEBP SUPPORTED ### 31 31 # serve png instead of webp? 32 RewriteCond %{REQUEST_FILENAME} \.webp$ 32 RewriteCond %{REQUEST_FILENAME} \.webp$ [NC] 33 33 RewriteCond %{HTTP_ACCEPT} !image/webp 34 34 RewriteCond %{REQUEST_FILENAME}.png -f 35 RewriteRule .* %{REQUEST_FILENAME}.png [ NC,L]35 RewriteRule .* %{REQUEST_FILENAME}.png [L] 36 36 37 37 # generate png image? 38 RewriteCond %{REQUEST_FILENAME} \.webp$ 39 RewriteCond %{REQUEST_FILENAME} !\.png\.webp$ 38 RewriteCond %{REQUEST_FILENAME} \.webp$ [NC] 39 RewriteCond %{REQUEST_FILENAME} !\.png\.webp$ [NC] 40 40 RewriteCond %{HTTP_ACCEPT} !image/webp 41 41 RewriteCond %{QUERY_STRING} !skip … … 45 45 46 46 # serve webp image 47 RewriteCond %{REQUEST_FILENAME} \.webp$ 48 RewriteRule .* - [ NC,T=image/webp,L]47 RewriteCond %{REQUEST_FILENAME} \.webp$ [NC] 48 RewriteRule .* - [T=image/webp,L] 49 49 50 50 # serve webp instead of jpg|jpeg|png? 51 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ 51 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC] 52 52 RewriteCond %{HTTP_ACCEPT} image/webp 53 53 RewriteCond %{REQUEST_FILENAME}.webp -f 54 RewriteRule .* %{REQUEST_FILENAME}.webp [ NC,L]54 RewriteRule .* %{REQUEST_FILENAME}.webp [L] 55 55 56 56 # generate webp image? 57 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ 57 RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC] 58 58 RewriteCond %{HTTP_ACCEPT} image/webp 59 59 RewriteCond %{QUERY_STRING} !skip
Note: See TracChangeset
for help on using the changeset viewer.