You are here

protected function GDOperationTrait::imageCopyMergeAlpha in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::imageCopyMergeAlpha()
  2. 8.2 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::imageCopyMergeAlpha()

Copy and merge part of an image, preserving alpha.

The standard imagecopymerge() function in PHP GD fails to preserve the alpha information of two merged images. This method implements the workaround described in http://php.net/manual/en/function.imagecopymerge.php#92787

Parameters

resource $dst_im: Destination image link resource.

resource $src_im: Source image link resource.

int $dst_x: X-coordinate of destination point.

int $dst_y: Y-coordinate of destination point.

int $src_x: X-coordinate of source point.

int $src_y: Y-coordinate of source point.

int $src_w: Source width.

int $src_h: Source height.

int $pct: Opacity of the source image in percentage.

Return value

bool Returns TRUE on success or FALSE on failure.

See also

http://php.net/manual/en/function.imagecopymerge.php#92787

3 calls to GDOperationTrait::imageCopyMergeAlpha()
Background::execute in src/Plugin/ImageToolkit/Operation/gd/Background.php
Performs the actual manipulation on the image.
GDOperationTrait::getEntropyCropByGridding in src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php
Computes the entropy crop of an image, using recursive gridding.
Watermark::execute in src/Plugin/ImageToolkit/Operation/gd/Watermark.php
Performs the actual manipulation on the image.

File

src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php, line 113

Class

GDOperationTrait
Trait for GD image toolkit operations.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function imageCopyMergeAlpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) {
  if ($pct === 100) {

    // Use imagecopy() if opacity is 100%.
    return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  }
  else {

    // If opacity is below 100%, use the approach described in
    // http://php.net/manual/it/function.imagecopymerge.php#92787
    // to preserve watermark alpha.
    // --------------------------------------
    // Create a cut resource.
    // @todo when #2583041 is committed, add a check for memory
    // availability before creating the resource.
    $cut = imagecreatetruecolor($src_w, $src_h);

    // @todo remove the is_resource check when PHP 8.0 is minimum version.
    if (!is_object($cut) && !is_resource($cut)) {
      return FALSE;
    }

    // Copy relevant section from destination image to the cut resource.
    if (!imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h)) {
      imagedestroy($cut);
      return FALSE;
    }

    // Copy relevant section from merged image to the cut resource.
    if (!imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h)) {
      imagedestroy($cut);
      return FALSE;
    }

    // Insert cut resource to destination image.
    $success = imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
    imagedestroy($cut);
    return $success;
  }
}