Magento Cron Job Name:captcha_delete_expired_images

    • Detailed Explanation/Purpose:

Magento allows captcha in Admin Login for security purpose. Whenever the administrator or user enters captcha, captcha image gets stored in media/captcha/admin folder. The administrator or user may logged-in several times in a day, every time captcha image gets stored in the captcha folder and occupy unnecessary space. So, need to delete those images.

How can we delete expired captcha images?
In Magento, there is a default functionality to delete captcha images by cron job. It will remove expired captcha images every 10 minutes. Captcha images are stored in a folder.

deleteExpiredImages() function is related to a cron job, it is defined in config.xml file with scheduled time.

$expire = It is Calculate expire time.

$file->getMTime() = Get last modification time of current directory Iterator item.

    
if ($file->getMTime() < $expire) 
     {
          unlink($file->getPathname());
	 //unlink function removes image
     }

If the condition is true, then related Captcha Image is unlinked(remove) from the directory.

  • This is XML of cron job (config.xml)

From this, we can make out that cron job will be executed in every 10 minutes.

Function:

public function deleteExpiredImages()
        {
           foreach (Mage::app()->getWebsites(true) as $website)
            {
               $expire = time() - Mage::helper('captcha')->getConfigNode('timeout', $website-
               >getDefaultStore())*60;
               $imageDirectory = Mage::helper('captcha')->getImgDir($website);
               foreach (new DirectoryIterator($imageDirectory) as $file)
               {
                  if ($file->isFile() && pathinfo($file->getFilename(), PATHINFO_EXTENSION)=='png') 
                   {
                      if ($file->getMTime() < $expire) 
                       {
		        //Remove expired Image
                        unlink($file->getPathname());
                       }
                    }
                 }
              }
           return $this;
        }

 
Configuration details used:



This can be edited in file app/code/core/Mage/Captcha/etc/config.xml and timeout value is 7 Second.