Question

I have png file and I want to have some part of this image (rectangle) to be transparent.

forexample something like this:

pseudocode:

<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>

where x, y, $width, $height define rectangle in image, that should be set transparent.

Is it possible using some libraries in PHP?

Was it helpful?

Solution 2

First, you need to set alpha channels to your image: http://www.php.net/manual/en/function.imagealphablending.php http://www.php.net/manual/en/function.imagesavealpha.php

Second, you need set transparent color to all pixels in your transparent area: http://www.php.net/manual/en/function.imagecolorset.php

OTHER TIPS

Yes it is possible. You can define an area in the image, fill it with a color and then set that color as transparennt. It requires the availability of the GD libraries. The respective manual for the command has this code in the example:

<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>

In your case, you would take an existing image with the respective function. the resulting resource would be the $im in the above example, you would then allocate a color, set it transparent and draw the rectangle as above, then save the image:

<?php
// get the image form the filesystem
$im = imagecreatefromjpeg($imgname);
// let's assume there is no red in the image, so lets take that one
$red = imagecolorallocate($im, 255, 0, 0);

// Make the red color transparent
imagecolortransparent($im, $red);

// Draw a red rectangle in the image
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top