我有 png 文件,我想让这个图像(矩形)的某些部分是透明的。
例如这样的:
伪代码:
<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>
其中 x, y, $width, $height 定义图像中的矩形,应该设置为透明。
是否可以在 PHP 中使用某些库?
我有 png 文件,我想让这个图像(矩形)的某些部分是透明的。
例如这样的:
伪代码:
<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>
其中 x, y, $width, $height 定义图像中的矩形,应该设置为透明。
是否可以在 PHP 中使用某些库?
是的,有可能。您可以在图像中定义一个区域,用一种颜色填充它,然后将该颜色设置为透明。它需要GD 库的可用性。该命令的相应手册在示例中有此代码:
<?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);
?>
在您的情况下,您将使用相应的功能拍摄现有图像。生成的资源将是上面示例中的 $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);
?>
首先,您需要为您的图像设置 alpha 通道: http://www.php.net/manual/en/function.imagealphablending.php http://www.php.net/manual/en/function.imagesavealpha.php
其次,您需要为透明区域中的所有像素设置透明颜色: http ://www.php.net/manual/en/function.imagecolorset.php