forked from atomtigerzoo/wordpress-attachment-metadata-fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-image-metadata-fixer.php
370 lines (303 loc) · 7.8 KB
/
wp-image-metadata-fixer.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* Wordpress attachment metadata fixer
*
* This script fixes missing image sizes in Wordpress' wp_postmeta database
* table by adding them to the meta data. It does not create new or changed
* image file, nor does it delete image files.
*
* It best used on a copy/dump of the wp_postmeta table.
*
* Please - I mean it - make backups before using this script! I am not
* responsible for any errors, failures, missing data or what else. Use this
* script at your own risk. Read the code. Know what you are doing. Never use
* it on live data! Never ever!
*
* See the code for more instructions and configurations.
*
* @author Henning Stein, www.atomtigerzoo.com
* @version 0.2.0
* @license MIT https://github.com/atomtigerzoo/wordpress-attachment-metadata-fixer/blob/master/LICENSE
*/
// Enable if you want some error reporting
#error_reporting(E_ALL);
#ini_set('display_errors', '1');
/**
* Start the script
*/
$imgfix = new FixImageSizes();
$imgfix->repair();
class FixImageSizes {
/**
* Test-Run only setting
* Leave it set as 'true' if you want to test before manipulating the
* database. When you are ready set it to 'false' [Without the quotes ;)]
*
* @var bool
*/
var $test = true;
/**
* Set the following values to login into your database
*/
/**
* Database host
* @var type
*/
var $db_host = 'localhost';
/**
* Database name
* Please don't use the live database. Create a dump and then use a separate
* database for this script!
*
* @var type
*/
var $db_database = 'image-fix';
/**
* Database user
* @var type
*/
var $db_user = 'your-database-username';
/**
* Database password
* @var type
*/
var $db_pass = 'your-database-password';
/**
* Set the sizes you want to have checked and added.
*
* For every entry in Wordpress (and the Wordpress-Backend settings) use
* the following layout - replace the capitalised words with your values:
*
* 'CUSTOM_NAME' => array(
* 'width' => WIDTH_IN_PIXELS,
* 'height' => HEIGHT_IN_PIXELS
* ),
*
* See below for some examples.
*
* @var type
*/
var $image_sizes = array(
// Standard Wordpress sizes
'thumbnail' => array( // use the sizes from your Wordpress Backend > Media
'width' => 150,
'height' => 150
),
'medium' => array( // use the sizes from your Wordpress Backend > Media
'width' => 300,
'height' => 300
),
'large' => array( // use the sizes from your Wordpress Backend > Media
'width' => 300,
'height' => 300
),
// Custom sizes
'author_photo' => array(
'width' => 80,
'height' => 80
),
'gallery' => array(
'width' => 200,
'height' => 250
),
'newsticker' => array(
'width' => 300,
'height' => 350
)
);
/**
*
* + + + + + + STOP EDITING HERE + + + + + +
*
*/
/**
* Counter for missing sizes
* @var int
*/
var $missing_sizes_counter;
/**
* Database connection holder
* @var \PDO
*/
var $db_connection;
/**
* Connect to database
*/
public function __construct() {
echo '<h2>Fix Image Fixes</h2><hr>';
if($this->test):
echo '<h3>Test run.</h3><hr>';
endif;
$this->db_connect();
}
/**
* Disconnect from database
*/
public function __destruct() {
$this->db_connection = null;
}
/**
* Connect to your database
*/
public function db_connect() {
try {
$dbh = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_database, $this->db_user, $this->db_pass);
// Disable doubled return-values
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// Enable errors
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db_connection = $dbh;
}
catch(Exception $ex) {
die('Database connection failed. [' . $ex->getMessage() . ']');
}
}
/**
* Retrieve attachements from database
*
* @param \PDO $dbh
* @return \PDO
*/
public function db_get_rows() {
try{
return $this->db_connection->query('SELECT * FROM wp_postmeta WHERE meta_key = "_wp_attachment_metadata"');
} catch (Exception $ex) {
die('Database query failed. [' . $ex->getMessage() . ']');
}
}
/**
* Simple matching for returning a mimetype for the given extension.
*
* Possible problems:
* Only checks the given string, not the file.
* Only uses png, jpg and gif.
*
* @param string $extension
* @return string
*/
public function file_extension_mimetype($extension = null) {
switch(strtolower($extension)):
case 'png':
return 'image/png';
case 'gif':
return 'image/gif';
case 'jpg':
case 'jpeg':
default:
return 'image/jpeg';
endswitch;
}
/**
* Return file details
*
* @param string $string
* @return array
*/
public function file_details($string = null) {
$details = pathinfo($string);
$details['mimetype'] = $this->file_extension_mimetype($details['extension']);
return $details;
}
/**
* Set sizes
*
* @param string $file_name
* @param string $file_mimetype
* @param string $file_extension
* @param int $width
* @param int $height
* @return array
*/
public function set_meta_size($file_name, $file_mimetype, $file_extension, $width, $height) {
return array(
"file" => $file_name . '-' . $width . 'x' . $height . '.' . $file_extension,
"width" => (int)$width,
"height" => (int)$height,
"mime-type" => $file_mimetype
);
}
/**
* Check for existing/missing sizes and add according
*
* @param array $meta_value
* @param array $filedetails
* @return array
*/
public function check_and_add_sizes($meta_value, $filedetails) {
foreach($this->image_sizes as $custom_name => $size):
if(isset($meta_value['sizes'][$custom_name])):
// Size exists. Next.
break;
endif;
$this->missing_sizes_counter++;
// Add missing sizes to array
$meta_value['sizes'][$custom_name] = $this->set_meta_size(
$filedetails['filename'],
$filedetails['mimetype'],
$filedetails['extension'],
$size['width'],
$size['height']
);
if($this->test):
echo $custom_name . ' will be added to sizes.<br>';
endif;
endforeach;
return $meta_value;
}
/**
* Update row in database
*
* @param array $meta_value
* @param bigint $update_id
*/
public function db_update_row($meta_value, $update_id) {
if($this->test):
echo '<br>Test-Run! No database query executed.';
return;
endif;
// Save to DB
$db_query = $this->db_connection->prepare("UPDATE wp_postmeta SET meta_value = :save_data WHERE meta_id = :update_id AND meta_key = '_wp_attachment_metadata'");
$db_executed_query = $db_query->execute(array(
':save_data' => serialize($meta_value),
':update_id' => $update_id
));
if(!$db_executed_query):
echo 'Update of ID ' . $update_id . ' failed. [' . $db_executed_query->getMessage() . ']';
return false;
endif;
echo 'Added and saved <span style="color: red;">' . $this->missing_sizes_counter . '</span> sizes to row-ID ' . $update_id;
return true;
}
/**
*
* @return type
*/
public function repair() {
// Get rows from DB
$db_rows = $this->db_get_rows();
// Counter for tests
$count = 1;
// Fix entries
foreach($db_rows as $row):
if($this->test && $count > 100) {
return;
}
// Unserialize 'meta_value'
$meta_value = unserialize($row['meta_value']);
// File details
$filedetails = $this->file_details($meta_value['file']);
// Missing sizes counter
$this->missing_sizes_counter = 0;
// Check and add sizes
$meta_value = $this->check_and_add_sizes($meta_value, $filedetails);
// Output
if($this->missing_sizes_counter === 0):
echo '<span style="color: #bbb;">' . $filedetails['filename'] . ' already had all sizes.</span>';
else:
$this->db_update_row($meta_value, $row['meta_id']);
endif;
$count++;
echo '<br><hr>';
endforeach;
}
}