Created
September 18, 2025 13:21
-
-
Save BhargavBhandari90/a511246107f699d0c2e2b19e2eb3bcef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Get difference of two folders and get non-optimised images | |
| function fp_scan_all_images() | |
| { | |
| $upload_dir = wp_upload_dir(); | |
| $uploads_path = $upload_dir['basedir']; | |
| $uploads_url = $upload_dir['baseurl']; | |
| $optimized_path = WP_CONTENT_DIR . '/flying-press-images'; | |
| // Step 1: Build lookup for optimized files | |
| $optimized_lookup = []; | |
| if (is_dir($optimized_path)) { | |
| $opt_iterator = new RecursiveIteratorIterator( | |
| new RecursiveDirectoryIterator($optimized_path, FilesystemIterator::SKIP_DOTS) | |
| ); | |
| foreach ($opt_iterator as $opt_file) { | |
| if ($opt_file->isFile()) { | |
| $relative = str_replace($optimized_path, '', $opt_file->getPathname()); | |
| $optimized_lookup[$relative] = true; | |
| } | |
| } | |
| } | |
| // Step 2: Find uploads not in optimized lookup | |
| $missing = []; | |
| $uploads_iterator = new RecursiveIteratorIterator( | |
| new RecursiveDirectoryIterator($uploads_path, FilesystemIterator::SKIP_DOTS) | |
| ); | |
| foreach ($uploads_iterator as $file) { | |
| if ($file->isFile()) { | |
| if (!in_array(strtolower($file->getExtension()), ['jpg', 'jpeg', 'png'], true)) { | |
| continue; | |
| } | |
| $relative = str_replace($uploads_path, '', $file->getPathname()); | |
| if (!isset($optimized_lookup[$relative])) { | |
| $missing[] = $uploads_url . $relative; // Full URL | |
| } | |
| } | |
| } | |
| error_log(print_r($missing, true)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment