palehorse
palehorse

Reputation: 43

PHP: transparent area in PNG

I want to create a transparent area in a png image, some sort of "hole". So I could place this image on top of some background image and see a fragment of the background through that "hole". I found this code on some forum:

$imgPath = 'before.png';
$img = imagecreatefrompng($imgPath); // load the image
list($width,$height) = getimagesize($imgPath); // get its size
$c = imagecolortransparent($img,imagecolorallocate($img,255,1,254)); // create transparent color, (255,1,254) is a color that won't likely occur in your image
$border = 10;
imagefilledrectangle($img, $border, $border, $width-$border, $height-$border, $c); // draw transparent box
imagepng($img,'after.png'); // save

It works for creating the transparent area (rectangular in this case) in png image. But when I place this png image on top of other image, the area loses transparency, so I end up with the colored rectangle in the middle of the resulting image. Can someone please help me?

Upvotes: 4

Views: 894

Answers (2)

Andrew Odri
Andrew Odri

Reputation: 9432

An alternative option would be using the PHP ImageMagick extension, Imagick.

You can create the rectangle by setting the background parameter of the Imagick::newImage function, the cicle using the ImagickDraw::circle function, and the key is to apply the circle using the Imagick::compositeImage and only copying the transparency over. This will prevent you from having a solid image with a transparent circle on top; everything that is transparent in the mask will be transparent on the original image.

The below code should do the trick (although I am sure it will need a few tweaks to meet your needs :P):

<?php

    $base = new Imagick("before.png");
    $base->cropImage(512, 512, 0, 0);
    $base->setImageMatte(true);

    $mask = new Imagick();
    $mask->newImage(512, 512, new ImagickPixel("transparent"));

    $circle = new ImagickDraw();
    $circle->setFillColor("black");
    $circle->circle(150, 150, 100, 100);

    $mask->drawImage($circle);

    $base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);

    $base->writeImage('after.png');
    header("Content-Type: image/png");
    echo $base;

?>

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Try this for the transparent colour:

$c = imagecolorallocatealpha($img,0,0,0,127);

Upvotes: 0

Related Questions