Skip to content

Bug #23815: Added ImageCopyMergeAlpha #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,17 @@ ZEND_BEGIN_ARG_INFO(arginfo_imagecopymerge, 0)
ZEND_ARG_INFO(0, src_h)
ZEND_ARG_INFO(0, pct)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_imagecopymergealpha, 0)
ZEND_ARG_INFO(0, src_im)
ZEND_ARG_INFO(0, dst_im)
ZEND_ARG_INFO(0, dst_x)
ZEND_ARG_INFO(0, dst_y)
ZEND_ARG_INFO(0, src_x)
ZEND_ARG_INFO(0, src_y)
ZEND_ARG_INFO(0, src_w)
ZEND_ARG_INFO(0, src_h)
ZEND_ARG_INFO(0, pct)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_imagecopymergegray, 0)
ZEND_ARG_INFO(0, src_im)
ZEND_ARG_INFO(0, dst_im)
Expand Down Expand Up @@ -917,6 +927,7 @@ const zend_function_entry gd_functions[] = {
PHP_FE(imagecopy, arginfo_imagecopy)
#if HAVE_LIBGD15
PHP_FE(imagecopymerge, arginfo_imagecopymerge)
PHP_FE(imagecopymergealpha, arginfo_imagecopymergealpha)
PHP_FE(imagecopymergegray, arginfo_imagecopymergegray)
#endif
PHP_FE(imagecopyresized, arginfo_imagecopyresized)
Expand Down Expand Up @@ -3779,6 +3790,35 @@ PHP_FUNCTION(imagecopymerge)
}
/* }}} */

/* {{{ proto bool imagecopymergealpha(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
Merge one part of an image with another -while preserving Alpha channel*/
PHP_FUNCTION(imagecopymergealpha)
{
zval *SIM, *DIM;
long SX, SY, SW, SH, DX, DY, PCT;
gdImagePtr im_dst, im_src;
int srcH, srcW, srcY, srcX, dstY, dstX, pct;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrlllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
return;
}

ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);

srcX = SX;
srcY = SY;
srcH = SH;
srcW = SW;
dstX = DX;
dstY = DY;
pct = PCT;

gdImageCopyMergeAlpha(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
RETURN_TRUE;
}
/* }}} */

/* {{{ proto bool imagecopymergegray(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
Merge one part of an image with another */
PHP_FUNCTION(imagecopymergegray)
Expand Down
76 changes: 76 additions & 0 deletions ext/gd/libgd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,82 @@ void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int s
}
}

/* This function is a real alpha channel operations,
but it has restrictions. */
void gdImageCopyMergeAlpha (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct)
{
int c;
int x, y;
int toy;
toy = dstY;

if (pct == 100) {
/* no opacity adjustment required pass through to gdImageCopy() */
gdImageCopy(dst, src, dstX, dstY, srcX, srcY, w, h);
return;
}

if (pct == 0) {
/* 0% opacity? nothing needs to be done */
return;
}

if (src->trueColor && dst->trueColor) {
/* support for maintaining the alpha (transparency) of both source and
* destination images (assuming they are true colour) while opacity blending.
*/
int ca, cr, cg, cb, nc;
float na;
float ac;

/* we need to loop through the src image to get the max transparency level */
int mt = 0;

for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
c = gdImageGetTrueColorPixel (src, srcX + x, srcY + y);
ca = gdImageAlpha(src, c);

mt = ca > mt ? ca : mt;
}
}

if (mt < (gdAlphaMax / 2)) {
mt = gdAlphaMax - mt;
}

/* alpha correction factor */
ac = (float)mt / gdAlphaMax;

/* loop through the image again and set/adjust alpha channel level */
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
c = gdImageGetTrueColorPixel (src, srcX + x, srcY + y);
ca = gdImageAlpha(src, c);
cr = gdImageRed(src, c);
cg = gdImageGreen(src, c);
cb = gdImageBlue(src, c);

na = (ca + gdAlphaMax - (gdAlphaMax * ((float)pct / 100))) * ac;
na = (na > gdAlphaMax) ? gdAlphaMax : ((na < gdAlphaOpaque) ? gdAlphaOpaque: na);

nc = gdImageColorAllocateAlpha(dst, cr, cg, cb, (int)na);
if (nc == -1) {
gdImageColorClosestAlpha(dst, cr, cg, cb, (int)na);
}

/* set pixel on destination image */
gdImageSetPixel (dst, dstX + x, dstY + y, nc);
}
}

return;
}

/* falback */
gdImageCopyMerge(dst, src, dstX, dstY, srcX, srcY, w, h, pct);
}

/* This function is a substitute for real alpha channel operations,
so it doesn't pay attention to the alpha channel. */
void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct)
Expand Down
3 changes: 3 additions & 0 deletions ext/gd/libgd/gdtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ main (int argc, char **argv)
gdImageCopyMerge (im2, im3, 150, 200, 10, 10, 90, 50, 50);
gdImageCopyMerge (im2, im3, 180, 70, 10, 10, 90, 50, 50);

gdImageCopyMergeAlpha (im2, im3, 150, 200, 10, 10, 90, 50, 50);
gdImageCopyMergeAlpha (im2, im3, 180, 70, 10, 10, 90, 50, 50);

gdImageCopyMergeGray (im2, im3, 250, 160, 10, 10, 90, 50, 50);
gdImageCopyMergeGray (im2, im3, 80, 70, 10, 10, 90, 50, 50);

Expand Down
1 change: 1 addition & 0 deletions ext/gd/php_gd.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ PHP_FUNCTION(imagecolorsforindex);
PHP_FUNCTION(imagecolortransparent);
PHP_FUNCTION(imagecopy);
PHP_FUNCTION(imagecopymerge);
PHP_FUNCTION(imagecopymergealpha);
PHP_FUNCTION(imagecopyresized);
PHP_FUNCTION(imagetypes);
PHP_FUNCTION(imagecreate);
Expand Down
Binary file added ext/gd/tests/ff-logo-sm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions ext/gd/tests/imagecopymergealpha_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Testing imagecopymergealpha() of GD library
--CREDITS--
Matt Clegg <cleggmatt [at] gmail [dot] com>
Orignally by: redmonkey
--SKIPIF--
<?php
if (!extension_loaded("gd")) die("skip GD not present");
?>
--FILE--
<?php

$images=array(
'imagecopymerge'=>1,
'imagecopymergealpha'=>2
);

foreach ($images as $f=>$md5){

$bg = imagecreatefrompng('tux.png');
$over = imagecreatefrompng('ff-logo-sm.png');
imagealphablending($bg, true);
imagesavealpha($bg, true);
$f($bg, $over, 276, 300, 0, 0, 123, 119, 50);

ob_start();
imagepng($bg);
$dump = "$f: ".md5(ob_get_contents())."\n";
ob_end_clean();

echo $dump;

}


?>
--EXPECTF--
imagecopymerge: 8e5cf51d6f59e9cea8572bfac47b5668
imagecopymergealpha: f685c4a30c4255de4290c928ab4ac05e

Binary file added ext/gd/tests/tux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.