forked from rdeutz/rebuildK2imageCache
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rebuild.php
167 lines (146 loc) · 5.12 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* How to use:
*
* 1) Place this file into the root folder of your Joomla site (where your configuration.php file exists)
* 2) Adjust the variables section below; use a size of 0 if you DO NOT want to process a specific image size
* 3) Execute via terminal with "php -f rebuild.php" (it overwrites existing files without notice!)
*
* @version 1.0
* @author Robert Deutz <[email protected]>
*
* @version 1.2
* @author JoomlaWorks Ltd.
* @date June 11th, 2019
*
* === CHANGELOG ===
* v1.2:
* - IMPORTANT change: the script must now be executed from your Joomla site's root folder (where your configuration.php file exists).
* - Conversion now works from the newest to the oldest source file. So your most recent images will be converted first (makes sense to do so).
* - Add range option so you can work in batches. Set the $from & $to variables to the range you want converted.
* - Added progress counter next to each source file name.
*
* v1.1:
* - Updated codebase to work with latest K2. The script will be maintained by JoomlaWorks from now on - thank you Robert!
*
* === TO DO ===
* - Add option to resize source image
* - Implement as Joomla content or system plugin
*
* (end)
*/
// New images sizes (width in pixels)
$sizeXS = 80;
$sizeS = 160;
$sizeM = 320;
$sizeL = 640;
$sizeXL = 900;
$sizeG = 280;
$jpeg_quality = 80;
// Set conversion range (set to 0 to disable - default action)
$from = 0;
$to = 0;
/**
* DO NOT CHANGE ANYTHING AFTER THIS LINE IF YOU DON'T KNOW WHAT YOU ARE DOING!
*/
// Load class.upload.php
$uploadclassfile = '';
$oldUploadClassLocation = dirname(__FILE__).'/administrator/components/com_k2/lib/class.upload.php';
$newUploadClassLocation = dirname(__FILE__).'/media/k2/assets/vendors/verot/class.upload.php/src/class.upload.php';
if (file_exists($oldUploadClassLocation) && is_readable($oldUploadClassLocation)) {
$uploadclassfile = $oldUploadClassLocation;
}
if (file_exists($newUploadClassLocation) && is_readable($newUploadClassLocation)) {
$uploadclassfile = $newUploadClassLocation;
}
if (!$uploadclassfile) {
echo "Can't find class.upload.php! Is K2 installed? Did you copy rebuild.php to the root folder of your Joomla site?";
exit;
}
define('_JEXEC', 1);
require_once($uploadclassfile);
// Helper functions
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);
}
function buildImages($sourcefile, $targetdir, $sizes, $jpeg_quality=80)
{
$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;
}
// Set directories and image sizes
$sourcedir = dirname(__FILE__).'/media/k2/items/src';
$targetdir = dirname(__FILE__).'/media/k2/items/cache';
$sizes = array(
'XS' => $sizeXS,
'S' => $sizeS,
'M' => $sizeM,
'L' => $sizeL,
'XL' => $sizeXL,
'Generic' => $sizeG
);
// Count total images
$all = count(glob($sourcedir."/*.jpg"));
// --- Convert the images ---
$filesByDateModified = array();
$count = 0;
if ($fhandle = opendir($sourcedir)) {
while (false !== ($entry = readdir($fhandle))) {
$file = $sourcedir.'/'.$entry;
if (is_file($file) && $entry != "." && $entry != "..") {
$filesByDateModified[filemtime($file)] = $file;
}
}
closedir($fhandle);
// Reverse sort source image files by date modified (to begin converting the newest ones)
krsort($filesByDateModified);
foreach ($filesByDateModified as $timestamp => $file) {
$count++;
if ($from > 0 && $count < $from) {
continue;
}
if ($to > 0 && $count > $to) {
break;
}
$entry = str_replace($sourcedir.'/', '', $file);
$r = buildImages($file, $targetdir, $sizes, $jpeg_quality);
if ($r === true) {
echo "Source file {$count}/{$all}: ".$entry . " [OK]\n";
} else {
echo "Source file {$count}/{$all}: ".$entry . " [FAILED]\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";
}
}
}
}