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

    1291 comments

    • Comment Link JessedaP Friday, 18 June 2021 14:12 posted by JessedaP

      I wanted to thank you for this wonderful read!! I absolutely loved every little bit of it. I've got you saved as a favorite to look at new stuff you post…

    • Comment Link JessedaP Thursday, 17 June 2021 15:17 posted by JessedaP

      Magnificent beat ! I would like to apprentice while you amend your web site, how can i subscribe for a blog website? The account aided me a appropriate deal. I have been tiny bit acquainted of this your broadcast provided shiny clear concept

    • Comment Link JessedaP Wednesday, 16 June 2021 23:51 posted by JessedaP

      Hey there, I think your blog might be having browser compatibility issues. When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, amazing blog!

    • Comment Link JessedaP Tuesday, 15 June 2021 03:11 posted by JessedaP

      You could definitely see your expertise in the work you write. The sector hopes for more passionate writers such as you who aren't afraid to mention how they believe. At all times follow your heart.

    • Comment Link JessedaP Monday, 14 June 2021 05:35 posted by JessedaP

      fantastic submit, very informative. I wonder why the opposite experts of this sector don't notice this. You should continue your writing. I am confident, you've a great readers' base already!

    • Comment Link tadalafil 40 mg from india Friday, 11 June 2021 03:00 posted by tadalafil 40 mg from india

      40 mg tadalafil https://superactivetadalafil.com/ generic tadalafil tadalafil pills

    • Comment Link 40 mg tadalafil Friday, 11 June 2021 02:51 posted by 40 mg tadalafil

      order tadalafil https://pulmoprestadalafil.com/ tadalafil 40 mg from india tadalafil 60 mg for sale

    • Comment Link tadalafil gel Friday, 11 June 2021 02:27 posted by tadalafil gel

      tadalafil https://nextadalafil.com/ generic tadalafil 40 mg tadalafil max dose

    • Comment Link generic tadalafil 40 mg Friday, 11 June 2021 02:13 posted by generic tadalafil 40 mg

      generic tadalafil united states https://extratadalafill.com/ tadalafil 40 mg daily tadalafil 60 mg for sale

    • Comment Link tadalafil dosage Tuesday, 08 June 2021 12:51 posted by tadalafil dosage

      buy tadalafil us https://tadalafilgenc.com/ tadalafil 30 mg tadalafil online

    Leave a comment

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