Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(PHP_SAPI !== 'cli'){
- echo 'Скрипт запущен не из консоли!';
- return;
- }
- $UPLOAD_DIR = realpath(__DIR__ ."/../.." )."/upload/";
- convertInDir($UPLOAD_DIR);
- global $ALLOW_FILE_EXTS;
- $ALLOW_FILE_EXTS = ['jpg' , 'jpeg' , 'png'];
- function convertInDir($UPLOAD_DIR){
- $odir = opendir($UPLOAD_DIR);
- global $ALLOW_FILE_EXTS;
- $ALLOW_FILE_EXTS = ['jpg' , 'jpeg' , 'png'];
- while (($file = readdir($odir)) !== FALSE)
- {
- if ($file != '.' && $file != '..')
- {
- if(is_dir($UPLOAD_DIR . $file)){
- scanDirectory($UPLOAD_DIR . $file);
- }else if(isNeedConvert($UPLOAD_DIR . $file)){
- convert($UPLOAD_DIR . $file);
- }
- }
- }
- closedir($odir);
- }
- function scanDirectory($dir){
- $odir = opendir($dir);
- while (($file = readdir($odir)) !== FALSE)
- {
- if ($file != '.' && $file != '..')
- {
- $pathfile = $dir . '/' . $file;
- if(is_dir($pathfile)){
- scanDirectory($pathfile);
- }else if(isNeedConvert($pathfile)){
- convert($pathfile);
- }
- }
- }
- closedir($odir);
- }
- function isNeedConvert($pathfile){
- global $ALLOW_FILE_EXTS;
- $pathinfo = pathinfo($pathfile);
- return is_file($pathfile) && in_array(strtolower($pathinfo['extension']) , $ALLOW_FILE_EXTS) && (!file_exists($pathfile . '.webp') || (filectime($pathfile . '.webp') !== filectime($pathfile)));
- }
- function convert($file){
- $dirname = dirname($file);
- $file = str_replace(' ' , '\ ' , $file);
- $pathinfo = pathinfo($file);
- exec('cwebp ' . $file . ' -q 80 -o ' . $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] . '.webp');
- $now = time();
- touch($dirname . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] . '.webp' , $now);
- touch($dirname . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] , $now);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement