Simpletest Coverage - modules/image/image.module

1 <?php
2 // $Id: image.module,v 1.6 2009/07/21 07:09:46 webchick Exp $
3
4 /**
5 * @file
6 * Exposes global functionality for creating image styles.
7 */
8
9 /**
10 * Implement of hook_help().
11 */
12 function image_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#image':
15 $naming_approaches = array();
16 $naming_approaches[] = t('Based on where it will be used: !name', array('!name' => '<code>profile-picture</code>'));
17 $naming_approaches[] = t('Describing its appearance: !name', array('!name' => '<code>square-85x85</code>'));
18 $output = '';
19 $output .= '<p>' . t('The Image module provides functionality for displaying images on your site though <a href="!url">image styles</a>.', array('!url' => url('admin/settings/image-styles'))) .'</p>';
20 $output .= '<h3>' . t('Image styles') . '</h3>';
21 $output .= '<p>' . t('Image <em>styles</em> allow your site to output an image in several different ways without affecting the original image. Any created images will automatically be refreshed if any changes are made to the image style.') .'</p>';
22 $output .= '<p>' . t('Every image style must have a name, which will be used in the URL of generated images. There are two common approaches to naming image styles:') . '</p>';
23 $output .= theme('item_list', $naming_approaches);
24 $output .= '<p>' . t('Both approaches are common and which you choose depends on how you use the image style.') . '</p>';
25 $output .= '<p>' . t('After creating an image style, <em>effects</em> may be added to the style. Image module comes with some basic effects such as <em>crop</em>, <em>scale</em>, <em>desaturate</em>, and <em>rotate</em>. In addition to the effects included with Image, other modules may provide additional effects. Multiple effects may be combined together, such as using the <em>crop and scale</em> effect and the <em>desaturate</em> effect, you could create square, grayscale thumbnails.');
26 return $output;
27 case 'admin/settings/image-styles':
28 return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>';
29 case 'admin/settings/image-styles/edit/%/add/%':
30 case 'admin/settings/image-styles/edit/%/effects/%':
31 $effect = ($arg[5] == 'add') ? image_effect_definition_load($arg[6]) : image_effect_load($arg[6]);
32 return isset($effect['help']) ? ('<p>' . $effect['help'] . '</p>') : NULL;
33 }
34 }
35
36 /**
37 * Implement hook_menu().
38 */
39 function image_menu() {
40 $items = array();
41
42 $items['image/generate/%image_style'] = array(
43 'title' => 'Generate image style',
44 'page callback' => 'image_style_generate',
45 'page arguments' => array(2),
46 'access callback' => TRUE,
47 'type' => MENU_CALLBACK,
48 );
49 $items['admin/settings/image-styles'] = array(
50 'title' => 'Image styles',
51 'description' => 'Configure styles that can be used for resizing or adjusting images on display.',
52 'page callback' => 'image_style_list',
53 'access arguments' => array('administer image styles'),
54 );
55 $items['admin/settings/image-styles/list'] = array(
56 'title' => 'List',
57 'description' => 'List the current image styles on the site.',
58 'page callback' => 'image_style_list',
59 'access arguments' => array('administer image styles'),
60 'type' => MENU_DEFAULT_LOCAL_TASK,
61 'weight' => 1,
62 );
63 $items['admin/settings/image-styles/add'] = array(
64 'title' => 'Add style',
65 'description' => 'Add a new image style.',
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('image_style_add_form'),
68 'access arguments' => array('administer image styles'),
69 'type' => MENU_LOCAL_TASK,
70 'weight' => 2,
71 );
72 $items['admin/settings/image-styles/edit/%image_style'] = array(
73 'title' => 'Edit style',
74 'title callback' => 'image_style_title',
75 'title arguments' => array('!name', 4),
76 'description' => 'Configure an image style.',
77 'page callback' => 'drupal_get_form',
78 'page arguments' => array('image_style_form', 4),
79 'access arguments' => array('administer image styles'),
80 'type' => MENU_CALLBACK,
81 );
82 $items['admin/settings/image-styles/delete/%image_style'] = array(
83 'title' => 'Delete style',
84 'title callback' => 'image_style_title',
85 'title arguments' => array('Delete !name', 4),
86 'description' => 'Delete an image style.',
87 'page callback' => 'drupal_get_form',
88 'page arguments' => array('image_style_delete_form', 4, TRUE),
89 'access arguments' => array('administer image styles'),
90 'type' => MENU_CALLBACK,
91 );
92 $items['admin/settings/image-styles/edit/%image_style/effects/%image_effect'] = array(
93 'title' => 'Edit image effect',
94 'title callback' => 'image_effect_title',
95 'title arguments' => array('!label effect', 6),
96 'description' => 'Edit an exiting effect within a style.',
97 'page callback' => 'drupal_get_form',
98 'page arguments' => array('image_effect_form', 4, 6),
99 'access arguments' => array('administer image styles'),
100 'type' => MENU_CALLBACK,
101 );
102 $items['admin/settings/image-styles/edit/%image_style/effects/%image_effect/delete'] = array(
103 'title' => 'Delete image effect',
104 'title callback' => 'image_effect_title',
105 'title arguments' => array('Delete !label', 6),
106 'description' => 'Delete an exiting effect from a style.',
107 'page callback' => 'drupal_get_form',
108 'page arguments' => array('image_effect_delete_form', 4, 6),
109 'access arguments' => array('administer image styles'),
110 'type' => MENU_CALLBACK,
111 );
112 $items['admin/settings/image-styles/edit/%image_style/add/%image_effect_definition'] = array(
113 'title' => 'Add image effect',
114 'title callback' => 'image_effect_title',
115 'title arguments' => array('Add !label effect', 6),
116 'description' => 'Add a new effect to a style.',
117 'page callback' => 'drupal_get_form',
118 'page arguments' => array('image_effect_form', 4, 6),
119 'access arguments' => array('administer image styles'),
120 'type' => MENU_CALLBACK,
121 );
122
123 return $items;
124 }
125
126 /**
127 * Implement hook_theme().
128 */
129 function image_theme() {
130 return array(
131 'image_style' => array(
132 'arguments' => array(
133 'style' => NULL,
134 'path' => NULL,
135 'alt' => '',
136 'title' => '',
137 'attributes' => array(),
138 'getsize' => TRUE,
139 ),
140 ),
141 'image_style_list' => array(
142 'arguments' => array('styles' => NULL),
143 ),
144 'image_style_effects' => array(
145 'arguments' => array('form' => NULL),
146 ),
147 'image_style_preview' => array(
148 'arguments' => array('style' => NULL),
149 ),
150 'image_anchor' => array(
151 'arguments' => array('element' => NULL),
152 ),
153 'image_resize_summary' => array(
154 'arguments' => array('data' => NULL),
155 ),
156 'image_scale_summary' => array(
157 'arguments' => array('data' => NULL),
158 ),
159 'image_crop_summary' => array(
160 'arguments' => array('data' => NULL),
161 ),
162 'image_rotate_summary' => array(
163 'arguments' => array('data' => NULL),
164 ),
165 );
166 }
167
168 /**
169 * Implement hook_permission().
170 */
171 function image_permission() {
172 return array(
173 'administer image styles' => array(
174 'title' => t('Administer image styles'),
175 'description' => t('Create and modify styles for generating image modifications such as thumbnails.'),
176 ),
177 );
178 }
179
180 /**
181 * Implement hook_flush_caches().
182 */
183 function image_flush_caches() {
184 return array('cache_image');
185 }
186
187 /**
188 * Implement hook_file_download().
189 *
190 * Control the access to files underneath the styles directory.
191 */
192 function image_file_download($filepath) {
193 if (strpos($filepath, 'styles/') === 0) {
194 $args = explode('/', $filepath);
195 // Discard the first part of the path (styles).
196 array_shift($args);
197 // Get the style name from the second part.
198 $style_name = array_shift($args);
199 // Then the remaining parts are the path to the image.
200 $original_path = implode('/', $args);
201
202 // Check that the file exists and is an image.
203 if ($info = image_get_info(file_create_path($filepath))) {
204 // Check the permissions of the original to grant access to this image.
205 $headers = module_invoke_all('file_download', $original_path);
206 if (!in_array(-1, $headers)) {
207 return array(
208 'Content-Type' => $info['mime_type'],
209 'Content-Length' => $info['file_size'],
210 );
211 }
212 }
213 return -1;
214 }
215 }
216
217 /**
218 * Implement hook_file_move().
219 */
220 function image_file_move($file, $source) {
221 // Delete any image derivatives at the original image path.
222 image_path_flush($file->filepath);
223 }
224
225 /**
226 * Implement hook_file_delete().
227 */
228 function image_file_delete($file) {
229 // Delete any image derivatives of this image.
230 image_path_flush($file->filepath);
231 }
232
233 /**
234 * Clear cached versions of a specific file in all styles.
235 *
236 * @param $path
237 * The Drupal file path to the original image.
238 */
239 function image_path_flush($path) {
240 $path = file_directory_strip($path);
241 $styles = image_styles();
242 foreach ($styles as $style) {
243 if ($path = file_create_path('styles/' . $style['name'] . '/' . $path)) {
244 file_unmanaged_delete($path);
245 }
246 }
247 }
248
249 /**
250 * Get an array of all styles and their settings.
251 *
252 * @return
253 * An array of styles keyed by the image style ID (isid).
254 * @see image_style_load()
255 */
256 function image_styles() {
257 $styles = &drupal_static(__FUNCTION__);
258
259 // Grab from cache or build the array.
260 if (!isset($styles)) {
261 if ($cache = cache_get('image_styles', 'cache')) {
262 $styles = $cache->data;
263 }
264 else {
265 $styles = array();
266 $result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
267 ->fields('image_styles')
268 ->orderBy('name')
269 ->execute();
270 foreach ($result as $style) {
271 $styles[$style['name']] = $style;
272 $styles[$style['name']]['effects'] = image_style_effects($style);
273 }
274
275 cache_set('image_styles', $styles);
276 }
277 }
278
279 return $styles;
280 }
281
282 /**
283 * Load a style by style name or ID. May be used as a loader for menu items.
284 *
285 * @param $name
286 * The name of the style.
287 * @param $isid
288 * Optional. The numeric id of a style if the name is not known.
289 * @return
290 * An image style array containing the following keys:
291 * - "isid": The unique image style ID.
292 * - "name": The unique image style name.
293 * - "effects": An array of image effects within this image style.
294 * If the image style name or ID is not valid, an empty array is returned.
295 * @see image_effect_load()
296 */
297 function image_style_load($name = NULL, $isid = NULL) {
298 $styles = image_styles();
299
300 // If retrieving by name.
301 if (isset($name) && isset($styles[$name])) {
302 return $styles[$name];
303 }
304
305 // If retrieving by image style id.
306 if (isset($isid)) {
307 foreach ($styles as $name => $style) {
308 if ($style['isid'] == $isid) {
309 return $style;
310 }
311 }
312 }
313
314 // Otherwise the style was not found.
315 return FALSE;
316 }
317
318 /**
319 * Save an image style.
320 *
321 * @param style
322 * An image style array.
323 * @return
324 * An image style array. In the case of a new style, 'isid' will be populated.
325 */
326 function image_style_save($style) {
327 if (isset($style['isid']) && is_numeric($style['isid'])) {
328 // Load the existing style to make sure we account for renamed styles.
329 $old_style = image_style_load(NULL, $style['isid']);
330 image_style_flush($old_style);
331 drupal_write_record('image_styles', $style, 'isid');
332 if ($old_style['name'] != $style['name']) {
333 $style['old_name'] = $old_style['name'];
334 }
335 }
336 else {
337 drupal_write_record('image_styles', $style);
338 $style['is_new'] = TRUE;
339 }
340
341 // Let other modules update as necessary on save.
342 module_invoke_all('image_style_save', $style);
343
344 // Clear all caches and flush.
345 image_style_flush($style);
346
347 return $style;
348 }
349
350 /**
351 * Delete an image style.
352 *
353 * @param $style
354 * An image style array.
355 * @param $replacement_style_name
356 * (optional) When deleting a style, specify a replacement style name so
357 * that existing settings (if any) may be converted to a new style.
358 * @return
359 * TRUE on success.
360 */
361 function image_style_delete($style, $replacement_style_name = '') {
362 image_style_flush($style);
363
364 db_delete('image_effects')->condition('isid', $style['isid'])->execute();
365 db_delete('image_styles')->condition('isid', $style['isid'])->execute();
366
367 // Let other modules update as necessary on save.
368 $style['old_name'] = $style['name'];
369 $style['name'] = $replacement_style_name;
370 module_invoke_all('image_style_delete', $style);
371
372 return TRUE;
373 }
374
375 /**
376 * Load all the effects for an image style.
377 *
378 * @param $style
379 * An image style array.
380 * @return
381 * An array of image effects associated with specified image style in the
382 * format array('isid' => array()), or an empty array if the specified style
383 * has no effects.
384 */
385 function image_style_effects($style) {
386 $effects = image_effects();
387 $style_effects = array();
388 foreach ($effects as $effect) {
389 if ($style['isid'] == $effect['isid']) {
390 $style_effects[$effect['ieid']] = $effect;
391 }
392 }
393
394 return $style_effects;
395 }
396
397 /**
398 * Get an array of image styles suitable for using as select list options.
399 *
400 * @param $include_empty
401 * If TRUE a <none> option will be inserted in the options array.
402 * @return
403 * Array of image styles both key and value are set to style name.
404 */
405 function image_style_options($include_empty = TRUE) {
406 $styles = image_styles();
407 $options = array();
408 if ($include_empty && !empty($styles)) {
409 $options[''] = t('<none>');
410 }
411 $options = array_merge($options, drupal_map_assoc(array_keys($styles)));
412 if (empty($options)) {
413 $options[''] = t('No defined styles');
414 }
415 return $options;
416 }
417
418 /**
419 * Menu callback; Given a style and image path, generate a derivative.
420 *
421 * This menu callback is always served after checking a token to prevent
422 * generation of unnecessary images. After generating an image transfer it to
423 * the requesting agent via file_transfer().
424 */
425 function image_style_generate() {
426 $args = func_get_args();
427 $style = array_shift($args);
428 $style_name = $style['name'];
429 $path = implode('/', $args);
430
431 $source = file_create_path($path);
432 $path_md5 = md5($path);
433 $destination = image_style_path($style['name'], $path);
434
435 // Check that it's a defined style and that access was granted by
436 // image_style_url().
437 if (!$style || !cache_get('access:' . $style_name . ':' . $path_md5, 'cache_image')) {
438 drupal_access_denied();
439 exit();
440 }
441
442 // Don't start generating the image if it is already in progress.
443 $cid = 'generate:' . $style_name . ':' . $path_md5;
444 if (cache_get($cid, 'cache_image')) {
445 // Tell client to retry again in 3 seconds. Currently no browsers are known
446 // to support Retry-After.
447 drupal_set_header('503 Service Unavailable');
448 drupal_set_header('Retry-After', 3);
449 print t('Image generation in progress, please try again shortly.');
450 exit();
451 }
452
453 // If the image has already been generated then send it.
454 if ($image = image_load($destination)) {
455 file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
456 }
457
458 // Set a cache entry designating this image as being in-process.
459 cache_set($cid, $destination, 'cache_image');
460
461 // Try to generate the image.
462 if (image_style_create_derivative($style, $source, $destination)) {
463 $image = image_load($destination);
464 cache_clear_all($cid, 'cache_image');
465 file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
466 }
467 else {
468 cache_clear_all($cid, 'cache_image');
469 watchdog('image', 'Unable to generate the derived image located at %path.', $destination);
470 drupal_set_header('500 Internal Server Error');
471 print t('Error generating image.');
472 exit();
473 }
474 }
475
476 /**
477 * Create a new image based on an image style.
478 *
479 * @param $style
480 * An image style array.
481 * @param $source
482 * Path of the source file.
483 * @param $destination
484 * Path of the destination file.
485 * @return
486 * TRUE if an image derivative is generated, FALSE if no image derivative
487 * is generated. NULL if the derivative is being generated.
488 */
489 function image_style_create_derivative($style, $source, $destination) {
490 // Get the folder for the final location of this style.
491 $directory = dirname($destination);
492
493 // Build the destination folder tree if it doesn't already exist.
494 if (!file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
495 watchdog('image', 'Failed to create style directory: %directory', array('%directory' => $directory), WATCHDOG_ERROR);
496 return FALSE;
497 }
498
499 if (!$image = image_load($source)) {
500 return FALSE;
501 }
502
503 foreach ($style['effects'] as $effect) {
504 image_effect_apply($image, $effect);
505 }
506
507 if (!image_save($image, $destination)) {
508 if (file_exists($destination)) {
509 watchdog('image', 'Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array('%destination' => $destination), WATCHDOG_ERROR);
510 }
511 return FALSE;
512 }
513
514 return TRUE;
515 }
516
517 /**
518 * Flush cached media for a style.
519 *
520 * @param $style
521 * An image style array.
522 */
523 function image_style_flush($style) {
524 $style_directory = realpath(file_directory_path() . '/styles/' . $style['name']);
525 if (is_dir($style_directory)) {
526 file_unmanaged_delete_recursive($style_directory);
527 }
528
529 // Let other modules update as necessary on flush.
530 module_invoke_all('image_style_flush', $style);
531
532 // Clear image style and effect caches.
533 cache_clear_all('image_styles', 'cache');
534 cache_clear_all('image_effects', 'cache');
535 drupal_static_reset('image_styles');
536 drupal_static_reset('image_effects');
537
538 // Clear page caches when flushing.
539 if (module_exists('block')) {
540 cache_clear_all('*', 'cache_block', TRUE);
541 }
542 cache_clear_all('*', 'cache_page', TRUE);
543 }
544
545 /**
546 * Return the URL for an image derivative given a style and image path.
547 *
548 * This function is the default image generation method. It returns a URL for
549 * an image that can be used in an <img> tag. When the browser requests the
550 * image at image/generate/[style_name]/[path] the image is generated if it does
551 * not already exist and then served to the browser. This allows each image to
552 * have its own PHP instance (and memory limit) for generation of the new image.
553 *
554 * @param $style_name
555 * The name of the style to be used with this image.
556 * @param $path
557 * The path to the image.
558 * @return
559 * The absolute URL where a style image can be downloaded, suitable for use
560 * in an <img> tag. Requesting the URL will cause the image to be created.
561 * @see image_style_generate()
562 */
563 function image_style_url($style_name, $path) {
564 $destination = image_style_path($style_name, $path);
565
566 // If the image already exists use that rather than regenerating it.
567 if (file_exists($destination)) {
568 return file_create_url($destination);
569 }
570
571 // Disable page cache for this request. This prevents anonymous users from
572 // needlessly hitting the image generation URL when the image already exists.
573 drupal_page_is_cacheable(FALSE);
574
575 // Set a cache entry to grant access to this style/image path. This will be
576 // checked by image_style_generate().
577 cache_set('access:' . $style_name . ':' . md5($path), 1, 'cache_image', REQUEST_TIME + 600);
578
579 // Generate a callback path for the image.
580 $url = url('image/generate/' . $style_name . '/' . $path, array('absolute' => TRUE));
581 return $url;
582 }
583
584 /**
585 * Return a relative path to an image when using a style.
586 *
587 * The path returned by this function may not exist. The default generation
588 * method only creates images when they are requested by a user's browser.
589 *
590 * @param $style_name
591 * The name of the style to be used with this image.
592 * @param $path
593 * The path to the image.
594 * @return
595 * The path to an image style image relative to Drupal's root.
596 * @see image_style_url()
597 */
598 function image_style_path($style_name, $path) {
599 return file_directory_path() . '/styles/' . $style_name . '/' . file_directory_strip($path);
600 }
601
602 /**
603 * Pull in image effects exposed by modules implementing hook_image_effect_info().
604 *
605 * @return
606 * An array of image effects to be used when transforming images.
607 * @see hook_image_effect_info()
608 * @see image_effect_definition_load()
609 */
610 function image_effect_definitions() {
611 $effects = &drupal_static(__FUNCTION__);
612
613 if (!isset($effects)) {
614 if ($cache = cache_get('image_effects') && !empty($cache->data)) {
615 $effects = $cache->data;
616 }
617 else {
618 $effects = array();
619 foreach (module_implements('image_effect_info') as $module) {
620 foreach (module_invoke($module, 'image_effect_info') as $name => $effect) {
621 // Ensure the current toolkit supports the effect.
622 $effect['module'] = $module;
623 $effect['name'] = $name;
624 $effect['data'] = isset($effect['data']) ? $effect['data'] : array();
625 $effects[$name] = $effect;
626 };
627 }
628 uasort($effects, '_image_effect_definitions_sort');
629 cache_set('image_effects', $effects);
630 }
631 }
632
633 return $effects;
634 }
635
636 /**
637 * Load the definition for an image effect.
638 *
639 * The effect definition is a set of core properties for an image effect, not
640 * containing any user-settings. The definition defines various functions to
641 * call when configuring or executing an image effect. This loader is mostly for
642 * internal use within image.module. Use image_effect_load() or
643 * image_style_load() to get image effects that contain configuration.
644 *
645 * @param $effect
646 * The name of the effect definition to load.
647 * @return
648 * An array containing the image effect definition with the following keys:
649 * - "effect": The unique name for the effect being performed. Usually prefixed
650 * with the name of the module providing the effect.
651 * - "module": The module providing the effect.
652 * - "help": A description of the effect.
653 * - "function": The name of the function that will execute the effect.
654 * - "form": i'm (optional) The name of a function to configure the effect.
655 * - "summary": (optional) The name of a theme function that will display a
656 * one-line summary of the effect. Does not include the "theme_" prefix.
657 */
658 function image_effect_definition_load($effect) {
659 $definitions = image_effect_definitions();
660 return isset($definitions[$effect]) ? $definitions[$effect] : FALSE;
661 }
662
663 /**
664 * Load all image effects from the database.
665 *
666 * @return
667 * An array of all image effects.
668 * @see image_effect_load()
669 */
670 function image_effects() {
671 $effects = &drupal_static(__FUNCTION__);
672
673 if (!isset($effects)) {
674 $effects = array();
675
676 // Add database image effects.
677 $result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC))
678 ->fields('image_effects')
679 ->orderBy('image_effects.weight', 'ASC')
680 ->execute();
681 foreach ($result as $effect) {
682 $effect['data'] = unserialize($effect['data']);
683 $definition = image_effect_definition_load($effect['name']);
684 // Do not load image effects whose definition cannot be found.
685 if ($definition) {
686 $effect = array_merge($definition, $effect);
687 $effects[$effect['ieid']] = $effect;
688 }
689 }
690 }
691
692 return $effects;
693 }
694
695 /**
696 * Load a single image effect.
697 *
698 * @param $ieid
699 * The image effect ID.
700 * @return
701 * An image effect array, consisting of the following keys:
702 * - "ieid": The unique image effect ID.
703 * - "isid": The unique image style ID that contains this image effect.
704 * - "weight": The weight of this image effect within the image style.
705 * - "name": The name of the effect definition that powers this image effect.
706 * - "data": An array of configuration options for this image effect.
707 * Besides these keys, the entirety of the image definition is merged into
708 * the image effect array. Returns FALSE if the specified effect cannot be
709 * found.
710 * @see image_style_load()
711 * @see image_effect_definition_load()
712 */
713 function image_effect_load($ieid) {
714 $effects = image_effects();
715 return isset($effects[$ieid]) ? $effects[$ieid] : FALSE;
716 }
717
718 /**
719 * Save an image effect.
720 *
721 * @param $effect
722 * An image effect array.
723 * @return
724 * An image effect array. In the case of a new effect, 'ieid' will be set.
725 */
726 function image_effect_save($effect) {
727 if (!empty($effect['ieid'])) {
728 drupal_write_record('image_effects', $effect, 'ieid');
729 }
730 else {
731 drupal_write_record('image_effects', $effect);
732 }
733 $style = image_style_load(NULL, $effect['isid']);
734 image_style_flush($style);
735 return $effect;
736 }
737
738 /**
739 * Delete an image effect.
740 *
741 * @param $effect
742 * An image effect array.
743 */
744 function image_effect_delete($effect) {
745 db_delete('image_effects')->condition('ieid', $effect['ieid'])->execute();
746 $style = image_style_load(NULL, $effect['isid']);
747 image_style_flush($style);
748 }
749
750 /**
751 * Given an image object and effect, perform the effect on the file.
752 *
753 * @param $image
754 * An image object returned by image_load().
755 * @param $effect
756 * An image effect array.
757 * @return
758 * TRUE on success. FALSE if unable to perform the image effect on the image.
759 */
760 function image_effect_apply($image, $effect) {
761 if (drupal_function_exists($effect['effect callback'])) {
762 return call_user_func($effect['effect callback'], $image, $effect['data']);
763 }
764 return FALSE;
765 }
766
767 /**
768 * Return a themed image using a specific image style.
769 *
770 * @param $style_name
771 * The name of the style to be used to alter the original image.
772 * @param $path
773 * The path of the image file relative to the Drupal files directory.
774 * This function does not work with images outside the files directory nor
775 * with remotely hosted images.
776 * @param $alt
777 * The alternative text for text-based browsers.
778 * @param $title
779 * The title text is displayed when the image is hovered in some popular
780 * browsers.
781 * @param $attributes
782 * Associative array of attributes to be placed in the img tag.
783 * @param $getsize
784 * If set to TRUE, the image's dimension are fetched and added as
785 * width/height attributes.
786 * @return
787 * A string containing the image tag.
788 * @ingroup themeable
789 */
790 function theme_image_style($style_name, $path, $alt = '', $title = '', $attributes = array(), $getsize = TRUE) {
791 // theme_image() can only honor the $getsize parameter with local file paths.
792 // The derivative image is not created until it has been requested so the file
793 // may not yet exist, in this case we just fallback to the URL.
794 $style_path = image_style_path($style_name, $path);
795 if (!file_exists($style_path)) {
796 $style_path = image_style_url($style_name, $path);
797 }
798 return theme('image', $style_path, $alt, $title, $attributes, $getsize);
799 }
800
801 /**
802 * Accept a percentage and return it in pixels.
803 */
804 function image_filter_percent($value, $current_pixels) {
805 if (strpos($value, '%') !== FALSE) {
806 $value = str_replace('%', '', $value) * 0.01 * $current_pixels;
807 }
808 return $value;
809 }
810
811 /**
812 * Accept a keyword (center, top, left, etc) and return it as a pixel offset.
813 *
814 * @param $value
815 * @param $current_pixels
816 * @param $new_pixels
817 */
818 function image_filter_keyword($value, $current_pixels, $new_pixels) {
819 switch ($value) {
820 case 'top':
821 case 'left':
822 return 0;
823
824 case 'bottom':
825 case 'right':
826 return $current_pixels - $new_pixels;
827
828 case 'center':
829 return $current_pixels / 2 - $new_pixels / 2;
830 }
831 return $value;
832 }
833
834 /**
835 * Internal function for sorting image effect definitions through uasort().
836 *
837 * @see image_effect_definitions()
838 */
839 function _image_effect_definitions_sort($a, $b) {
840 return strcasecmp($a['name'], $b['name']);
841 }
842

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.