You are here

function image_gd_image_effects_text in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 image_effects_text/image_effects_text.inc \image_gd_image_effects_text()

GD toolkit specific implementation of the text effect.

Parameters

stdClass $image:

array $data: The parameters for this effect.

Return value

bool true on success, false otherwise.

File

image_effects_text/image_effects_text.inc, line 373

Code

function image_gd_image_effects_text(stdClass $image, array $data) {

  // Convert color and alpha to GD alpha and color value.
  // GD alpha value: 0 = opaque, 127 = transparent.
  $data['alpha'] = (int) ((1 - $data['alpha'] / 100) * 127);
  $color = imagecolorallocatealpha($image->resource, $data['RGB']['red'], $data['RGB']['green'], $data['RGB']['blue'], $data['alpha']);
  if ($color !== FALSE) {
    $bounds = NULL;

    // Adjust Y position for vertical alignment (if different from bottom).
    if ($data['valign'] !== 'bottom') {

      // Get bounding box.
      // PHP Manual: "This function requires both the GD library and the » FreeType library."
      // So it is not more demanding than imagettftext, which we need anyway.
      $bounds = imagettfbbox($data['size'], 0, $data['fontpath'], $data['text']);
      if (!$bounds) {
        drupal_set_message(t('Failed to calculate text dimensions using GD toolkit. Ignoring the alignment settings.'), 'warning');
      }
      else {

        // Get height of bounding box.
        $height = $bounds[1] - $bounds[7];

        // Shift ypos down (full height on bottom, half the height on center).
        $data['ypos'] += $data['valign'] === 'center' ? (int) ($height / 2) : $height;
      }
    }

    // Adjust X position for horizontal alignment (if different from left).
    if ($data['halign'] !== 'left') {

      // Get bounding box. PHP Manual: "This function requires both the GD
      // library and the » FreeType library.", so it is not more demanding than
      // imagettftext(), which we need anyway.
      if ($bounds === NULL) {
        $bounds = imagettfbbox($data['size'], 0, $data['fontpath'], $data['text']);
        if (!$bounds) {
          drupal_set_message(t('Failed to calculate text dimensions using GD toolkit. Ignoring the alignment.'), 'warning');
        }
      }
      if ($bounds !== FALSE) {

        // Get width of bounding box.
        $width = $bounds[2] - $bounds[0];

        // Shift xpos to the left (full width on right, half the width on center).
        $data['xpos'] -= $data['halign'] === 'center' ? (int) ($width / 2) : $width;
      }
    }

    // PHP Manual: "This function requires both the GD library and the » FreeType library."
    $bounds = imagettftext($image->resource, $data['size'], $data['angle'], $data['xpos'], $data['ypos'], $color, $data['fontpath'], $data['text']);
    return $bounds !== FALSE;
  }
  return FALSE;
}