Simpletest Coverage - modules/image/image.effects.inc

1 <?php
2 // $Id: image.effects.inc,v 1.2 2009/07/21 07:09:46 webchick Exp $
3
4 /**
5 * @file
6 * Functions needed to execute image effects provided by Image module.
7 */
8
9 /**
10 * Implement hook_image_effect_info().
11 */
12 function image_image_effect_info() {
13 $effects = array(
14 'image_resize' => array(
15 'label' => t('Resize'),
16 'help' => t('Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.'),
17 'effect callback' => 'image_resize_effect',
18 'form callback' => 'image_resize_form',
19 'summary theme' => 'image_resize_summary',
20 ),
21 'image_scale' => array(
22 'label' => t('Scale'),
23 'help' => t('Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.'),
24 'effect callback' => 'image_scale_effect',
25 'form callback' => 'image_scale_form',
26 'summary theme' => 'image_scale_summary',
27 ),
28 'image_scale_and_crop' => array(
29 'label' => t('Scale and crop'),
30 'help' => t('Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.'),
31 'effect callback' => 'image_scale_and_crop_effect',
32 'form callback' => 'image_resize_form',
33 'summary theme' => 'image_resize_summary',
34 ),
35 'image_crop' => array(
36 'label' => t('Crop'),
37 'help' => t('Cropping will remove portions of an image to make it the specified dimensions.'),
38 'effect callback' => 'image_crop_effect',
39 'form callback' => 'image_crop_form',
40 'summary theme' => 'image_crop_summary',
41 ),
42 'image_desaturate' => array(
43 'label' => t('Desaturate'),
44 'help' => t('Desaturate converts an image to grayscale.'),
45 'effect callback' => 'image_desaturate_effect',
46 ),
47 'image_rotate' => array(
48 'label' => t('Rotate'),
49 'help' => t('Rotating an image may cause the dimensions of an image to increase to fit the diagonal.'),
50 'effect callback' => 'image_rotate_effect',
51 'form callback' => 'image_rotate_form',
52 'summary theme' => 'image_rotate_summary',
53 ),
54 );
55
56 return $effects;
57 }
58
59 /**
60 * Image effect callback; Resize an image resource.
61 *
62 * @param $image
63 * An image object returned by image_load().
64 * @param $data
65 * An array of attributes to use when performing the resize effect with the
66 * following items:
67 * - "width": An integer representing the desired width in pixels.
68 * - "height": An integer representing the desired height in pixels.
69 * @return
70 * TRUE on success. FALSE on failure to resize image.
71 * @see image_resize()
72 */
73 function image_resize_effect(&$image, $data) {
74 if (!image_resize($image, $data['width'], $data['height'])) {
75 watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
76 return FALSE;
77 }
78 return TRUE;
79 }
80
81 /**
82 * Image effect callback; Scale an image resource.
83 *
84 * @param $image
85 * An image object returned by image_load().
86 * @param $data
87 * An array of attributes to use when performing the scale effect with the
88 * following items:
89 * - "width": An integer representing the desired width in pixels.
90 * - "height": An integer representing the desired height in pixels.
91 * - "upscale": A Boolean indicating that the image should be upscalled if
92 * the dimensions are larger than the original image.
93 * @return
94 * TRUE on success. FALSE on failure to scale image.
95 * @see image_scale()
96 */
97 function image_scale_effect(&$image, $data) {
98 // Set sane default values.
99 $data += array(
100 'upscale' => FALSE,
101 );
102
103 // Set impossibly large values if the width and height aren't set.
104 $data['width'] = empty($data['width']) ? PHP_INT_MAX : $data['width'];
105 $data['height'] = empty($data['height']) ? PHP_INT_MAX : $data['height'];
106
107 if (!image_scale($image, $data['width'], $data['height'], $data['upscale'])) {
108 watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
109 return FALSE;
110 }
111 return TRUE;
112 }
113
114 /**
115 * Image effect callback; Crop an image resource.
116 *
117 * @param $image
118 * An image object returned by image_load().
119 * @param $data
120 * An array of attributes to use when performing the crop effect with the
121 * following items:
122 * - "width": An integer representing the desired width in pixels.
123 * - "height": An integer representing the desired height in pixels.
124 * - "anchor": A string describing where the crop should originate in the form
125 * of "XOFFSET-YOFFSET". XOFFSET is either a number of pixels or
126 * "left", "center", "right" and YOFFSET is either a number of pixels or
127 * "top", "center", "bottom".
128 * @return
129 * TRUE on success. FALSE on failure to crop image.
130 * @see image_crop()
131 */
132 function image_crop_effect(&$image, $data) {
133 // Set sane default values.
134 $data += array(
135 'anchor' => 'center-center',
136 );
137
138 list($x, $y) = explode('-', $data['anchor']);
139 $x = image_filter_keyword($x, $image->info['width'], $data['width']);
140 $y = image_filter_keyword($y, $image->info['height'], $data['height']);
141 if (!image_crop($image, $x, $y, $data['width'], $data['height'])) {
142 watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
143 return FALSE;
144 }
145 return TRUE;
146 }
147
148 /**
149 * Image effect callback; Scale and crop an image resource.
150 *
151 * @param $image
152 * An image object returned by image_load().
153 * @param $data
154 * An array of attributes to use when performing the scale and crop effect
155 * with the following items:
156 * - "width": An integer representing the desired width in pixels.
157 * - "height": An integer representing the desired height in pixels.
158 * @return
159 * TRUE on success. FALSE on failure to scale and crop image.
160 * @see image_scale_and_crop()
161 */
162 function image_scale_and_crop_effect(&$image, $data) {
163 if (!image_scale_and_crop($image, $data['width'], $data['height'])) {
164 watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
165 return FALSE;
166 }
167 return TRUE;
168 }
169
170 /**
171 * Image effect callback; Desaturate (grayscale) an image resource.
172 *
173 * @param $image
174 * An image object returned by image_load().
175 * @param $data
176 * An array of attributes to use when performing the desaturate effect.
177 * @return
178 * TRUE on success. FALSE on failure to desaturate image.
179 * @see image_desaturate()
180 */
181 function image_desaturate_effect(&$image, $data) {
182 if (!image_desaturate($image)) {
183 watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
184 return FALSE;
185 }
186 return TRUE;
187 }
188
189 /**
190 * Image effect callback; Rotate an image resource.
191 *
192 * @param $image
193 * An image object returned by image_load().
194 * @param $data
195 * An array of attributes to use when performing the rotate effect containing
196 * the following items:
197 * - "degrees": The number of (clockwise) degrees to rotate the image.
198 * - "random": A Boolean indicating that a random rotation angle should be
199 * used for this image. The angle specified in "degrees" is used as a
200 * positive and negative maximum.
201 * - "bgcolor": The background color to use for exposed areas of the image.
202 * Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave
203 * blank for transparency on image types that support it.
204 * @return
205 * TRUE on success. FALSE on failure to rotate image.
206 * @see image_rotate().
207 */
208 function image_rotate_effect(&$image, $data) {
209 // Set sane default values.
210 $data += array(
211 'degrees' => 0,
212 'bgcolor' => NULL,
213 'random' => FALSE,
214 );
215
216 // Convert short #FFF syntax to full #FFFFFF syntax.
217 if (strlen($data['bgcolor']) == 4) {
218 $c = $data['bgcolor'];
219 $data['bgcolor'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];
220 }
221
222 // Convert #FFFFFF syntax to hexadecimal colors.
223 if ($data['bgcolor'] != '') {
224 $data['bgcolor'] = hexdec(str_replace('#', '0x', $data['bgcolor']));
225 }
226 else {
227 $data['bgcolor'] = NULL;
228 }
229
230 if (!empty($data['random'])) {
231 $degrees = abs((float)$data['degrees']);
232 $data['degrees'] = rand(-1 * $degrees, $degrees);
233 }
234
235 if (!image_rotate($image, $data['degrees'], $data['bgcolor'])) {
236 watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['height'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
237 return FALSE;
238 }
239 return TRUE;
240 }
241

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.