Advertisement
businessdad

WordPress - Regenerate thumbnails in batches (faster)

Feb 10th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. /* DISCLAIMER
  2.  * This script is deliberately simple. It doesn't check permissions, nor it
  3.  * prevents users from triggering the regeneration multiple times in a row.
  4.  * USE IT AT YOUR OWN RISK.
  5.  *
  6.  * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
  7.  */
  8.  
  9. /**
  10.  * Regenerates the thumbnails for an image. Code inspired by the Regenerate
  11.  * Thumbnails plugin (https://wordpress.org/plugins/regenerate-thumbnails/).
  12.  *
  13.  * @param int id An image ID.
  14.  * @return array An array with the result of the operation.
  15.  */
  16. function aelia_process_image($id) {
  17.   $image = get_post($id);
  18.  
  19.   if(!$image || 'attachment' != $image->post_type || 'image/' != substr($image->post_mime_type, 0, 6)) {
  20.     return array(
  21.       'message' => sprintf(__('Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails'), esc_html($id))
  22.     );
  23.   }
  24.    
  25.   $fullsizepath = get_attached_file($image->ID);
  26.  
  27.   if(false === $fullsizepath || !file_exists($fullsizepath)) {
  28.     return array(
  29.       'message' => sprintf(__('Image ID: %s. The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails'), $image->ID, esc_html($fullsizepath))
  30.     );
  31.   }
  32.    
  33.   $metadata = wp_generate_attachment_metadata($image->ID, $fullsizepath);
  34.  
  35.   if(is_wp_error($metadata)) {
  36.     return array(
  37.       'message' => sprintf('Image ID: %s.', $image->ID) . ' ' . $metadata->get_error_message(),
  38.     );
  39.   }
  40.   if(empty($metadata)) {
  41.     return array(
  42.       'message' => sprintf('Image ID: %s. Unknown failure reason', $image->ID),
  43.     );
  44.   }
  45.  
  46.   // If this fails, then it just means that nothing was changed(old value == new value)
  47.   wp_update_attachment_metadata($image->ID, $metadata);
  48.  
  49.   return array(
  50.     'message' => sprintf(__('"%1$s"(ID %2$s) was successfully resized.', 'regenerate-thumbnails'),
  51.                          esc_html(get_the_title($image->ID)),
  52.                          $image->ID));
  53. }
  54.  
  55. /**
  56.  * Regenerates the thumbnails images when the Admin section is loaded. Trigger
  57.  * the resizing by calling the following URL:
  58.  * http://yoursite.com/wp-admin?regen_thumbs=1&img_id_from=<image ID>&img_id_to=<image ID>
  59.  */
  60. add_action('admin_init', function() {
  61.   if(!empty($_GET['regen_thumbs']) && !empty($_GET['img_id_from'])) {
  62.     $image_id_from = $_GET['img_id_from'];
  63.     $image_id_to = !empty($_GET['img_id_to']) ? $_GET['img_id_to'] : $image_id_from + 40;
  64.    
  65.     set_time_limit(abs($image_id_to - $image_id_from) * 30); // 30 seconds per image should be PLENTY
  66.    
  67.     // Show all errors, to make it easier to troubleshoot issues
  68.     error_reporting(E_ALL);
  69.     ini_set('display_errors', 1);
  70.        
  71.     echo '<pre>';
  72.     $time_start = microtime(true);
  73.     echo "Started at " . date('Y-m-d H:i:s') . "\n";
  74.     echo "Resizing images from $image_id_from to $image_id_to\n";
  75.     for($image_id = $image_id_from; $image_id <= $image_id_to; $image_id++) {
  76.       $result = aelia_process_image($image_id);
  77.       if(isset($result['message'])) {
  78.         echo $result['message'] . "\n";
  79.       }
  80.       else {
  81.         echo sprintf("Invalid result received processing image ID %s", $image_id);
  82.       }      
  83.      
  84.       // Flush cache regularly, to avoid out of memory errors
  85.       if($image_id % 10 == 0) {
  86.         wp_cache_flush();
  87.       }    
  88.     }
  89.     $time_end = microtime(true);
  90.     $execution_time = ($time_end - $time_start);
  91.     echo "Finished at " . date('Y-m-d H:i:s') . "after $execution_time seconds";
  92.     echo '</pre>';
  93.   }  
  94. }, 20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement