aboutsummaryrefslogtreecommitdiff
path: root/imagecache_auto.inc
blob: e359d92e8db19695adc6708e16dfeaab62baa086 (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
<?php
// $Id$

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

/**
 * Create ImageCache profiles for common display resolutions.
 */
function load_imagecache_presets_create() {
  $presets = module_invoke_all('load_imagecache_presets');
  foreach ($presets as $options) {
    load_imagecache_preset_create($options);
  }
}

/**
 * Create an ImageCache preset.
 *
 * @param $options
 *   Preset options.
 *
 * @see http://drupal.org/node/558664
 */
function load_imagecache_preset_create($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;
  }

  // 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 load_imagecache_preset_delete($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 load_imagecache_presets_update($old = array(), $new = NULL) {
  // Remove old presets.
  foreach ($old as $options) {
    load_imagecache_preset_delete($options);
  }

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

/**
 * Return an array of common display resolutions.
 */
function load_imagecache_presets_load_imagecache_presets() {
  return array(
    array('width' => '90%', 'height' => '90%', 'operation' => 'imagecache_scale', 'name' => '0.90x0.90'),
    array('width' => '80%', 'height' => '80%', 'operation' => 'imagecache_scale', 'name' => '0.80x0.80'),
    array('width' => '70%', 'height' => '70%', 'operation' => 'imagecache_scale', 'name' => '0.70x0.70'),
    array('width' => '60%', 'height' => '60%', 'operation' => 'imagecache_scale', 'name' => '0.60x0.60'),
    array('width' => '50%', 'height' => '50%', 'operation' => 'imagecache_scale', 'name' => '0.50x0.50'),
    array('width' => '40%', 'height' => '40%', 'operation' => 'imagecache_scale', 'name' => '0.40x0.40'),
    array('width' => '30%', 'height' => '30%', 'operation' => 'imagecache_scale', 'name' => '0.30x0.30'),
    array('width' => '20%', 'height' => '20%', 'operation' => 'imagecache_scale', 'name' => '0.20x0.20'),
    array('width' => '10%', 'height' => '10%', 'operation' => 'imagecache_scale', 'name' => '0.10x0.10'),
    array('width' => '95%', 'height' => '95%', 'operation' => 'imagecache_scale', 'name' => '0.95x0.95'),
    array('width' => '85%', 'height' => '85%', 'operation' => 'imagecache_scale', 'name' => '0.85x0.85'),
    array('width' => '75%', 'height' => '75%', 'operation' => 'imagecache_scale', 'name' => '0.75x0.75'),
    array('width' => '65%', 'height' => '65%', 'operation' => 'imagecache_scale', 'name' => '0.65x0.65'),
    array('width' => '55%', 'height' => '55%', 'operation' => 'imagecache_scale', 'name' => '0.55x0.55'),
    array('width' => '45%', 'height' => '45%', 'operation' => 'imagecache_scale', 'name' => '0.45x0.45'),
    array('width' => '35%', 'height' => '35%', 'operation' => 'imagecache_scale', 'name' => '0.35x0.35'),
    array('width' => '25%', 'height' => '25%', 'operation' => 'imagecache_scale', 'name' => '0.25x0.25'),
    array('width' => '15%', 'height' => '15%', 'operation' => 'imagecache_scale', 'name' => '0.15x0.15'),
    array('width' => '5%',  'height' => '5%',  'operation' => 'imagecache_scale', 'name' => '0.05x0.05'),
  );
}