raunak-gupta
10/11/2016 - 12:35 PM

Program to replace the uncompressed wp-content\uploads media with the compressed wp-content\compress media.

Program to replace the uncompressed wp-content\uploads media with the compressed wp-content\compress media.

<?php

define("COMPRESS_DIR", realpath(dirname(__FILE__)) . "\compress"); //folder where all the compressed images will be stored
define("UPLOADS_DIR", realpath(dirname(__FILE__)) . "\uploads"); //folder where all the uploaded media/images are stored
//reading all the compressed files name
$compress_dir_files = scandir(COMPRESS_DIR);
$compress_dir_files = array_diff($compress_dir_files, array(".", ".."));

echo nl2br('Total Number of Compressed Files are: ' . count($compress_dir_files) . "\n\n");

$imgsAfterSearch = findAndCheckFiels($compress_dir_files);

echo nl2br('Total Number of Files found are: ' . count($imgsAfterSearch) . "\n\n");


//if there are any duplicates images name then unset those name
if (count($imgsAfterSearch) > count($compress_dir_files)) {
    echo nl2br('Total Files found in upload directory names: ' . "\n\n");
    pr($imgsAfterSearch);
    
    //containg the name of all the unique images, for replacing with the orignal uncomprase images
    $unique_img_names = array_diff($imgsAfterSearch, array_diff_assoc($imgsAfterSearch, array_unique($imgsAfterSearch)));
    
    //containg the name of all the duplicates images, for user to check and manually take action.
    $need_user_action = array_diff($compress_dir_files, $unique_img_names);
    
    //replace all the unique images with uncomprased images
//    $delete = findAndMoveFiels($unique_img_names);
    echo nl2br('Total Number of Files Copyed are: ' . count($unique_img_names) . "\n\n");
}
//if there are equal number of compress and $imgsAfterSearch files then move it.
else {
    $delete = findAndMoveFiels($compress_dir_files);
}

echo nl2br('Copyed Files names: ' . "\n\n");
pr($unique_img_names);
/*
  //Deleting all successfully-copied files
  foreach ($delete as $file) {
  unlink($file);
  }
 */

//function to find the orignal file in uploads dir and then return the name of the images.
function findAndCheckFiels($compress_files_array) {

    $imgsAfterSearch = array(); //aray that will store all the match & copyed files name.

    $cdir = scandir(UPLOADS_DIR);
    //lvl 1 => retrieving year folder(s)/directory(s)
    foreach ($cdir as $key => $value) {
        if (!in_array($value, array(".", "..", 'wc-logs', 'woocommerce_uploads'))) {
            //C:\xampp\htdocs\demo_shop\wp-content\uploads + \ + 2015
            $yr_dir = UPLOADS_DIR . DIRECTORY_SEPARATOR . $value;
            if (is_dir($yr_dir)) {
                $months_dir = scandir($yr_dir);
                //lvl 2 => retrieving months folder(s)/directory(s)
                foreach ($months_dir as $key => $value) {
                    if (!in_array($value, array(".", ".."))) {
                        //C:\xampp\htdocs\demo_shop\wp-content\uploads\2015 + \ + 09
                        $fiels = $yr_dir . DIRECTORY_SEPARATOR . $value;
                        $media = scandir($fiels);
                        //lvl 3 => retrieving fiel(s)
                        foreach ($media as $medium) {
                            if (in_array($medium, $compress_files_array)) {
                                $imgsAfterSearch[] = $medium;
                            }
                        }//end <- lvl 3.
                    }
                }//end <- lvl 2.
            }
        }
    }//end <- lvl 1.
    return $imgsAfterSearch;
}

//function to find the orignal file in uploads dir and then copy compress file to uploads dir
function findAndMoveFiels($compress_files_array) {

    $delete = array(); //aray that will store all the match & copyed files name.

    $cdir = scandir(UPLOADS_DIR);
    //lvl 1 => retrieving year folder(s)/directory(s)
    foreach ($cdir as $key => $value) {
        if (!in_array($value, array(".", "..", 'wc-logs', 'woocommerce_uploads'))) {
            //C:\xampp\htdocs\demo_shop\wp-content\uploads + \ + 2015
            $yr_dir = UPLOADS_DIR . DIRECTORY_SEPARATOR . $value;

            if (is_dir($yr_dir)) {
                $months_dir = scandir($yr_dir);
                //lvl 2 => retrieving months folder(s)/directory(s)
                foreach ($months_dir as $key => $value) {
                    if (!in_array($value, array(".", ".."))) {
                        //C:\xampp\htdocs\demo_shop\wp-content\uploads\2015 + \ + 09
                        echo $fiels = $yr_dir . DIRECTORY_SEPARATOR . $value;
                        echo '<hr>';
                        $media = scandir($fiels);
                        //lvl 3 => retrieving fiel(s)
                        foreach ($media as $medium) {
                            if (in_array($medium, $compress_files_array)) {
                                echo nl2br($medium . "\n\n");
                                // If we copied this successfully, mark it for deletion
                                $source = COMPRESS_DIR . DIRECTORY_SEPARATOR . $medium;
                                $destination = $fiels . DIRECTORY_SEPARATOR . $medium;

                                if (copy($source, $destination)) {
                                    $delete[] = $source;
                                }
                            }
                        }//end <- lvl 3.
                    }
                }//end <- lvl 2.
            }
        }
    }//end <- lvl 1.
    return $delete;
}

function pr($array) {
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}

die();