/* DISCLAIMER
* This script is deliberately simple. It doesn't check permissions, nor it
* prevents users from triggering the regeneration multiple times in a row.
* USE IT AT YOUR OWN RISK.
*
* Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
*/
/**
* Regenerates the thumbnails for an image. Code inspired by the Regenerate
* Thumbnails plugin (https://wordpress.org/plugins/regenerate-thumbnails/).
*
* @param int id An image ID.
* @return array An array with the result of the operation.
*/
function aelia_process_image($id) {
$image = get_post($id);
if(!$image || 'attachment' != $image->post_type || 'image/' != substr($image->post_mime_type, 0, 6)) {
return array(
'message' => sprintf(__('Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails'), esc_html($id))
);
}
$fullsizepath = get_attached_file($image->ID);
if(false === $fullsizepath || !file_exists($fullsizepath)) {
return array(
'message' => sprintf(__('Image ID: %s. The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails'), $image->ID, esc_html($fullsizepath))
);
}
$metadata = wp_generate_attachment_metadata($image->ID, $fullsizepath);
if(is_wp_error($metadata)) {
return array(
'message' => sprintf('Image ID: %s.', $image->ID) . ' ' . $metadata->get_error_message(),
);
}
if(empty($metadata)) {
return array(
'message' => sprintf('Image ID: %s. Unknown failure reason', $image->ID),
);
}
// If this fails, then it just means that nothing was changed(old value == new value)
wp_update_attachment_metadata($image->ID, $metadata);
return array(
'message' => sprintf(__('"%1$s"(ID %2$s) was successfully resized.', 'regenerate-thumbnails'),
esc_html(get_the_title($image->ID)),
$image->ID));
}
/**
* Regenerates the thumbnails images when the Admin section is loaded. Trigger
* the resizing by calling the following URL:
* http://yoursite.com/wp-admin?regen_thumbs=1&img_id_from=<image ID>&img_id_to=<image ID>
*/
add_action('admin_init', function() {
if(!empty($_GET['regen_thumbs']) && !empty($_GET['img_id_from'])) {
$image_id_from = $_GET['img_id_from'];
$image_id_to = !empty($_GET['img_id_to']) ? $_GET['img_id_to'] : $image_id_from + 40;
set_time_limit(abs($image_id_to - $image_id_from) * 30); // 30 seconds per image should be PLENTY
// Show all errors, to make it easier to troubleshoot issues
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo '<pre>';
$time_start = microtime(true);
echo "Started at " . date('Y-m-d H:i:s') . "\n";
echo "Resizing images from $image_id_from to $image_id_to\n";
for($image_id = $image_id_from; $image_id <= $image_id_to; $image_id++) {
$result = aelia_process_image($image_id);
if(isset($result['message'])) {
echo $result['message'] . "\n";
}
else {
echo sprintf("Invalid result received processing image ID %s", $image_id);
}
// Flush cache regularly, to avoid out of memory errors
if($image_id % 10 == 0) {
wp_cache_flush();
}
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo "Finished at " . date('Y-m-d H:i:s') . "after $execution_time seconds";
echo '</pre>';
}
}, 20);
Comments