Alpha support for PHP's imagecopymerge function

Blog
PHP
Programming

I've just checked PHP's issue tracker and a core developer says that this function (imagecopymerge) was never meant to support alpha channel! and they refused to commit the provided patch! so ridicules Anyway i tried rodrigo's workaround and it worked quite well, thanks rodrigo for sharing it. I came up with another idea which is much faster than his solution, (it may need a little bit more memory)

It's also shared here

    /**
     * PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
     * This is a function like imagecopymerge but it handle alpha channel well!!!
     **/
    function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
        $opacity=$pct;
        // getting the watermark width
        $w = imagesx($src_im);
        // getting the watermark height
        $h = imagesy($src_im);
        
        // creating a cut resource
        $cut = imagecreatetruecolor($src_w, $src_h);
        // copying that section of the background to the cut
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
        // inverting the opacity
        $opacity = 100 - $opacity;
        
        // placing the watermark now
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);
    }
Your rating: None Average: 4 (5 votes)

Comments

Chris's picture

Hi, I took your email from

Hi, I took your email from your post on PHP.net and wondering if you could
shed some light on my problem...

I can currently create an image using text input and background selected by a
user. See here. What I'm looking to do is create the image and then use it to
merge with another img (icon) but not sure how to modify myimage.php below to
acheive this in one go, can you help? Ultimately I'm looking to select an
icon to display where you see the maple leaf on the bg image.

I can pass the icon variable below:

function getImage()
{
var img_text = document.getElementById("inp_img_text").value;
var img_bg = document.getElementById("inp_img_bg").value;
var img_icon = document.getElementById("inp_icon").value;
var img_obj = document.getElementById("btn_img_change");
img_obj.src = "myimage.php?name=" + img_text + "&bg=" + img_bg +
"&icon=" + img_icon;
}

Name:

Background:

Teal
Red
Black

Icon:

Face
Sun

But how would I modify my PHP to imagecreate and imagecopyandmerge?
<?php
$please = $_REQUEST['bg'] ;
$my_img = imagecreatefromjpeg( $please);
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
$input_text = $_REQUEST['name'] ;
//$font = 'arial.ttf';
//imagettftext ($my_img, 6, 15, 30, 40, $txt_colour, $font, $input_text);

imagestring( $my_img, 10, 30, 32, $input_text, $text_colour );
imagesetthickness ( $my_img, 5 );
//imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
//imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

I would greatly appreciate any help with this. Cheers! Chris.

Wolfram's picture

Hi Sina, found you through

Hi Sina, found you through your posting: imagecopymerge_alpha. Tnx 2 for the hint at spam poison. My reading at the moment: Mad - We are treating the wrong ones. The problem are the normal ones. (In German) Nice page. Wolfram

admin's picture

Hello , Glad you liked it.

Hello ,
Glad you liked it.

Wolfram's picture

Hi Sina, I have a problem

Hi Sina,
I have a problem with your php-function:

<?php
/**
* PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
* This is a function like imagecopymerge but it handle alpha channel well!!!
**/
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$opacity=$pct;
// getting the watermark width
$w = imagesx($src_im);
// getting the watermark height
$h = imagesy($src_im);

// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying that section of the background to the cut
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// inverting the opacity
$opacity = 100 - $opacity;

// placing the watermark now
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);
}
?>

May be there is something missing or superfluous: $w and $h are not used later on.
Starting to hate the gd-part of php. But I need a lot of small pictures as buttons for my project and to stay flexible its better to generate them with a script. I hope the time invested will bear fruit.
Thanks in advance
Wolfram

Marc Kneifel's picture

Hello! Having problems with

Hello!

Having problems with alpha channel support in php I searched a while and found you solution at the php manuals. I like it very much! Thank you for this great extension.

Best Regards from Germany

admin's picture

Hi Marc, Glad to hear that

Hi Marc,
Glad to hear that you liked it :)

Visitor's picture

Thank you for this solution.

Thank you for this solution.

Darin's picture

I don't know who you are, but

I don't know who you are, but you're a lifesaver. :) I've been spending hours on this problem, then found your post, and it works perfectly!