Monday, 16 November 2015 19:46

    Fast and efficient script to rebuild your Joomla K2 image cache

    Written by
    Rate this item
    (2 votes)

    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 42184 times Last modified on Tuesday, 17 November 2015 08:59

    1516 comments

    • Comment Link SlonHausy Tuesday, 24 May 2022 19:23 posted by SlonHausy

      wxbux

      tzfl6

      dei0

    • Comment Link SlonHausy Tuesday, 24 May 2022 16:53 posted by SlonHausy

      ru5ja

      124gl

      gzf9

    • Comment Link 우리카지노계열 Friday, 13 May 2022 03:54 posted by 우리카지노계열

      Thank you for the good writeup. It in truth was once
      a amusement account it. Glance advanced to far introduced agreeable
      from you! However, how could we keep up a correspondence?

    • Comment Link 우리카지노 계열 Thursday, 12 May 2022 08:05 posted by 우리카지노 계열

      You are so cool! I do not believe I've truly read a single thing
      like this before. So nice to discover another person with some unique thoughts
      on this topic. Really.. thank you for starting this up.
      This site is one thing that is required on the internet, someone with
      a bit of originality!

    • Comment Link 카지노 룰렛 Monday, 09 May 2022 10:12 posted by 카지노 룰렛

      I simply could not depart your website prior to suggesting that I actually enjoyed
      the standard info an individual provide for your visitors?
      Is going to be again continuously to investigate cross-check new posts

    • Comment Link Jmmfleet Monday, 25 April 2022 16:35 posted by Jmmfleet

      Fertomid india online pharmacy

    • Comment Link Jnnyfleet Friday, 22 April 2022 12:48 posted by Jnnyfleet

      cialis blood pressure cialis 5mg daily

    • Comment Link AnuuSkellalat Wednesday, 20 April 2022 06:20 posted by AnuuSkellalat

      cvs pharmacy in store coupons prescription drugs for anxiety

    • Comment Link AnuoSkellalat Wednesday, 13 April 2022 20:59 posted by AnuoSkellalat

      best european online pharmacy pharmacy technician online programs

    • Comment Link Knrnevins Wednesday, 13 April 2022 20:18 posted by Knrnevins

      viagra from canadian pharmacies drugstore beetle

    Leave a comment

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