Monday, 16 November 2015 19:46

    Fast and efficient script to rebuild your Joomla K2 image cache

    Written by
    Rate this item
    (1 Vote)

    In this article, I write about re-building your K2 image cache. If like me, you try and test various images sizes for your perfect article style and display, you have probably encounter the challence that K2 presents when resizing images. I came across a really useful tool written by Robert Deutz and found at this address: https://github.com/rdeutz/rebuildK2imageCache

    The php script will rebuld your entire K2 image cache in just a few seconds saving precious time and frustration. Here is how to implement.

     

     
    • Copy the code below in your favourite text editor and save it as rebuild.php
    •  

      <?php

      /**

      * How to use:

      * Change the size variables as per your requirements

      * Check Variables, use a size of 0 for not processing this image size

      * @author Robert Deutz <This email address is being protected from spambots. You need JavaScript enabled to view it.>

      */

      // Variabels

      $sizeG = 800;

      $sizeL = 768;

      $sizeM = 480;

      $sizeS = 320;

      $sizeXL = 1920;

      $sizeXS = 160;

      $jpeg_quality = 75;

      /**

      * DO NOT CHANGE ANYTHING AFTER THIS LINE IF YOU DON'T KNOW WHAT YOU ARE DOING!

      */

      $uploadclassfile = dirname(__FILE__).'/../../../administrator/components/com_k2/lib/class.upload.php';

      if(!file_exists($uploadclassfile))

      {

      echo "Can't find class.upload.php! Is K2 installed? Did you copy rebuild.php to the right directory?";

      }

      define('_JEXEC',1);

      require_once ($uploadclassfile);

      // dirs

      $sourcedir = dirname(__FILE__).'/src';

      $targetdir = dirname(__FILE__).'/cache';

      $sizes = array('Generic' => $sizeG,'L' => $sizeL,'M' => $sizeM,'S' => $sizeS,'XL' => $sizeXL,'XS' => $sizeXS);

      if ($fhandle = opendir($sourcedir)) {

      while (false !== ($entry = readdir($fhandle)))

      {

      $file = $sourcedir.'/'.$entry;

      if (is_file($file))

      {

      echo '.';

      $r = buildImages($file, $targetdir, $sizes, $jpeg_quality);

      if ($r === true)

      {

      echo "File: ".$entry . " SUCCESSFUL\n";

      }

      else

      {

      echo "File: ".$entry . " FAIL\n";

      echo "Details:\n";

      foreach($sizes AS $key => $value)

      {

      $result = 'Success';

      if (array_key_exists($key, $r))

      {

      $result = 'Failed';

      }

      echo "Size $key ($value px): ".$result."\n";

      }

      }

      }

      }

      closedir($fhandle);

      }

      function buildImages($sourcefile, $targetdir, $sizes, $jpeg_quality=70)

      {

      $resultsummery = true;

      foreach($sizes AS $key => $value)

      {

      if ($value != 0)

      {

      $filename = basename($sourcefile,'.jpg');

      $targetfile = $targetdir.'/'.$filename.'_'.$key.'.jpg';

      if (buildImage($sourcefile, $targetfile, $value) !== true)

      {

      // Successful

      $resultdetails[$key] = true;

      }

      else

      {

      // Failed

      $resultsummery = false

      $resultdetails[$key] = false;

      }

      }

      }

      return $resultsummery ? true : $resultdetails;

      }

      function buildImage($sourcefile, $targetfile, $size, $jpeg_quality=70)

      {

      $handle = new Upload($sourcefile);

      $savepath = dirname($targetfile);

      $handle->image_resize = true;

      $handle->image_ratio_y = true;

      $handle->image_convert = 'jpg';

      $handle->jpeg_quality = $jpeg_quality;

      $handle->file_auto_rename = false;

      $handle->file_overwrite = true;

      $handle->file_new_name_body = basename($targetfile,'.jpg');

      $handle->image_x = (int) $size;

      return $handle->Process($savepath);

      }

       

       

       

    • Upload the file rebuild.php to "YOURSITE_ROOT/media/k2/items"
    •  

       

    • Run it and just open it on your web server typing your url like localhost/YOURSITE/media/k2/items/rebuild.php
    •  

      Depending on the size of your website and the number of images you are using, this may takes some time. However it will be much faster than letting K2 process your cache again

       

       

     

     

     

    Read 3281 times Last modified on Tuesday, 17 November 2015 08:59

    2 comments

    Leave a comment

    Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.