imagefilter

(PHP 5, PHP 7, PHP 8)

imagefilter对图像使用过滤器

说明

imagefilter(GdImage $image, int $filter, array|int|float|bool ...$args): bool

imagefilter()image 上应用指定的过滤器 filter

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

filter

filter 可以是下列中的一个:

  • IMG_FILTER_NEGATE:将图像中所有颜色反转。
  • IMG_FILTER_GRAYSCALE: Converts the image into grayscale by changing the red, green and blue components to their weighted sum using the same coefficients as the REC.601 luma (Y') calculation. The alpha components are retained. For palette images the result may differ due to palette limitations.
  • IMG_FILTER_BRIGHTNESS:更改图像的亮度。使用 args 设置亮度级别。亮度的范围是 -255 到 255。
  • IMG_FILTER_CONTRAST:更改图像的对比度。使用 args 设置对比度级别。
  • IMG_FILTER_COLORIZE:与 IMG_FILTER_GRAYSCALE 类似,区别是可以指定颜色。使用 argsarg2arg3redgreenblue 的形式和 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:使图像更平滑。用 args 设定平滑级别。
  • IMG_FILTER_PIXELATE:对图像应用像素化效果,使用 args 设置块大小和 arg2 设置像素化效果模式。
  • IMG_FILTER_SCATTER:将散射效果应用于图像,使用 argsarg2 定义效果强度,另外使用 arg3 仅应用选定像素颜色。

args

  • IMG_FILTER_BRIGHTNESS:亮度级别。
  • IMG_FILTER_CONTRAST:对比度级别。
  • IMG_FILTER_COLORIZE: 红色成分的值。
  • IMG_FILTER_SMOOTH: Smoothness level.
  • IMG_FILTER_PIXELATE: Block size in pixels.
  • IMG_FILTER_SCATTER: Effect substraction level. This must not be higher or equal to the addition level set with arg2.

arg2

  • IMG_FILTER_COLORIZE: 绿色成分的值。
  • IMG_FILTER_PIXELATE:是否使用高级像素化效果(默认为 false)。
  • IMG_FILTER_SCATTER:效果添加级别。

arg3

  • IMG_FILTER_COLORIZE: 蓝色成分的值。
  • IMG_FILTER_SCATTER: Optional array indexed color values to apply effect at.

arg4

  • IMG_FILTER_COLORIZE: Alpha channel, A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource
7.4.0 新增散射支持(IMG_FILTER_SCATTER)。

范例

示例 #1 imagefilter() 灰度示例

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

示例 #2 imagefilter() 亮度示例

<?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.';
}
?>

示例 #3 imagefilter() 着色示例

<?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.';
}
?>

示例 #4 imagefilter() 反例

<?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.';
}
?>

示例 #5 imagefilter() 像素化示例

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

以上例程的输出类似于:

示例输出:imagefilter() 像素化

示例 #6 imagefilter() 散射示例

<?php
// Load the image
$logo = imagecreatefrompng('./php.png');

// Apply a very soft scatter effect to the image
imagefilter($logo, IMG_FILTER_SCATTER, 3, 5);

// Output the image with the scatter effect
header('Content-Type: image/png');
imagepng($logo);
imagedestroy($logo);
?>

以上例程的输出类似于:

Output of example : imagefilter() scatter

注释

注意: IMG_FILTER_SCATTER 的结果始终随机。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top