Plugin Directory

Changeset 2871770


Ignore:
Timestamp:
02/27/2023 03:32:07 PM (3 years ago)
Author:
fastware
Message:

update to v1.0.4

Location:
fastware-webpavif/trunk
Files:
1 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • fastware-webpavif/trunk/includes/ImageConverter.php

    r2871086 r2871770  
    99    {
    1010
    11         public function skipConvertJpgPngToWebpAvif(string $path): void {
     11        public function queueConvertJpgPngToWebpAvif(string $path): void {
    1212            $path = wp_normalize_path(sanitize_text_field($path));
    1313            if (!$this->validatePath($path, ['.jpeg','.jpg','.png']))
     
    2323            if ($this->supportsAvif())
    2424                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');
    2570        }
    2671
     
    2873         * Converts the given image to webp / avif
    2974         * @param string $path
    30          * @return bool
    31          */
    32         public function convertJpgPngToWebpAvif(string $path): void
     75         * @return void
     76         */
     77        private function convertJpgPngToWebpAvif(string $path): void
    3378        {
    3479            $path = wp_normalize_path(sanitize_text_field($path));
     
    4388            if ($this->supportsAvif())
    4489                $this->convert($path, 'avif');
    45 
    46             return;
    47         }
    48 
    49         /**
    50          * Converts the given image to png
    51          * @param string $path
    52          * @return bool
    53          */
    54         public function convertWebpToPng(string $path): bool
    55         {
    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 created
    63             return is_file($path . '.png');
    6490        }
    6591
     
    221247                $htaccessContent = $templateHtaccessContent . "\n\n" . $htaccessContent;
    222248
    223                 // make sure the .htaccess is writable
    224                 @chmod($htaccessPath, 0750);
     249                // write htaccess
    225250                file_put_contents($htaccessPath, trim($htaccessContent));
    226251            }
     
    255280
    256281        /**
    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
    262290            // @todo: remove converted images
    263291
     
    277305            $htaccessContent = (string)file_get_contents($htaccessPath);
    278306            $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);
    306313        }
    307314
     
    318325        }
    319326
     327        private function getWorkingDir(): string {
     328            return wp_normalize_path(WP_CONTENT_DIR . '/fastware-webpavif');
     329        }
     330
    320331    }
    321332}
  • fastware-webpavif/trunk/readme.txt

    r2871086 r2871770  
    22Contributors: fastware
    33Tags: webp avif image optimize
    4 Stable tag: 1.0.3
     4Stable tag: 1.0.4
    55Requires at least: 5.9
    66Tested up to: 6.1
    7 Requires PHP: 8.0
     7Requires PHP: 7.4
    88License: GPLv3
    99
     
    2626**Server requirements**
    2727- Apache 2.x with mod_rewrite enabled
    28 - ImageMagick or GDlib with web support
     28- ImageMagick or GDlib with WebP support
    2929- Optional: ImageMagick with AVIF support (from ImageMagick v7.0.25 and greater)
    3030
    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 
    3331== Changelog ==
     32- 1.0.4 [27-02-2023] Added support for PHP 7.4, fixed issue with scheduled converts
    3433- 1.0.3 [22-02-2023] Adjust code to improve quality
    3534- 1.0.2 [21-02-2023] Bugfix in GD convert
  • fastware-webpavif/trunk/templates/.htaccess

    r2871086 r2871770  
    44  RewriteOptions Inherit
    55
    6   RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|avif|webp)$
     6  RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|avif|webp)$ [NC]
    77  RewriteCond %{REQUEST_FILENAME} !-f
    88  RewriteRule ".*" "-" [L]
     
    1010  ### IF AVIF SUPPORTED ###
    1111  # 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]
    1414
    1515  # serve avif instead of jpg|jpeg|png?
    16   RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$
     16  RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC]
    1717  RewriteCond %{HTTP_ACCEPT} image/avif
    1818  RewriteCond %{REQUEST_FILENAME}.avif -f
    19   RewriteRule .* %{REQUEST_FILENAME}.avif [NC,L]
     19  RewriteRule .* %{REQUEST_FILENAME}.avif [L]
    2020
    2121  # generate avif image?
    22   RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$
     22  RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC]
    2323  RewriteCond %{HTTP_ACCEPT} image/avif
    2424  RewriteCond %{QUERY_STRING} !skip
     
    3030  ### IF WEBP SUPPORTED ###
    3131  # serve png instead of webp?
    32   RewriteCond %{REQUEST_FILENAME} \.webp$
     32  RewriteCond %{REQUEST_FILENAME} \.webp$ [NC]
    3333  RewriteCond %{HTTP_ACCEPT} !image/webp
    3434  RewriteCond %{REQUEST_FILENAME}.png -f
    35   RewriteRule .* %{REQUEST_FILENAME}.png [NC,L]
     35  RewriteRule .* %{REQUEST_FILENAME}.png [L]
    3636
    3737  # 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]
    4040  RewriteCond %{HTTP_ACCEPT} !image/webp
    4141  RewriteCond %{QUERY_STRING} !skip
     
    4545
    4646  # 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]
    4949
    5050  # serve webp instead of jpg|jpeg|png?
    51   RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$
     51  RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC]
    5252  RewriteCond %{HTTP_ACCEPT} image/webp
    5353  RewriteCond %{REQUEST_FILENAME}.webp -f
    54   RewriteRule .* %{REQUEST_FILENAME}.webp [NC,L]
     54  RewriteRule .* %{REQUEST_FILENAME}.webp [L]
    5555
    5656  # generate webp image?
    57   RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$
     57  RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png)$ [NC]
    5858  RewriteCond %{HTTP_ACCEPT} image/webp
    5959  RewriteCond %{QUERY_STRING} !skip
Note: See TracChangeset for help on using the changeset viewer.