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

    1080 comments

    • Comment Link RobertPaw Sunday, 28 February 2021 23:34 posted by RobertPaw

      cialis headaches afterwards cialis or viagra
      cialis dosage 40 mg dangerous http://cialisirt.com/ cialis generic
      generic cialis black 800mg how to get cialis samples

    • Comment Link VincentBrubs Sunday, 28 February 2021 22:56 posted by VincentBrubs

      purchasing cialis on the internet purchasing cialis on the internet
      generic cialis no doctor's prescription http://cialisirt.com/ generic cialis without prescription
      generic names for cialis and viagra fastest delivery of cialis buying online

    • Comment Link Spibeemeviendy Sunday, 28 February 2021 21:58 posted by Spibeemeviendy

      vegas casino games slots free https://playslotstime.com/ free casino games vegas world zone online casino games casino bonus

    • Comment Link LeonardWox Sunday, 28 February 2021 18:27 posted by LeonardWox

      which is better - cialis or viagra real cialis online with paypal
      cialis before and after https://edplsgeneric.com/ cialis ingredient
      how does cialis work otc cialis

    • Comment Link best CBD gummies Sunday, 28 February 2021 01:21 posted by best CBD gummies

      I used to be able to find good advice from your blog posts.

    • Comment Link RobertPaw Saturday, 27 February 2021 23:29 posted by RobertPaw

      cialis online pharmacy viagra or cialis
      what is cialis used for https://edplsgeneric.com/ cialis for daily use
      canada price on cialis canada price on cialis

    • Comment Link FjjuSmert Saturday, 27 February 2021 22:09 posted by FjjuSmert

      herbal viagra green box http://viagraonlinejc.com/ generic viagra from india

    • Comment Link Spibeemeviendy Saturday, 27 February 2021 20:28 posted by Spibeemeviendy

      free casino for fun only https://playcasinostyle.com/ heart of vegas slots zone online casino slots free slot games for fun

    • Comment Link cbd sleep Saturday, 27 February 2021 17:57 posted by cbd sleep

      It's amazing to pay a quick visit this website and reading the views of all friends on the topic of this
      piece of writing, while I am also zealous of getting knowledge.

    • Comment Link gummies for anxiety Saturday, 27 February 2021 15:00 posted by gummies for anxiety

      Howdy! Someone in my Facebook group shared
      this website with us so I came to give it a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers!
      Superb blog and fantastic style and design.

    Leave a comment

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