1 image_example.module image_example_colorize_effect(&$image, $data)

Image effect callback; Colorize an image resource.


object $image: An image object returned by image_load().

array $data: An array of attributes to use when performing the colorize effect with the following items:

  • "color": The web-style hex color to use when colorizing the image.

Return value

bool: TRUE on success. FALSE on failure to colorize image.

Related topics


modules/examples/image_example/image_example.module, line 239
Hooks implementations for the Image Example module.


function image_example_colorize_effect(&$image, $data) {
  // Image manipulation should be done to the $image->resource, which will be
  // automatically saved as a new image once all effects have been applied.
  // If your effect makes changes to the $image->resource that relate to any
  // information stored in the $image->info array (width, height, etc.) you
  // should update that information as well. See modules/system/image.gd.inc
  // for examples of functions that perform image manipulations.
  // Not all GD installations are created equal. It is a good idea to check for
  // the existence of image manipulation functions before using them.
  // PHP installations using non-bundled GD do not have imagefilter(). More
  // information about image manipulation functions is available in the PHP
  // manual. http://www.php.net/manual/en/book.image.php
  if (!function_exists('imagefilter')) {
    watchdog('image', 'The image %image could not be colorized because the imagefilter() function is not available in this PHP installation.', array('%image' => $image->source));
    return FALSE;

  // Verify that Backdrop is using the PHP GD library for image manipulations
  // since this effect depends on functions in the GD library.
  if ($image->toolkit != 'gd') {
    watchdog('image', 'Image colorize failed on %path. Using non GD toolkit.', array('%path' => $image->source), WATCHDOG_ERROR);
    return FALSE;

  // Convert short #FFF syntax to full #FFFFFF syntax.
  if (strlen($data['color']) == 4) {
    $c = $data['color'];
    $data['color'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];

  // Convert #FFFFFF syntax to hexadecimal colors.
  $data['color'] = hexdec(str_replace('#', '0x', $data['color']));

  // Convert the hexadecimal color value to a color index value.
  $rgb = array();
  for ($i = 16; $i >= 0; $i -= 8) {
    $rgb[] = (($data['color'] >> $i) & 0xFF);

  // First desaturate the image, and then apply the new color.
  imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
  imagefilter($image->resource, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]);

  return TRUE;