Hi everyone,
I am having problems with some of GD's functions, specifically imagecopymerge.
The codeđSlightly modified example from php.net)
<?php
// A fix to get a function like imagecopymerge WITH ALPHA SUPPORT
// Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
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 );
// Find the most opaque pixel in the image (the one with the 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 = 0; $x < $w; $x++ )
{
for( $y = 0; $y < $h; $y++ )
{
//get current alpha value (represents the TANSPARENCY!)
$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);
}
// USAGE EXAMPLE:
$img_b = imagecreatefromjpeg('http://i.ytimg.com/vi/6McBlnlxo_8/hqdefault.jpg');
$img_a = imagecreatefrompng('1.png');
// SAME COMMANDS:
imagecopymerge_alpha($img_a, $img_b, 470, 1, 0, 0, imagesx($img_b), imagesy($img_b),50);
// OUTPUT IMAGE:
header("Content-Type: image/png");
imagesavealpha($img_a, true);
imagepng($img_a, NULL);
?>
The result:
http://filedrop.test-platform.co.uk/php_gd_combine_images/test.php
ExpectingđDifferent image, but still same effect).
http://airtv.test-platform.co.uk/images/header_bg/bowlingforsoup.png
Problem:
I can not seem to get the transparency effect right as you can see from what I am expecting it is massively different, in fact the result does not actually seem to be merging them in layers, i.e. greybox on top and picture on bottom(and to the left).
Can anyone spot what I am doing wrong?