aboutsummaryrefslogtreecommitdiff
path: root/imagecache_auto.module
blob: 3bfd15c64956c61efb6121d6e60dbbacef8aaa2c (plain)
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
<?php
/**
 * @file
 * ImageCache Auto.
 *
 * Create ImageCache presets on the fly.
 */

/**
 * Implements hook_menu()
 */
function imagecache_auto_menu() {
  $items = array();

  $items['admin/build/imagecache/auto'] = array(
    'title'            => t('ImageCache Auto'),
    'description'      => t('ImageCache Auto settings.'),
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('imagecache_auto_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type'             => MENU_LOCAL_TASK,
    'file'             => 'imagecache_auto.admin.inc',
  );

  $items[file_directory_path() . '/imagecache_auto'] = array(
    'page callback'   => 'imagecache_auto',
    'access callback' => TRUE,
    'type'            => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Automatically create ImageCache presets.
 */
function imagecache_auto() {
  include_once('imagecache_auto.inc');

  $max_width  = (int) variable_get('imagecache_auto_max_width', '10000');
  $max_height = (int) variable_get('imagecache_auto_max_height', '10000');
  $args       = func_get_args();
  $options    = array(
    'width'  => (int) check_plain(array_shift($args)),
    'height' => (int) check_plain(array_shift($args)),
    'path'   => implode('/', $args),
  );

  // Validation.
  if ($options['width'] == NULL || $options['width'] < 0 || $options['width'] > $max_width) {
    drupal_not_found();
  }
  elseif ($options['height'] == NULL || $options['height'] < 0 || $options['height'] > $max_height) {
    drupal_not_found();
  }

  // Make sure that the preset exists.
  imagecache_auto_create_preset($options);

  // Redirect to the image cache image version.
  $preset = $options['width'] . 'x' . $options['height'];
  $path   = file_directory_path() . '/imagecache/'. $preset . '/' . $options['path'];
  drupal_goto($path);
}