首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

imagefilter

(PHP 5, PHP 7)

imagefilter - 将过滤器应用于图像

描述

代码语言:javascript
复制
bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 [, int $arg4 ]]]] )

ImageFilter()应用于给定的过滤器filtertypeimage

参数

代码语言:txt
复制
`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,除了你可以指定颜色。使用arg1arg2arg3在形式redgreenbluearg4alpha通道。每种颜色的范围是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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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 →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com