Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php
  2. if(PHP_SAPI !== 'cli'){
  3. echo 'Скрипт запущен не из консоли!';
  4. return;
  5. }
  6. $UPLOAD_DIR = realpath(__DIR__ ."/../.." )."/upload/";
  7. convertInDir($UPLOAD_DIR);
  8. global $ALLOW_FILE_EXTS;
  9. $ALLOW_FILE_EXTS = ['jpg' , 'jpeg' , 'png'];
  10.  
  11.  
  12.  
  13. function convertInDir($UPLOAD_DIR){
  14. $odir = opendir($UPLOAD_DIR);
  15. global $ALLOW_FILE_EXTS;
  16. $ALLOW_FILE_EXTS = ['jpg' , 'jpeg' , 'png'];
  17.  
  18. while (($file = readdir($odir)) !== FALSE)
  19. {
  20. if ($file != '.' && $file != '..')
  21. {
  22. if(is_dir($UPLOAD_DIR . $file)){
  23. scanDirectory($UPLOAD_DIR . $file);
  24. }else if(isNeedConvert($UPLOAD_DIR . $file)){
  25. convert($UPLOAD_DIR . $file);
  26. }
  27. }
  28. }
  29.  
  30. closedir($odir);
  31. }
  32.  
  33. function scanDirectory($dir){
  34. $odir = opendir($dir);
  35. while (($file = readdir($odir)) !== FALSE)
  36. {
  37. if ($file != '.' && $file != '..')
  38. {
  39. $pathfile = $dir . '/' . $file;
  40. if(is_dir($pathfile)){
  41. scanDirectory($pathfile);
  42. }else if(isNeedConvert($pathfile)){
  43. convert($pathfile);
  44. }
  45. }
  46. }
  47.  
  48. closedir($odir);
  49. }
  50.  
  51. function isNeedConvert($pathfile){
  52. global $ALLOW_FILE_EXTS;
  53. $pathinfo = pathinfo($pathfile);
  54. return is_file($pathfile) && in_array(strtolower($pathinfo['extension']) , $ALLOW_FILE_EXTS) && (!file_exists($pathfile . '.webp') || (filectime($pathfile . '.webp') !== filectime($pathfile)));
  55. }
  56.  
  57. function convert($file){
  58. $dirname = dirname($file);
  59. $file = str_replace(' ' , '\ ' , $file);
  60. $pathinfo = pathinfo($file);
  61. exec('cwebp ' . $file . ' -q 80 -o ' . $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] . '.webp');
  62. $now = time();
  63. touch($dirname . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] . '.webp' , $now);
  64. touch($dirname . '/' . $pathinfo['filename'] . '.'.$pathinfo['extension'] , $now);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement