imagefilter
(PHP 5, PHP 7)
imagefilter - 将过滤器应用于图像
描述
bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 [, int $arg4 ]]]] )
ImageFilter()应用于给定的过滤器filtertype
上image
。
参数
`image`
一个图像资源,由图像创建函数之一返回,如imagecreatetruecolor()。
filtertype
filtertype
可以是以下之一:
IMG_FILTER_NEGATE
:反转图像的所有颜色。
IMG_FILTER_GRAYSCALE
:通过使用与REC.601亮度(Y')计算相同的系数将红色,绿色和蓝色分量更改为其加权和,将图像转换为灰度。alpha组件被保留。对于调色板图像,由于调色板的限制,结果可能会有所不同。
IMG_FILTER_BRIGHTNESS
:更改图像的亮度。使用arg1
设置亮度水平。亮度范围是-255到255。
IMG_FILTER_CONTRAST
:改变图像的对比度。使用arg1
设置的对比度级别。
IMG_FILTER_COLORIZE
:喜欢IMG_FILTER_GRAYSCALE
,除了你可以指定颜色。使用arg1
,arg2
并arg3
在形式red
,green
,blue
并arg4
为alpha
通道。每种颜色的范围是0到255。
IMG_FILTER_EDGEDETECT
:使用边缘检测来突出显示图像中的边缘。
IMG_FILTER_EMBOSS
:压印图像。
IMG_FILTER_GAUSSIAN_BLUR
:使用高斯方法模糊图像。
IMG_FILTER_SELECTIVE_BLUR
:模糊图像。
IMG_FILTER_MEAN_REMOVAL
:使用平均去除来实现“粗略”效果。
IMG_FILTER_SMOOTH
:使图像更平滑。使用arg1
设置平滑度。
IMG_FILTER_PIXELATE
:对图像应用像素化效果,用于arg1
设置块大小和arg2
设置像素化效果模式。
arg1
IMG_FILTER_BRIGHTNESS
:亮度级别。
IMG_FILTER_CONTRAST
:对比度等级。
IMG_FILTER_COLORIZE
:红色组件的值。
IMG_FILTER_SMOOTH
:平滑度。
IMG_FILTER_PIXELATE
:以像素为单位的块大小。
arg2
IMG_FILTER_COLORIZE
:绿色组件的价值。
IMG_FILTER_PIXELATE
:是否使用高级像素效果(默认为FALSE
)。
arg3
IMG_FILTER_COLORIZE
:蓝色分量的值。arg4
IMG_FILTER_COLORIZE
:Alpha通道,介于0和127之间的值。0表示完全不透明,而127表示完全透明。
返回值
返回TRUE
成功或失败时返回FALSE
。
更新日志
版 | 描述 |
---|---|
5.3.0 | 支持像素化(IMG_FILTER_PIXELATE)。 |
5.2.5 | 增加了对IMG_FILTER_COLORIZE的Alpha支持。 |
例子
Example #1 imagefilter() grayscale example
<?php
$im?=?imagecreatefrompng('dave.png');
if($im?&&?imagefilter($im,?IMG_FILTER_GRAYSCALE))
{
????echo?'Image?converted?to?grayscale.';
????imagepng($im,?'dave.png');
}
else
{
????echo?'Conversion?to?grayscale?failed.';
}
imagedestroy($im);
?>
Example #2 imagefilter() brightness example
<?php
$im?=?imagecreatefrompng('sean.png');
if($im?&&?imagefilter($im,?IMG_FILTER_BRIGHTNESS,?20))
{
????echo?'Image?brightness?changed.';
????imagepng($im,?'sean.png');
????imagedestroy($im);
}
else
{
????echo?'Image?brightness?change?failed.';
}
?>
Example #3 imagefilter() colorize example
<?php
$im?=?imagecreatefrompng('philip.png');
/*?R,?G,?B,?so?0,?255,?0?is?green?*/
if($im?&&?imagefilter($im,?IMG_FILTER_COLORIZE,?0,?255,?0))
{
????echo?'Image?successfully?shaded?green.';
????imagepng($im,?'philip.png');
????imagedestroy($im);
}
else
{
????echo?'Green?shading?failed.';
}
?>
Example #4 imagefilter() negate example
<?php
//?Define?our?negate?function?so?its?portable?for?
//?php?versions?without?imagefilter()
function?negate($im)
{
????if(function_exists('imagefilter'))
????{
????????return?imagefilter($im,?IMG_FILTER_NEGATE);
????}
????for($x?=?0;?$x?<?imagesx($im);?++$x)
????{
????????for($y?=?0;?$y?<?imagesy($im);?++$y)
????????{
????????????$index?=?imagecolorat($im,?$x,?$y);
????????????$rgb?=?imagecolorsforindex($index);
????????????$color?=?imagecolorallocate($im,?255?-?$rgb['red'],?255?-?$rgb['green'],?255?-?$rgb['blue']);
????????????imagesetpixel($im,?$x,?$y,?$color);
????????}
????}
????return(true);
}
$im?=?imagecreatefromjpeg('kalle.jpg');
if($im?&&?negate($im))
{
????echo?'Image?successfully?converted?to?negative?colors.';
????imagejpeg($im,?'kalle.jpg',?100);
????imagedestroy($im);
}
else
{
????echo?'Converting?to?negative?colors?failed.';
}
?>
Example #5 imagefilter() pixelate example
<?php
//?Load?the?PHP?logo,?we?need?to?create?two?instances?
//?to?show?the?differences
$logo1?=?imagecreatefrompng('./php.png');
$logo2?=?imagecreatefrompng('./php.png');
//?Create?the?image?instance?we?want?to?show?the?
//?differences?on
$output?=?imagecreatetruecolor(imagesx($logo1)?*?2,?imagesy($logo1));
//?Apply?pixelation?to?each?instance,?with?a?block?
//?size?of?3
imagefilter($logo1,?IMG_FILTER_PIXELATE,?3);
imagefilter($logo2,?IMG_FILTER_PIXELATE,?3,?true);
//?Merge?the?differences?onto?the?output?image
imagecopy($output,?$logo1,?0,?0,?0,?0,?imagesx($logo1)?-?1,?imagesy($logo1)?-?1);
imagecopy($output,?$logo2,?imagesx($logo2),?0,?0,?0,?imagesx($logo2)?-?1,?imagesy($logo2)?-?1);
imagedestroy($logo1);
imagedestroy($logo2);
//?Output?the?differences
header('Content-Type:?image/png');
imagepng($output);
imagedestroy($output);
?>
上面的例子会输出类似于:
← imagefilltoborder
imageflip →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com