#391258 by lesmana: Wrong description of Scale And Crop action.
[project/imagecache.git] / imagecache_actions.inc
CommitLineData
6cf0c855
DP
1<?php
2// $Id$
3
f563b784 4function 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
20function 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
808d1f74 28function theme_imagecache_resize($element) {
29 $data = $element['#value'];
30 return 'width: '. $data['width'] .', height: '. $data['height'];
31}
32
1508b2bc 33/**
e1bc2b25 34 * ImageCache Scale
1508b2bc 35 */
431c94d0 36function imagecache_scale_form($data = array()) {
9ba6a64d
DP
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;
1508b2bc
DP
45}
46
42cc9535 47function theme_imagecache_scale($element) {
70ad0a90 48 $output = theme_imagecache_resize($element) . ', upscale: ';
159f43cf
DP
49 $output .= ($element['#value']['upscale']) ? t('Yes') : t('No');
50 return $output;
42cc9535
DP
51}
52
f490ff7d 53function imagecache_scale_image(&$image, $data) {
6cf0c855
DP
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;
f490ff7d 57 if (!imageapi_image_scale($image, $data['width'], $data['height'], $data['upscale'])) {
11641bc5 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;
6cf0c855 60 }
11641bc5 61 return TRUE;
6cf0c855
DP
62}
63
1508b2bc
DP
64
65/**
e1bc2b25 66 * ImageCache Scale and Crop
1508b2bc 67 */
431c94d0 68function imagecache_scale_and_crop_form($data = array()) {
1508b2bc
DP
69 return imagecache_resize_form($data);
70}
71
42cc9535
DP
72function theme_imagecache_scale_and_crop($element) {
73 return theme_imagecache_resize($element);
74}
75
76
f490ff7d 77function imagecache_scale_and_crop_image(&$image, $data) {
f490ff7d 78 if (!imageapi_image_scale_and_crop($image, $data['width'], $data['height'])) {
11641bc5 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;
1508b2bc 81 }
11641bc5 82 return TRUE;
1508b2bc
DP
83}
84
85
86
87/**
e1bc2b25 88 * ImageCache Deprecated Scale.
1508b2bc
DP
89 * This will be removed in imagecache 2.1
90 */
431c94d0 91function imagecache_deprecated_scale_form($data = array()) {
6cf0c855
DP
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')),
b85315ac 101 '#default_value' => isset($data['fit']) ? $data['fit'] : NULL,
6cf0c855
DP
102 '#weight' => 1,
103 '#description' => $description,
104 );
105 $form['width'] = array(
106 '#type' => 'textfield',
107 '#title' => t('Width'),
b85315ac 108 '#default_value' => isset($data['width']) ? $data['width'] : '',
6cf0c855
DP
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'),
b85315ac 114 '#default_value' => isset($data['height']) ? $data['height'] : '',
6cf0c855
DP
115 '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
116 );
117 return $form;
118}
119
42cc9535
DP
120function 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
f490ff7d 126function imagecache_deprecated_scale_image(&$image, $data) {
1508b2bc 127 if ($data['fit'] == 'outside' && $data['width'] && $data['height']) {
daf33abf 128 $ratio = $image->info['width'] / $image->info['height'];
1508b2bc
DP
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'];
6cf0c855 132 }
1508b2bc
DP
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;
f490ff7d 136 if (!imageapi_image_scale($image, $data['width'], $data['height'])) {
11641bc5 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;
1508b2bc 139 }
11641bc5 140 return TRUE;
6cf0c855
DP
141}
142
1508b2bc
DP
143
144
145/**
e1bc2b25 146 * ImageCache Crop
1508b2bc 147 */
431c94d0 148function imagecache_crop_form($data = array()) {
8c361d7c
DP
149 $data += array(
150 'width' => '',
151 'height' => '',
152 'xoffset' => '',
153 'yoffset' => '',
154 );
6cf0c855
DP
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
42cc9535
DP
182function theme_imagecache_crop($element) {
183 $data = $element['#value'];
184 return 'width: '. $data['width'] .', height: '. $data['height'] .', xoffset: '. $data['xoffset'] .', yoffset: '. $data['yoffset'];
185}
186
f490ff7d
DP
187function imagecache_crop_image(&$image, $data) {
188 if (!imageapi_image_crop($image, $data['xoffset'], $data['yoffset'], $data['width'], $data['height'])) {
11641bc5 189 watchdog('imagecache', 'imagecache_crop failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
190 return FALSE;
1508b2bc 191 }
11641bc5 192 return TRUE;
1508b2bc
DP
193}
194
195
196/**
e1bc2b25 197 * ImageCache Desaturate
1508b2bc 198 */
431c94d0 199function imagecache_desaturate_form($data = array()) {
1508b2bc
DP
200 return array();
201}
202
42cc9535
DP
203function theme_imagecache_desaturate($element) {
204 return '';
205}
206
207
f490ff7d
DP
208function imagecache_desaturate_image(&$image, $data = array()) {
209 if (!imageapi_image_desaturate($image)) {
11641bc5 210 watchdog('imagecache', 'imagecache_desaturate failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
211 return FALSE;
1508b2bc 212 }
11641bc5 213 return TRUE;
6cf0c855 214}
19fefb49
DP
215
216
217
218/**
e1bc2b25 219 * ImageCache Rotate
19fefb49 220 */
431c94d0 221function imagecache_rotate_form($data = array()) {
19fefb49
DP
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',
11e599d8 236 '#default_value' => (isset($data['bgcolor'])) ? $data['bgcolor'] : '',
19fefb49 237 '#title' => t('Background color'),
11e599d8 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.'),
19fefb49
DP
239 );
240 return $form;
241}
242
243function theme_imagecache_rotate($element) {
a23a1b48 244 $output = t('degrees:') .' '. $element['#value']['degrees'] .', ';
245 $output .= t('randomize:') .' '. (($element['#value']['random']) ? t('Yes') : t('No')) .', ';
11e599d8 246 $output .= t('background:') .' '. strlen(trim($element['#value']['bgcolor'])) ? $element['#value']['bgcolor'] : t('Transparent/white');
19fefb49
DP
247 return $output;
248}
249
250function imagecache_rotate_image(&$image, $data) {
11e599d8 251 // Merge in default values.
252 $data += array(
253 'degrees' => '0',
254 'random' => FALSE,
255 'bgcolor' => '',
256 );
257
19fefb49 258 // Set sane default values.
11e599d8 259 if (strlen(trim($data['bgcolor']))) {
260 $data['bgcolor'] = hexdec(str_replace('#', '', $data['bgcolor']));
261 }
262 else {
263 $data['bgcolor'] = NULL;
264 }
19fefb49 265
11e599d8 266 if ($data['random']) {
65312e36
DP
267 $degrees = abs((float)$data['degrees']);
268 $data['degrees'] = rand(-1 * $degrees, $degrees);
19fefb49
DP
269 }
270
271 if (!imageapi_image_rotate($image, $data['degrees'], $data['bgcolor'])) {
11641bc5 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;
19fefb49 274 }
11641bc5 275 return TRUE;
19fefb49 276}
2684c3e3
DP
277
278/**
e1bc2b25 279 * ImageCache Sharpen
2684c3e3
DP
280 */
281function 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
312function theme_imagecache_sharpen($element) {
a23a1b48 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'] ;
2684c3e3
DP
317 return $output;
318}
319
320function 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'])) {
11641bc5 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;
2684c3e3 330 }
11641bc5 331 return TRUE;
e1bc2b25 332}