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 可以是下列中的一个:

args

arg2

arg3

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 的结果始终随机。

参见