0a41e936718221c2d195e166132bb4087ce6eaf4
[project/imagecache.git] / imagecache_actions.inc
1 <?php
2 // $Id$
3
4 function imagecache_resize_form($action) {
5 $form['width'] = array(
6 '#type' => 'textfield',
7 '#title' => t('Width'),
8 '#default_value' => isset($action['width']) ? $action['width'] : '100%',
9 '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
10 );
11 $form['height'] = array(
12 '#type' => 'textfield',
13 '#title' => t('Height'),
14 '#default_value' => isset($action['height']) ? $action['height'] : '100%',
15 '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
16 );
17 return $form;
18 }
19
20 function imagecache_resize_image(&$image, $data) {
21 if (!imageapi_image_resize($image, $data['width'], $data['height'])) {
22 watchdog('imagecache', 'imagecache_resize_image failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
23 return FALSE;
24 }
25 return TRUE;
26 }
27
28 function theme_imagecache_resize($element) {
29 $data = $element['#value'];
30 return 'width: '. $data['width'] .', height: '. $data['height'];
31 }
32
33 /**
34 * ImageCache Scale
35 */
36 function imagecache_scale_form($data = array()) {
37 $form = imagecache_resize_form($data);
38 $form['upscale'] = array(
39 '#type' => 'checkbox',
40 '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : 0,
41 '#title' => t('Allow Upscaling'),
42 '#description' => t('Let scale make images larger than their original size'),
43 );
44 return $form;
45 }
46
47 function theme_imagecache_scale($element) {
48 $output = theme_imagecache_resize($element) . ', upscale: ';
49 $output .= ($element['#value']['upscale']) ? t('Yes') : t('No');
50 return $output;
51 }
52
53 function imagecache_scale_image(&$image, $data) {
54 // Set impossibly large values if the width and height aren't set.
55 $data['width'] = $data['width'] ? $data['width'] : 9999999;
56 $data['height'] = $data['height'] ? $data['height'] : 9999999;
57 if (!imageapi_image_scale($image, $data['width'], $data['height'], $data['upscale'])) {
58 watchdog('imagecache', 'imagecache_scale_image failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
59 return FALSE;
60 }
61 return TRUE;
62 }
63
64
65 /**
66 * ImageCache Scale and Crop
67 */
68 function imagecache_scale_and_crop_form($data = array()) {
69 return imagecache_resize_form($data);
70 }
71
72 function theme_imagecache_scale_and_crop($element) {
73 return theme_imagecache_resize($element);
74 }
75
76
77 function imagecache_scale_and_crop_image(&$image, $data) {
78 if (!imageapi_image_scale_and_crop($image, $data['width'], $data['height'])) {
79 watchdog('imagecache', 'imagecache_scale_and_crop failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
80 return FALSE;
81 }
82 return TRUE;
83 }
84
85
86
87 /**
88 * ImageCache Deprecated Scale.
89 * This will be removed in imagecache 2.1
90 */
91 function imagecache_deprecated_scale_form($data = array()) {
92 $helptext = array();
93 $helptext['inside'] = t('<strong>Inside dimensions</strong>: Final dimensions will be less than or equal to the entered width and height. Useful for ensuring a maximum height and/or width.');
94 $helptext['outside'] = t('<strong>Outside dimensions</strong>: Final dimensions will be greater than or equal to the entered width and height. Ideal for cropping the result to a square.');
95 $description = '<ul><li>'. implode('</li><li>', $helptext) .'</li><ul>';
96
97 $form['fit'] = array(
98 '#type' => 'select',
99 '#title' => t('Scale to fit'),
100 '#options' => array('inside' => t('Inside dimensions'), 'outside' => t('Outside dimensions')),
101 '#default_value' => isset($data['fit']) ? $data['fit'] : NULL,
102 '#weight' => 1,
103 '#description' => $description,
104 );
105 $form['width'] = array(
106 '#type' => 'textfield',
107 '#title' => t('Width'),
108 '#default_value' => isset($data['width']) ? $data['width'] : '',
109 '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
110 );
111 $form['height'] = array(
112 '#type' => 'textfield',
113 '#title' => t('Height'),
114 '#default_value' => isset($data['height']) ? $data['height'] : '',
115 '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
116 );
117 return $form;
118 }
119
120 function theme_imagecache_deprecated_scale($element) {
121 $data = $element['#value'];
122 $options = array('inside' => t('Inside dimensions'), 'outside' => t('Outside dimensions'));
123 return 'width: '. $data['width'] .', height: '. $data['height'] .', fit: '. $options[$data['fit']];
124 }
125
126 function imagecache_deprecated_scale_image(&$image, $data) {
127 if ($data['fit'] == 'outside' && $data['width'] && $data['height']) {
128 $ratio = $image->info['width'] / $image->info['height'];
129 $new_ratio = $data['width']/$data['height'];
130 $data['width'] = $ratio > $new_ratio ? 0 : $data['width'];
131 $data['height'] = $ratio < $new_ratio ? 0 : $data['height'];
132 }
133 // Set impossibly large values if the width and height aren't set.
134 $data['width'] = $data['width'] ? $data['width'] : 9999999;
135 $data['height'] = $data['height'] ? $data['height'] : 9999999;
136 if (!imageapi_image_scale($image, $data['width'], $data['height'])) {
137 watchdog('imagecache', 'imagecache_deprecated_scale failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
138 return FALSE;
139 }
140 return TRUE;
141 }
142
143
144
145 /**
146 * ImageCache Crop
147 */
148 function imagecache_crop_form($data = array()) {
149 $data += array(
150 'width' => '',
151 'height' => '',
152 'xoffset' => '',
153 'yoffset' => '',
154 );
155 $form['width'] = array(
156 '#type' => 'textfield',
157 '#title' => t('Width'),
158 '#default_value' => $data['width'],
159 '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
160 );
161 $form['height'] = array(
162 '#type' => 'textfield',
163 '#title' => t('Height'),
164 '#default_value' => $data['height'],
165 '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
166 );
167 $form['xoffset'] = array(
168 '#type' => 'textfield',
169 '#title' => t('X offset'),
170 '#default_value' => $data['xoffset'],
171 '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
172 );
173 $form['yoffset'] = array(
174 '#type' => 'textfield',
175 '#title' => t('Y offset'),
176 '#default_value' => $data['yoffset'],
177 '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
178 );
179 return $form;
180 }
181
182 function theme_imagecache_crop($element) {
183 $data = $element['#value'];
184 return 'width: '. $data['width'] .', height: '. $data['height'] .', xoffset: '. $data['xoffset'] .', yoffset: '. $data['yoffset'];
185 }
186
187 function imagecache_crop_image(&$image, $data) {
188 if (!imageapi_image_crop($image, $data['xoffset'], $data['yoffset'], $data['width'], $data['height'])) {
189 watchdog('imagecache', 'imagecache_crop failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
190 return FALSE;
191 }
192 return TRUE;
193 }
194
195
196 /**
197 * ImageCache Desaturate
198 */
199 function imagecache_desaturate_form($data = array()) {
200 return array();
201 }
202
203 function theme_imagecache_desaturate($element) {
204 return '';
205 }
206
207
208 function imagecache_desaturate_image(&$image, $data = array()) {
209 if (!imageapi_image_desaturate($image)) {
210 watchdog('imagecache', 'imagecache_desaturate failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
211 return FALSE;
212 }
213 return TRUE;
214 }
215
216
217
218 /**
219 * ImageCache Rotate
220 */
221 function imagecache_rotate_form($data = array()) {
222 $form['degrees'] = array(
223 '#type' => 'textfield',
224 '#default_value' => (isset($data['degrees'])) ? $data['degrees'] : 0,
225 '#title' => t('Rotation angle'),
226 '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'),
227 );
228 $form['random'] = array(
229 '#type' => 'checkbox',
230 '#default_value' => (isset($data['random'])) ? $data['random'] : 0,
231 '#title' => t('Randomize'),
232 '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
233 );
234 $form['bgcolor'] = array(
235 '#type' => 'textfield',
236 '#default_value' => (isset($data['bgcolor'])) ? $data['bgcolor'] : '',
237 '#title' => t('Background color'),
238 '#description' => t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). An empty value will cause images that support transparency to have transparent backgrounds, otherwise it will be white.'),
239 );
240 return $form;
241 }
242
243 function theme_imagecache_rotate($element) {
244 $output = t('degrees:') .' '. $element['#value']['degrees'] .', ';
245 $output .= t('randomize:') .' '. (($element['#value']['random']) ? t('Yes') : t('No')) .', ';
246 $output .= t('background:') .' '. strlen(trim($element['#value']['bgcolor'])) ? $element['#value']['bgcolor'] : t('Transparent/white');
247 return $output;
248 }
249
250 function imagecache_rotate_image(&$image, $data) {
251 // Merge in default values.
252 $data += array(
253 'degrees' => '0',
254 'random' => FALSE,
255 'bgcolor' => '',
256 );
257
258 // Set sane default values.
259 if (strlen(trim($data['bgcolor']))) {
260 $data['bgcolor'] = hexdec(str_replace('#', '', $data['bgcolor']));
261 }
262 else {
263 $data['bgcolor'] = NULL;
264 }
265
266 if ($data['random']) {
267 $degrees = abs((float)$data['degrees']);
268 $data['degrees'] = rand(-1 * $degrees, $degrees);
269 }
270
271 if (!imageapi_image_rotate($image, $data['degrees'], $data['bgcolor'])) {
272 watchdog('imagecache', 'imagecache_rotate_image failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
273 return FALSE;
274 }
275 return TRUE;
276 }
277
278 /**
279 * ImageCache Sharpen
280 */
281 function imagecache_sharpen_form($data) {
282 $form['info'] = array(
283 '#value' => t('<strong>NOTE:</strong> The sigma parameter below is currently <em>only</em> used when the Imagemagick toolkit is active.'),
284 );
285 $form['radius'] = array(
286 '#type' => 'textfield',
287 '#default_value' => (isset($data['radius'])) ? $data['radius'] : '0.5' ,
288 '#title' => t('Radius'),
289 '#description' => t('The radius of the gaussian, in pixels, not counting the center pixel. If you\'re using Imagemagick, you can set this to 0 to let Imagemagick select a suitable radius. Typically 0.5 to 1 for screen resolutions. (default 0.5)'),
290 );
291 $form['sigma'] = array(
292 '#type' => 'textfield',
293 '#default_value' => (isset($data['sigma'])) ? $data['sigma'] : '0.5' ,
294 '#title' => t('Sigma'),
295 '#description' => t('The standard deviation of the gaussian, in pixels. General rule of thumb: if radius < 1 then sigma = radius, else sigma = sqrt(radius). (default 0.5)'),
296 );
297 $form['amount'] = array(
298 '#type' => 'textfield',
299 '#default_value' => (isset($data['amount'])) ? $data['amount'] : '100' ,
300 '#title' => t('Amount'),
301 '#description' => t('The percentage of the difference between the original and the blur image that is added back into the original. Typically 50 to 200. (default 100).'),
302 );
303 $form['threshold'] = array(
304 '#type' => 'textfield',
305 '#default_value' => (isset($data['threshold'])) ? $data['threshold'] : '0.05' ,
306 '#title' => t('Threshold'),
307 '#description' => t('The threshold, as a fraction of max RGB levels, needed to apply the difference amount. Typically 0 to 0.2. (default 0.05).'),
308 );
309 return $form;
310 }
311
312 function theme_imagecache_sharpen($element) {
313 $output = t('radius:') .' '. $element['#value']['radius'] .', ';
314 $output .= t('sigma:') .' '. $element['#value']['sigma'] .', ';
315 $output .= t('amount:') .' '. $element['#value']['amount'] .', ';
316 $output .= t('threshold:') .' '. $element['#value']['threshold'] ;
317 return $output;
318 }
319
320 function imagecache_sharpen_image(&$image, $data) {
321 // Set sane default values.
322 $data['radius'] = $data['radius'] ? $data['radius'] : "0.5";
323 $data['sigma'] = $data['sigma'] ? $data['sigma'] : "0.5";
324 $data['amount'] = $data['amount'] ? $data['amount'] : "100";
325 $data['threshold'] = $data['threshold'] ? $data['threshold'] : "0.05";
326
327 if (!imageapi_image_sharpen($image, $data['radius'], $data['sigma'], $data['amount'], $data['threshold'])) {
328 watchdog('imagecache', 'imagecache_sharpen_image failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
329 return FALSE;
330 }
331 return TRUE;
332 }