images_styles_gen.module 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function images_styles_gen_menu() {
  6. $items['admin/config/media/image-styles/generate'] = array(
  7. 'title' => 'pre-generate images styles',
  8. 'page callback' => 'drupal_get_form',
  9. 'page arguments' => array('images_styles_gen_settings'),
  10. 'access arguments' => array('administer image styles'),
  11. 'type' => MENU_LOCAL_ACTION,
  12. );
  13. return $items;
  14. }
  15. function images_styles_gen_settings(){
  16. foreach(image_styles() as $style) {
  17. $styles_options[$style['name']] = $style['name'];
  18. }
  19. $form['images_styles_gen_styles'] = array(
  20. '#type'=>'select',
  21. '#options'=>$styles_options,
  22. '#default_value' => variable_get('images_styles_gen_styles', array()),
  23. '#title' => t('Images styles to pre generate'),
  24. '#multiple' => true,
  25. );
  26. return system_settings_form($form);
  27. }
  28. /**
  29. * Implements hook_cron().
  30. */
  31. function images_styles_gen_cron() {
  32. $result = db_query('SELECT fid, uri FROM {file_managed} WHERE filemime like :mime AND status = :status', array('mime' => 'image/%', 'status' => FILE_STATUS_PERMANENT));
  33. $queue = DrupalQueue::get('generate_images_styles');
  34. $n = 0;
  35. $images_styles_gen_styles = variable_get('images_styles_gen_styles', array());
  36. watchdog('Images Styles Generate', 'active styles variable : '. print_r($images_styles_gen_styles, true), null, WATCHDOG_INFO);
  37. foreach(image_styles() as $style) {
  38. if( in_array($style['name'], $images_styles_gen_styles) ){
  39. $active_styles[] = $style;
  40. }
  41. }
  42. watchdog('Images Styles Generate', 'active styles : '. print_r($active_styles, true), null, WATCHDOG_INFO);
  43. foreach ($result as $img_info) {
  44. foreach($active_styles as $style) {
  45. $derivative_uri = image_style_path($style['name'], $img_info->uri);
  46. if (file_exists($derivative_uri)) continue; // skip existing files
  47. $data = (object) array(
  48. 'style' => $style,
  49. 'img_info' => $img_info,
  50. 'derivative_uri' => $derivative_uri
  51. );
  52. $queue->createItem($data);
  53. $n ++;
  54. }
  55. }
  56. watchdog('Images Styles Generate', $n .' images added on cron to queue for generation.', null, WATCHDOG_INFO);
  57. }
  58. /**
  59. * Implements hook_cron_queue_info().
  60. */
  61. function images_styles_gen_cron_queue_info() {
  62. $queues['generate_images_styles'] = array(
  63. 'worker callback' => '_images_styles_gen_generate',
  64. 'time' => 30
  65. );
  66. return $queues;
  67. }
  68. function _images_styles_gen_generate($data){
  69. if (!file_exists($data->derivative_uri)) {
  70. try {
  71. image_style_create_derivative($data->style, $data->img_info->uri, $data->derivative_uri);
  72. watchdog('Images Styles Generate', 'Generated : '. $data->style['name'] .' | '.$data->img_info->uri, null, WATCHDOG_INFO);
  73. } catch (Exception $e) {
  74. watchdog('Images Styles Generate', 'Generation error on queue : '. $data->style['name'] .' | '. $data->img_info->uri.' | '.$e->getMessage(), null, WATCHDOG_WARNING);
  75. }
  76. }
  77. return true;
  78. }