You are here

function textimage_image_stroke_change_pixels in Textimage 5

Same name and namespace in other branches
  1. 5.2 textimage.module \textimage_image_stroke_change_pixels()
  2. 6.2 textimage.module \textimage_image_stroke_change_pixels()
  3. 7.2 textimage.utils.inc \textimage_image_stroke_change_pixels()

Utility function for image_stroke. Analyzes surrounding pixels and determines opacity of a pixel at that x-y coordinate

1 call to textimage_image_stroke_change_pixels()
textimage_image_add_stroke in ./textimage.module
Stroke function adds a solid color stroke around an image with a transparent background.

File

./textimage.module, line 863

Code

function textimage_image_stroke_change_pixels(&$img, &$border_img, $thickness, $color, $x, $y, $width, $height) {
  list($r, $g, $b) = _textimage_hex2rgb($color);
  $pixel = imagecolorsforindex($img, imagecolorat($img, $x, $y));

  // Preform a radial analysis of all pixels within the radius of $thickness pixels
  $degree_increment = 90 / $thickness;
  $radial_coords = array();
  for ($degrees = 0; $degrees <= 90; $degrees += $degree_increment) {
    $x_offset = round(cos($degrees) * $thickness);
    $y_offset = round(sin($degrees) * $thickness);

    
    $radial_coords[] = array(
      'x' => $x + $x_offset,
      'y' => $y + $y_offset,
    );
    $radial_coords[] = array(
      'x' => $x - $x_offset,
      'y' => $y + $y_offset,
    );
    $radial_coords[] = array(
      'x' => $x + $x_offset,
      'y' => $y - $y_offset,
    );
    $radial_coords[] = array(
      'x' => $x - $x_offset,
      'y' => $y - $y_offset,
    );
  }

  // Generate a total alpha level for all analyzed pixels
  $total_alpha = 0;
  $total_colors = 0;
  foreach ($radial_coords as $coords) {
    if ($coords['x'] >= 0 && $coords['y'] >= 0 && $coords['x'] < $width && $coords['y'] < $height) {
      $xy_color = imagecolorsforindex($img, imagecolorat($img, $coords['x'], $coords['y']));
    }
    else {

      // This analized pixel is outside the dimensions of the image, record as transparent
      $xy_color = array(
        'alpha' => '127',
      );
    }
    $total_alpha += $xy_color['alpha'];
    $total_colors++;
  }

  // Check that we're not in the middle of the image or in a blonk area
  if ($total_alpha < 127 * $total_colors && $total_alpha > 0) {

    // If we're on a semi-transparent pixel, blend the remaining amount with our border color
    if ($pixel['alpha'] < 127) {
      $alpha = 127 - $pixel['alpha'];
    }
    else {
      $alpha = 127 - (127 * $total_colors - $total_alpha);
    }
    $alpha = $alpha < 0 ? 0 : $alpha;
    $alpha = $alpha > 127 ? 127 : $alpha;

    // Apply the color to the border overlay image
    $color = imagecolorallocatealpha($border_img, $r, $g, $b, $alpha);
    imagesetpixel($border_img, $x, $y, $color);
  }
}