2013-03-12 67 views
1

我有PNG文件,我想这个图像(矩形)的一些部分是透明的。PHP:如何将png文件的一部分设置为透明?

forexample是这样的:

伪代码:

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

其中x,y,宽度$,$高度限定在图像矩形,即应设置透明的。

是否有可能在PHP中使用某些库?

回答

1

是的,它是可能的。您可以在图像中定义一个区域,填充颜色,然后将该颜色设置为透明。它要求提供GD libraries。相应的manual for the command有这个代码的例子:

<?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); 
?> 

在你的情况,你会采取现有的图像与respective function。得到的资源将是上述示例中的$ im,然后您将分配一种颜色,将其设置为透明并如上所示绘制矩形,然后保存该图像:

<?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); 
?>