WordPress.org

Ready to get started?Download WordPress

Forums

How to Remove Unused Images (9 posts)

  1. ptbm
    Member
    Posted 2 years ago #

    I heard recommended plugins like DUI (by Bobhobby.com) and also Upload Janitor.

    None of them really works.

    As for the janitor, it is not working on my wp version. While on DUI, I had an error that says like this:

    Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /home/***/public_html/wp-content/plugins/dui/dui10.php on line 170
    There are total 0 unused images file

    Can anyone give me a solution?

    I just need to remove unused images because everytime I uploaded the wordpress, it multiplies the images into different sizes and it's killing my storage size.

    Thank you very much

  2. ptbm
    Member
    Posted 2 years ago #

    This is the DUI code:

    <?
    /*
    Plugin Name: DUI
    Version: 1.0
    Plugin URI: http://www.bobhobby.com/2008/02/24/delete-unused-image-files-plugin-for-wordpress/
    Author: Bob carret
    Author URI: http://www.bobhobby.com/
    Description: This plugin will delete UnUsed images file that was not referred by any post and page of wordpress
    */
    
    /*
    Copyright (c) 2008 www.bobhobby.com
    Released under the GPL license
    http://www.gnu.org/licenses/gpl.txt
    
    Disclaimer:
    	Use at your own risk. No warranty expressed or implied is provided.
    	This program is free software; you can redistribute it and/or modify
    	it under the terms of the GNU General Public License as published by
    	the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     	See the GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
    Requires : WordPress 2.3.X or newer ,PHP 5 +
    
    Usage :
    1. Download plugin and unzip
    2. Upload the plugin file to your WordPress plugins directory inside of wp-content.(i.e. www.YourDomain.com/blog/wp-content/plugin/dui/duixx.php)
    3. Activate it from the plugins menu inside of WordPress.
    4. Change Permissions monthly archives directory by Chmod to 777 with your favorite FTP Client Software(i.e. cuteFTP)
    5. Go to Admin Control panal -> Options -> DUI, select Unused images file that you want to delete then press delete button.
    6. This is IMPORTANT step after delete unused images file you must Chmod monthly archives directory to previous value(most are 755).
    */
    
    ### Load WP-Config File If This File Is Called Directly
    if (!function_exists('add_action')) {
    	require_once('../../../wp-config.php');
    }
    
    //  Check an Unused image file
    function CheckUnusedImage($ImageFileName) {
        global $wpdb,$table_prefix;
    	$result = intval($wpdb->get_var("SELECT COUNT(*) FROM ".$table_prefix."posts WHERE post_content LIKE '%/$ImageFileName%'"));
    	return $result>0;
    }
    
    // Get list image file
    function GetImageFileName($ImageDir)
    {
    	global $TotalImageCnt;
        $FileCnt = 0;
    	$UnUsedImages = array();
    
    	if (is_dir($ImageDir))
    	{  
    
             if ($DirHndl = opendir($ImageDir))
    		 {
                $files = array();
                while (($file = readdir($DirHndl)))
    			{
    			    $path =	pathinfo($file);
                    if (eregi('(jpg)|(gif)|(png)|(jpeg)', $path['extension']) || eregi('(thumbnail)',$path['basename'])) $files[] = $file;
                }
                closedir($DirHndl);
    			//print_r($files);
    
    			if (count($files))
    			{
    				foreach ($files as $fn)
    				{	 
    
    					if (!CheckUnusedImage($fn))
    					{
    						$home = get_option('home');
    						$upload_path = get_option('upload_path');
    						$UnUsedImages[$FileCnt] = $fn.'::'.$home.'/'.$upload_path.'/'.$ImageDir.'/'.$fn;
    						$FileCnt++;
    						$TotalImageCnt++;
    					}
    				}
    			}
            }
        }
      return $UnUsedImages;
    }
    
    // select directory
    function select_year_directory($year)
    {
        $Yeardirectory = array();
        $month = array('01','02','03','04','05','06','07','08','09','10','11','12');
        for ($i=0;$i<12;$i++)
    	{
    
    	   $dir =getcwd();
    
           if (is_dir($dir.'/'.$year.'/'.$month[$i]))
    	   {
    			 $Yeardirectory[$month[$i]] = GetImageFileName($year.'/'.$month[$i]);
    	   }
    	}
    	return $Yeardirectory;
    }
    
    // check image in directory
    function CheckInDir($Dir) {
    
    	if ($dh = opendir($Dir))
    	{
    		while ($el = readdir($dh)) {
    			$path = $el;
    
    			if (is_dir($path) && $el != '.' && $el != '..') {
    				$year_result[$path] = select_year_directory($path);
    				//echo $path.'<br/>';
    			}
    		}
    		closedir($dh);
    	}
    	return $year_result;
    }
    //-----------------------------------------------------------------------------------
    // add management in option menu
    function add_DUI_option_menu() {
    	if (function_exists('add_options_page')) { // is this check needed?
    		add_options_page('DUI option', 'DUI', 8, basename(__FILE__), 'DUI_options');
    	}
    }
    
    // main option -----------
    function DUI_options()
    {
    ?>
    <div class="wrap">
      	<h2>Delete Unused Images</h2>
    
    <?
    	if (array_key_exists('_submit_check', $_POST)) {
       		if (isset($_POST['im'])){
       			$im = $_POST['im'];
    
         		foreach($im as $DelectedImage) {
    		    	$fdt = explode(":",$DelectedImage);
    				chdir("../");
    				$upload_dir = get_option('upload_path');
    				chdir($upload_dir.'/'.$fdt[0]);
    				if (@unlink($fdt[1])) {
    					echo $fdt[0].'---> '.$fdt[1].' <font color="#FF0000">Deleted</font><br>';
    				}else{
    					echo $fdt[0].'---> '.$fdt[1].' <font color="#FF0000">Cannot delete file.Please change directory premissions on <font color="#000000">'.$fdt[0].'</font> to <font color="#000000">777</font></font><br>';
    				}
    				chdir("../../../");
    			}
    
    			echo "<hr>";
    		}
    	}
    
    	global $TotalImageCnt;
    	chdir("../");
    	$upload_dir = get_option('upload_path');
    	chdir($upload_dir);
    	$newdir = getcwd();
    	$TotalImageCnt=0;
    	$result = CheckInDir($newdir);
    
    	?>
    
    	<? echo "<b>There are total <font color='#FF0000'>".$TotalImageCnt."</font> unused images file</b><br><br>"; ?>
    
    	<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    	<?
    	if (is_array($result)) {
        	foreach ($result as $year => $monthArr) {
    	    	foreach ($monthArr as $month => $UnUsedImagesArr) {
    		    	if (count($UnUsedImagesArr)<>0) {
    					echo '<b><font color="#007700">'.$year.'/'.$month.'</font></b><br>';
    					foreach ($UnUsedImagesArr as $UnUsedImages) {
    				    	$IMF = explode("::",$UnUsedImages);
    						echo '<input type="checkbox" name="im[]" value="'.$year.'/'.$month.':'.$IMF[0].'"/>'.$IMF[0].	'&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$IMF[1].'" target="_blank">View</a><br>';
    					}
    					echo '<hr>';
    				} // end if
    			}
    		}
    	}
    ?>
    <input type="hidden" name="_submit_check" value="1"/>
    <input type="submit" name="submit" class="button" value="<?php _e('Delete Checked Files'); ?>" />
    </form>
    <br />
    <br />
    <br />
    <a href="http://www.BobHobby.com" target="_blank">www.BobHobby.com</a>
    </div>
    <?
    }
    
    add_action('admin_menu', 'add_DUI_option_menu');
    ?>

    Thank you

  3. ptbm
    Member
    Posted 2 years ago #

    no reply? :(

  4. qbproger
    Member
    Posted 2 years ago #

    I'm looking for something similar. I'd think the Broken Link Checker could add this capability without too much work. If it wasn't in PHP I might make a go at it.

    You didn't happen to find a solution did you?

  5. ptbm
    Member
    Posted 2 years ago #

    qbproger, I have not found the solution.

    All my image links are not broken so I dont think Broken Link Checker will work for my case.

  6. qbproger
    Member
    Posted 2 years ago #

    I was saying that since the Broken Link Checker is finding all the links already, it might not be too hard to find unused images.

  7. ptbm
    Member
    Posted 2 years ago #

    oh u mean re tweak the plugin? let me you if you can :)

  8. nicearma
    Member
    Posted 2 years ago #

    Here you can find the plugin modify, and working find DUI 1.1

  9. nicearma
    Member
    Posted 2 years ago #

    Here you have a new version of this plugin DNUI

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags