aboutsummaryrefslogtreecommitdiff
path: root/imagecache_auto.inc
blob: 788b195afdb5bd54d5833b40c40b67a089b1d648 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

/**
 * @file
 * ImageCache functions.
 */

/**
 * Create ImageCache profiles for common display resolutions.
 */
function imagecache_auto_create_presets() {
  $presets = imagecache_auto_presets();
  foreach ($presets as $options) {
    imagecache_auto_create_preset($options);
  }
}

/**
 * Create an ImageCache preset.
 *
 * @param $options
 *   Preset options.
 *
 * @see http://drupal.org/node/558664
 */
function imagecache_auto_create_preset($options) {
  // Set preset name
  if (isset($options['name'])) {
    $name = $options['name'];
  }
  else {
    $name = $options['width'] .'x'. $options['height'];
  }

  // Check if preset already exists.
  $preset = imagecache_preset_by_name($name);
  if (!empty($preset)) {
    return;
  }

  // Check if maximum number of presets was reached.
  $max_presets = (int) variable_get('imagecache_auto_max_presets', '250');
  if (imagecache_auto_count_presets() + 1 > $max_presets) {
    $message = 'Maximum number of imagecache presets reached. Please consider increasing the max number of presets.';
    watchdog('imagecache_auto', $message, array(), WATCHDOG_ERROR);
    return;
  }

  // Create a preset.
  $preset = imagecache_preset_save(array('presetname' => $name));

  // Set operation name.
  if (isset($options['action'])) {
    $operation = $options['action'];
  }
  else {
    $operation = 'imagecache_scale_and_crop';
  }

  // Add preset actions.
  $action           = new stdClass();
  $action->presetid = $preset['presetid'];
  $action->module   = 'imagecache';
  $action->action   = $operation;
  $action->data     = array('width' => $options['width'], 'height' => $options['height']);
  drupal_write_record('imagecache_action', $action);
}

/**
 * Delete an ImageCache preset.
 *
 * @param $options
 *   Preset options.
 */
function imagecache_auto_delete_preset($options) {
  // Set preset name.
  if (isset($options['name'])) {
    $name = $options['name'];
  }
  else {
    $name = $options['width'] .'x'. $options['height'];
  }    

  // Check if preset exists.
  $preset = imagecache_preset_by_name($name);
  if (!empty($preset)) {
    imagecache_preset_delete(imagecache_preset_by_name($name));
  }
}

/**
 * Update imagecache presets.
 *
 * @param $old
 *   Old presets to be deleted.
 *
 * @param $new
 *   New presets to be created.
 */
function imagecache_auto_presets_update($old = array(), $new = NULL) {
  // Remove old presets.
  foreach ($old as $options) {
    imagecache_auto_delete_preset($options);
  }

  if ($new == NULL) {
    imagecache_auto_create_presets();
  }
  else {
    // Create new presets.
    foreach ($new as $options) {
      imagecache_auto_create_preset($options);
    }
  }
}

/**
 * Return an array of presets.
 *
 * @see http://en.wikipedia.org/wiki/List_of_common_resolutions
 */
function imagecache_auto_presets() {
  $presets = array();
  foreach (module_implements('imagecache_auto_presets') as $module) {
    $presets = array_merge($presets, module_invoke($module, 'imagecache_auto_presets'));
  }

  return $presets;
}

/**
 * Count number of existing imagecache presets.
 *
 * @return
 *   Number of existing presets.
 */
function imagecache_auto_count_presets() {
  $query  = 'SELECT COUNT(presetid) AS count FROM imagecache_preset';
  $result = db_query($query);
  $count  = db_fetch_object($result);
  return $count->count;
}