forked from rdeutz/rebuildK2imageCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild.php
113 lines (100 loc) · 2.62 KB
/
rebuild.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?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 = 655;
$sizeM = 325;
$sizeS = 200;
$sizeXL = 900;
$sizeXS = 100;
$jpeg_quality = 70;
/**
* 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);
}