Robert Deutz's cache rebuilder tool: github.com/rdeutz/rebuildK2imageCache is working pretty good and it has helped me a lot.
.
Since I have more than 1000 items having an image in my K2 web site, K2 produces 1000x6 (_Generic.jpg, _XS.jpg, _S.jpg, _M.jpg, _L.jpg, _XL.jpg for each main image of each K2 item) different resized images in total, I ended up with 6000 images and since there is a 2000-file limit for each folder (in this case, it is /media/k2/items/cache/ folder) on my hosting service, I had to divide the cache folder into 6 subfolders to keep 6000 K2 images.
So I created these, folders with ftp software on my hosting :
/media/k2/items/cache/Generic/ ( to create 1000 _Generic.jpg images under this folder)
/media/k2/items/cache/XS/ ( to create 1000 _XS.jpg images under this folder)
/media/k2/items/cache/S/ ( to create 1000 _S.jpg images under this folder)
/media/k2/items/cache/M/ ( to create 1000 _M.jpg images under this folder)
/media/k2/items/cache/L/ ( to create 1000 _L.jpg images under this folder)
/media/k2/items/cache/XL/ ( to create 1000 _XL.jpg images under this folder)
So I uploaded rebuild.php to /media/k2/items/ folder and run it by typing
www.mysite.com/media/k2/items/rebuild.php on my browser's address bar and it displayed a 113 lines code so I copy and pasted that 113 lines code to notepad++ and changed code to produce 1000 of _Generic.jpg images under /media/k2/items/cache/Generic/ from 1000 source images under /media/k2/items/src/
Here is the changed code of of 113 lines to produce it:
<?php
/**
* How to use:
*
* 1) Copy this file as rebuild.php into "JPATH_ROOT/media/k2/items"
* 2) Check Variables, use a size of 0 for not processing this image size
* 3) run it "php -f rebuild.php" (it overwrites existing files without notice)
*
* @author Robert Deutz <[email protected]>
*/
// Variabels
$sizeG = 300;
$sizeL = 600;
$sizeM = 300;
$sizeS = 200;
$sizeXL = 900;
$sizeXS = 100;
$jpeg_quality = 90;
/**
* 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/Generic/';
$sizes = array('Generic' => $sizeG);
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=90)
{
$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=90)
{
$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);
}saved it as rebuild_generic.php and uploaded it to /media/k2/items/ and ran it by typing
www.mysite.com/media/k2/items/rebuild_generic.php on my browser's address bar. If your PHP variable limits are not enough to run this code from browser's address bar, then you should login to your hosting by SSH and go to the folder yourwebsiteroot/media/k2/items/ by using unix commands "cd media" or "ls" etc. then you should run "rebuild_generic.php" by typing "php -f rebuild.php" (rebuild_generic.php in my case). I created 5 other php files from rebuild_generic.php by changing relevant lines of code in it and by saving them as rebuild_generic.php, rebuild_XS.php, rebuild_S.php etc. Then I ran each to generate 1000 XS images, 1000 S images etc. under relevant directories I mentioned above.
This solved my 2000 image limit problem of /media/k2/items/cache/ folder for now.
I also had to change some other php files like:
/administrator/components/com_k2/views/items/tmpl => default.php
line 167 => added .DS.'XL'
line 168 => added XL/
so that admin can view image of an item when he edits a previously written item because it has a new location now.
because item's old image location was: /media/k2/items/cache/
and now item's new image location is: /media/k2/items/cache/XL/
I just wanted to show you how to use rebuild.php in details when you faced a "2000 file limit" for "item image cache" folder.
I hope it helps!
P.S.: ==>>> K2 developers should find a permanent solution for big web sites including thousands of items and thousands of item images created under /media/k2/items/cache/ folder in next releases of K2 <<<==