12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- /**
- * Implements hook_menu().
- */
- function images_styles_gen_menu() {
-
- $items['admin/config/media/image-styles/generate'] = array(
- 'title' => 'pre-generate images styles',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('images_styles_gen_settings'),
- 'access arguments' => array('administer image styles'),
- 'type' => MENU_LOCAL_ACTION,
- );
- return $items;
- }
- function images_styles_gen_settings(){
- foreach(image_styles() as $style) {
- $styles_options[$style['name']] = $style['name'];
- }
- $form['images_styles_gen_styles'] = array(
- '#type'=>'select',
- '#options'=>$styles_options,
- '#default_value' => variable_get('images_styles_gen_styles', array()),
- '#title' => t('Images styles to pre generate'),
- '#multiple' => true,
- );
- return system_settings_form($form);
- }
- /**
- * Implements hook_cron().
- */
- function images_styles_gen_cron() {
- $result = db_query('SELECT fid, uri FROM {file_managed} WHERE filemime like :mime AND status = :status', array('mime' => 'image/%', 'status' => FILE_STATUS_PERMANENT));
- $queue = DrupalQueue::get('generate_images_styles');
- $n = 0;
- $images_styles_gen_styles = variable_get('images_styles_gen_styles', array());
- watchdog('Images Styles Generate', 'active styles variable : '. print_r($images_styles_gen_styles, true), null, WATCHDOG_INFO);
- foreach(image_styles() as $style) {
- if( in_array($style['name'], $images_styles_gen_styles) ){
- $active_styles[] = $style;
- }
- }
- watchdog('Images Styles Generate', 'active styles : '. print_r($active_styles, true), null, WATCHDOG_INFO);
- foreach ($result as $img_info) {
- foreach($active_styles as $style) {
- $derivative_uri = image_style_path($style['name'], $img_info->uri);
- if (file_exists($derivative_uri)) continue; // skip existing files
- $data = (object) array(
- 'style' => $style,
- 'img_info' => $img_info,
- 'derivative_uri' => $derivative_uri
- );
- $queue->createItem($data);
- $n ++;
- }
- }
- watchdog('Images Styles Generate', $n .' images added on cron to queue for generation.', null, WATCHDOG_INFO);
- }
- /**
- * Implements hook_cron_queue_info().
- */
- function images_styles_gen_cron_queue_info() {
- $queues['generate_images_styles'] = array(
- 'worker callback' => '_images_styles_gen_generate',
- 'time' => 30
- );
- return $queues;
- }
- function _images_styles_gen_generate($data){
- if (!file_exists($data->derivative_uri)) {
- try {
- image_style_create_derivative($data->style, $data->img_info->uri, $data->derivative_uri);
- watchdog('Images Styles Generate', 'Generated : '. $data->style['name'] .' | '.$data->img_info->uri, null, WATCHDOG_INFO);
- } catch (Exception $e) {
- watchdog('Images Styles Generate', 'Generation error on queue : '. $data->style['name'] .' | '. $data->img_info->uri.' | '.$e->getMessage(), null, WATCHDOG_WARNING);
- }
- }
- return true;
- }
|