我有PNG文件,我想這個圖像(矩形)的一些部分是透明的。PHP:如何將png文件的一部分設置爲透明?
forexample是這樣的:
僞代碼:
<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>
其中x,y,寬度$,$高度限定在圖像矩形,即應設置透明的。
是否有可能在PHP中使用某些庫?
我有PNG文件,我想這個圖像(矩形)的一些部分是透明的。PHP:如何將png文件的一部分設置爲透明?
forexample是這樣的:
僞代碼:
<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>
其中x,y,寬度$,$高度限定在圖像矩形,即應設置透明的。
是否有可能在PHP中使用某些庫?
首先,您需要設置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
是的,它是可能的。您可以在圖像中定義一個區域,填充顏色,然後將該顏色設置爲透明。它要求提供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);
?>