imagefilter 对图像使用过滤器
发表日期:2021-07-01 08:55:59 | 来源: | | 浏览(917) 分类:GD 和图像处理 函数
imagefilter
(PHP 5, PHP 7, PHP 8)
imagefilter — 对图像使用过滤器
说明
imagefilter(
resource $src_im,
int $filtertype,
int $arg1 = ?,
int $arg2 = ?,
int $arg3 = ?
): bool
imagefilter() 把过滤器 filtertype 应用到图像上,在需要时使用 arg1,arg2 和 arg3。
filtertype 可以是下列中的一个:
IMG_FILTER_NEGATE:将图像中所有颜色反转。IMG_FILTER_GRAYSCALE:将图像转换为灰度的。IMG_FILTER_BRIGHTNESS:改变图像的亮度。用arg1设定亮度级别。IMG_FILTER_CONTRAST:改变图像的对比度。用arg1设定对比度级别。IMG_FILTER_COLORIZE:与IMG_FILTER_GRAYSCALE类似,不过可以指定颜色。用arg1,arg2和arg3分别指定red,blue和green。每种颜色范围是 0 到 255。IMG_FILTER_EDGEDETECT:用边缘检测来突出图像的边缘。IMG_FILTER_EMBOSS:使图像浮雕化。IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊图像。IMG_FILTER_SELECTIVE_BLUR:模糊图像。IMG_FILTER_MEAN_REMOVAL:用平均移除法来达到轮廓效果。IMG_FILTER_SMOOTH:使图像更柔滑。用arg1设定柔滑级别。
成功时返回 true, 或者在失败时返回 false。
示例 #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');
}
else {
echo 'Image brightness change failed.';
}
imagedestroy($im);
?>示例 #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');
}
else {
echo 'Green shading failed.';
}
imagedestroy($im);
?>参数
image由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
filtertypefiltertypecan be one of the following:IMG_FILTER_NEGATE: Reverses all colors of the image.IMG_FILTER_GRAYSCALE: Converts the image into grayscale.IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Usearg1to set the level of brightness.IMG_FILTER_CONTRAST: Changes the contrast of the image. Usearg1to set the level of contrast.IMG_FILTER_COLORIZE: LikeIMG_FILTER_GRAYSCALE, except you can specify the color. Usearg1,arg2andarg3in the form ofred,blue,greenandarg4for thealphachannel. The range for each color is 0 to 255.IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.IMG_FILTER_EMBOSS: Embosses the image.IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.IMG_FILTER_SELECTIVE_BLUR: Blurs the image.IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a "sketchy" effect.IMG_FILTER_SMOOTH: Makes the image smoother. Usearg1to set the level of smoothness.IMG_FILTER_PIXELATE: Applies pixelation effect to the image, usearg1to set the block size andarg2to set the pixelation effect mode.arg1IMG_FILTER_BRIGHTNESS: Brightness level.IMG_FILTER_CONTRAST: Contrast level.IMG_FILTER_COLORIZE: 红色成分的值。IMG_FILTER_SMOOTH: Smoothness level.IMG_FILTER_PIXELATE: Block size in pixels.arg2IMG_FILTER_COLORIZE: 绿色成分的值。IMG_FILTER_PIXELATE: Whether to use advanced pixelation effect or not (defaults tofalse).arg3IMG_FILTER_COLORIZE: 蓝色成分的值。arg4IMG_FILTER_COLORIZE: Alpha channel, A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 | Pixelation support (IMG_FILTER_PIXELATE) was added. |
| 5.2.5 | Alpha support for IMG_FILTER_COLORIZE was added. |
范例
示例 #4 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);
?>示例 #5 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.';
}
?>示例 #6 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.';
}
?>示例 #7 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.';
}
?>示例 #8 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 3imagefilter($logo1, IMG_FILTER_PIXELATE, 3);
imagefilter($logo2, IMG_FILTER_PIXELATE, 3, true);
// Merge the differences onto the output imageimagecopy($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 differencesheader('Content-Type: image/png');
imagepng($output);
imagedestroy($output);
?>以上例程的输出类似于:
注释
参见
imageconvolution() - 用系数 div 和 offset 申请一个 3x3 的卷积矩阵
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- gd_info 取得当前安装的 GD 库的信息(0)
- getimagesize 取得图像大小(0)
- getimagesizefromstring 从字符串中获取图像尺寸信息(0)
- image_type_to_extension 取得图像类型的文件后缀(0)
- image_type_to_mime_type 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型(0)
- image2wbmp 以 WBMP 格式将图像输出到浏览器或文件(0)
- imageaffine 返回经过仿射变换后的图像,剪切区域可选(0)
- imageaffinematrixconcat Concatenate two affine transformation matrices(0)
- imageaffinematrixget Get an affine transformation matrix(0)
- imagealphablending 设定图像的混色模式(0)
- imageantialias 是否使用抗锯齿(antialias)功能(0)
- imagearc 画椭圆弧(0)
- imagebmp Output a BMP image to browser or file(0)
- imagechar 水平地画一个字符(0)
- imagecharup 垂直地画一个字符(0)
- imagecolorallocate 为一幅图像分配颜色(0)
- imagecolorallocatealpha 为一幅图像分配颜色 + alpha(0)
- imagecolorat 取得某像素的颜色索引值(0)
- imagecolorclosest 取得与指定的颜色最接近的颜色的索引值(0)
- imagecolorclosestalpha 取得与指定的颜色加透明度最接近的颜色(0)
- imagecolorclosesthwb 取得与给定颜色最接近的色度的黑白色的索引(0)
- imagecolordeallocate 取消图像颜色的分配(0)
- imagecolorexact 取得指定颜色的索引值(0)
- imagecolorexactalpha 取得指定的颜色加透明度的索引值(0)
- imagecolormatch 使一个图像中调色板版本的颜色与真彩色版本更能匹配(0)
- imagecolorresolve 取得指定颜色的索引值或有可能得到的最接近的替代值(0)
- imagecolorresolvealpha 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值(0)
- imagecolorset 给指定调色板索引设定颜色(0)
- imagecolorsforindex 取得某索引的颜色(0)
- imagecolorstotal 取得一幅图像的调色板中颜色的数目(0)
- imagecolortransparent 将某个颜色定义为透明色(0)
- imageconvolution 用系数 div 和 offset 申请一个 3x3 的卷积矩阵(0)
- imagecopy 拷贝图像的一部分(0)
- imagecopymerge 拷贝并合并图像的一部分(0)
- imagecopymergegray 用灰度拷贝并合并图像的一部分(0)
- imagecopyresampled 重采样拷贝部分图像并调整大小(0)
- imagecopyresized 拷贝部分图像并调整大小(0)
- imagecreate 新建一个基于调色板的图像(0)
- imagecreatefrombmp 由文件或 URL 创建一个新图象。(0)
- imagecreatefromgd2 从 GD2 文件或 URL 新建一图像(0)
- imagecreatefromgd2part 从给定的 GD2 文件或 URL 中的部分新建一图像(0)
- imagecreatefromgd 从 GD 文件或 URL 新建一图像(0)
- imagecreatefromgif 由文件或 URL 创建一个新图象。(0)
- imagecreatefromjpeg 由文件或 URL 创建一个新图象。(0)
- imagecreatefrompng 由文件或 URL 创建一个新图象。(0)
- imagecreatefromstring 从字符串中的图像流新建一图像(0)
- imagecreatefromwbmp 由文件或 URL 创建一个新图象。(0)
- imagecreatefromwebp 由文件或 URL 创建一个新图象。(0)
- imagecreatefromxbm 由文件或 URL 创建一个新图象。(0)
- imagecreatefromxpm 由文件或 URL 创建一个新图象。(0)
- imagecreatetruecolor 新建一个真彩色图像(0)
- imagecrop Crop an image to the given rectangle(0)
- imagecropauto Crop an image automatically using one of the available modes(0)
- imagedashedline 画一虚线(0)
- imagedestroy 销毁一图像(0)
- imageellipse 画一个椭圆(0)
- imagefill 区域填充(0)
- imagefilledarc 画一椭圆弧且填充(0)
- imagefilledellipse 画一椭圆并填充(0)
- imagefilledpolygon 画一多边形并填充(0)
- imagefilledrectangle 画一矩形并填充(0)
- imagefilltoborder 区域填充到指定颜色的边界为止(0)
- imagefilter 对图像使用过滤器(0)
- imageflip Flips an image using a given mode(0)
- imagefontheight 取得字体高度(0)
- imagefontwidth 取得字体宽度(0)
- imageftbbox 给出一个使用 FreeType 2 字体的文本框(0)
- imagefttext 使用 FreeType 2 字体将文本写入图像(0)
- imagegammacorrect 对 GD 图像应用 gamma 修正(0)
- imagegd2 将 GD2 图像输出到浏览器或文件(0)
- imagegd 将 GD 图像输出到浏览器或文件(0)
- imagegetclip Get the clipping rectangle(0)
- imagegetinterpolation Get the interpolation method(0)
- imagegif 输出图象到浏览器或文件。(0)
- imagegrabscreen Captures the whole screen(0)
- imagegrabwindow Captures a window(0)
- imageinterlace 启用或禁用隔行扫描(0)
- imageistruecolor 检查图像是否为真彩色图像(0)
- imagejpeg 输出图象到浏览器或文件。(0)
- imagelayereffect 设定 alpha 混色标志以使用绑定的 libgd 分层效果(0)
- imageline 画一条线段(0)
- imageloadfont 载入一新字体(0)
- imageopenpolygon Draws an open polygon(0)
- imagepalettecopy 将调色板从一幅图像拷贝到另一幅(0)
- imagepalettetotruecolor Converts a palette based image to true color(0)
- imagepng 以 PNG 格式将图像输出到浏览器或文件(0)
- imagepolygon 画一个多边形(0)
- imagerectangle 画一个矩形(0)
- imageresolution Get or set the resolution of the image(0)
- imagerotate 用给定角度旋转图像(0)
- imagesavealpha 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)(0)
- imagescale Scale an image using the given new width and height(0)
- imagesetbrush 设定画线用的画笔图像(0)
- imagesetclip Set the clipping rectangle(0)
- imagesetinterpolation Set the interpolation method(0)
- imagesetpixel 画一个单一像素(0)
- imagesetstyle 设定画线的风格(0)
- imagesetthickness 设定画线的宽度(0)
- imagesettile 设定用于填充的贴图(0)
- imagestring 水平地画一行字符串(0)
- imagestringup 垂直地画一行字符串(0)
- imagesx 取得图像宽度(0)
- imagesy 取得图像高度(0)
- imagetruecolortopalette 将真彩色图像转换为调色板图像(0)
- imagettfbbox 取得使用 TrueType 字体的文本的范围(0)
- imagettftext 用 TrueType 字体向图像写入文本(0)
- imagetypes 返回当前 PHP 版本所支持的图像类型(0)
- imagewbmp 以 WBMP 格式将图像输出到浏览器或文件(0)
- imagewebp 将 WebP 格式的图像输出到浏览器或文件(0)
- imagexbm 将 XBM 图像输出到浏览器或文件(0)
- iptcembed 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中(0)
- iptcparse 将二进制 IPTC 块解析为单个标记(0)
- jpeg2wbmp 将 JPEG 图像文件转换为 WBMP 图像文件(0)
- png2wbmp 将 PNG 图像文件转换为 WBMP 图像文件(0)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号