You are here

function _imagecache_reflect_imagecopymerge_alpha in ImageCache Reflect 7

A fix to get a function like imagecopymerge with alpha blending.

Main script by aiden dot mail at freemail dot hu. Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com

See also

http://uk3.php.net/manual/en/function.imagecopymerge.php#88456

1 call to _imagecache_reflect_imagecopymerge_alpha()
imagecache_reflect_image in ./imagecache_reflect.module
Implements hook_image().

File

./imagecache_reflect.module, line 252
Adds a reflection action for images.

Code

function _imagecache_reflect_imagecopymerge_alpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct, $minalpha = NULL) {
  if (!isset($pct)) {
    return FALSE;
  }
  $pct /= 100;

  // Get image width and height.
  $w = imagesx($src_im);
  $h = imagesy($src_im);

  // Turn alpha blending off.
  imagealphablending($src_im, FALSE);
  if (is_null($minalpha)) {

    // Find the most opaque pixel in the image (smallest alpha value).
    $minalpha = 127;
    for ($x = 0; $x < $w; $x++) {
      for ($y = 0; $y < $h; $y++) {
        $alpha = imagecolorat($src_im, $x, $y) >> 24 & 0xff;
        if ($alpha < $minalpha) {
          $minalpha = $alpha;
        }
      }
    }
  }

  // Loop through image pixels and modify alpha for each.
  for ($x = $src_x; $x < $src_x + $src_w; $x++) {
    for ($y = $src_y; $y < $src_y + $src_h; $y++) {

      // Get current alpha value (represents the transparency).
      $colorxy = imagecolorat($src_im, $x, $y);
      $alpha = $colorxy >> 24 & 0xff;

      // Calculate new alpha.
      if ($minalpha !== 127) {
        $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
      }
      else {
        $alpha += 127 * $pct;
      }

      // Get the color index with new alpha.
      $alphacolorxy = imagecolorallocatealpha($src_im, $colorxy >> 16 & 0xff, $colorxy >> 8 & 0xff, $colorxy & 0xff, $alpha);

      // Set pixel with the new color + opacity.
      if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
        return FALSE;
      }
    }
  }

  // The image copy.
  imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}