百科狗-知识改变命运!
--

imagefilter() - gd函数(图像处理)

梵高12个月前 (11-21)阅读数 19#技术干货
文章标签图像

imagefilter()

(PHP 5, PHP 7)

对图像使用过滤器

说明

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设定柔滑级别。

Note:此函数仅在与 GD 库捆绑编译的 PHP 版本中可用。

成功时返回TRUE,或者在失败时返回FALSE

imagefilter()灰度例子

imagefilter() - gd函数(图像处理)

imagefilter()亮度例子

imagefilter()上彩例子

参数

$image

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

$filtertype

$filtertypecan 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. Use$arg1to set the level of brightness.
  • IMG_FILTER_CONTRAST: Changes the contrast of the image. Use$arg1to set the level of contrast.
  • IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use$arg1,$arg2and$arg3in the form of$red,$blue,$greenand$arg4for the$alphachannel. 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. Use$arg1to set the level of smoothness.
  • IMG_FILTER_PIXELATE: Applies pixelation effect to the image, use$arg1to set the block size and$arg2to set the pixelation effect mode.
$arg1
  • IMG_FILTER_BRIGHTNESS: Brightness level.
  • IMG_FILTER_CONTRAST: Contrast level.
  • IMG_FILTER_COLORIZE:红色成分的值。
  • IMG_FILTER_SMOOTH: Smoothness level.
  • IMG_FILTER_PIXELATE: Block size in pixels.
$arg2
  • IMG_FILTER_COLORIZE:绿色成分的值。
  • IMG_FILTER_PIXELATE: Whether to use advanced pixelation effect or not(defaults to FALSE).
$arg3
  • IMG_FILTER_COLORIZE:蓝色成分的值。
$arg4
  • IMG_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.

范例

imagefilter() grayscale example

imagefilter() brightness example

imagefilter() colorize example

Example #7 imagefilter() negate example

Example #8 imagefilter() pixelate example

以上例程的输出类似于:

注释

Note:此函数仅在与 GD 库捆绑编译的 PHP 版本中可用。

参见

  • imageconvolution()用系数 div 和 offset 申请一个 3x3 的卷积矩阵
The documentation misses the exact meaning and valid ranges of the arguments for ImageFilter(). According to the 5.2.0 sources the arguments are:
IMG_FILTER_BRIGHTNESS
-255 = min brightness, 0 = no change, +255 = max brightness
IMG_FILTER_CONTRAST
-100 = max contrast, 0 = no change, +100 = min contrast (note the direction!)
IMG_FILTER_COLORIZE
Adds (subtracts) specified RGB values to each pixel. The valid range for each color is -255...+255, not 0...255. The correct order is red, green, blue.
-255 = min, 0 = no change, +255 = max
This has not much to do with IMG_FILTER_GRAYSCALE.
IMG_FILTER_SMOOTH
Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix).
any float is accepted, large value (in practice: 2048 or more) = no change
ImageFilter seem to return false if the argument(s) are out of range for the chosen filter.
I needed an especially strong blur effect today and had a hard time achieving adequate results with the built-in IMG_FILTER_GAUSSIAN_BLUR filter. In order to achieve the strength of the blur I required I had to repeat the filter up to 100 times, which took way too long to be acceptable.
After a bit of searching, I found this answer to be quite a good solution to this problem: http://stackoverflow.com/a/20264482
Based on that technique, I wrote the following generic function to achieve a very strong blur in a reasonable amount of processing: 
From what i have been able to find from this function, it accepts the following arguments:
    IMG_FILTER_NEGATE
    IMG_FILTER_GRAYSCALE
    IMG_FILTER_EDGEDETECT
    IMG_FILTER_GAUSSIAN_BLUR
    IMG_FILTER_SELECTIVE_BLUR
    IMG_FILTER_EMBOSS
    IMG_FILTER_MEAN_REMOVAL
The following arguments need one or more arguments.
    IMG_FILTER_SMOOTH, -1924.124
    IMG_FILTER_COLORIZE, -127.12, -127.98, 127
    IMG_FILTER_CONTRAST, -90
    IMG_FILTER_BRIGHTNESS, 98
    
I haven't tested them all, the names speak for themselves.
Here is an alternative to IMG_FILTER_COLORIZE filter, but taking the alpha parameter of each pixel in account. 
Function to change the transparency of a png image on the fly. Works only with PNG, and with a browser supporting alpha channel.
The function stretches the opacity-range of the image, so that the most opaque pixel(s) will be set to the given opacity. (Other opacity values in pixels are modified accordingly.)
Returns success or failure.

Example for use: 
For people looking to apply a 'multiply' effect on images like the one in Photoshop (generally b&w ones), you can achieve it with the IMG_FILTER_COLORIZE filter. 
a function to create nice vignette effect: 
a function to make all colors gray except the only one
i made it myself so the code is note so beautiful ) 
Note: applying IMG_FILTER_EMBOSS to text and using in a customization to the CAPTCHA image script in phpBB or a project of your own is a very good way to stop OCR-ing bots from getting through. Embossed serif fonts are fairly easy for the human eye to understand but to an OCR script it is extremely difficult because it seems to give it the illusion of 3D. 
If you only allocate 2 or 3 colours in the image, it uses the background colour alot in the embossed text, which greatly contributes to this.
I made my own custom CAPTCHA script to stop phpBB post spam for a client site I was developing and I have gone from getting 2-3 new spam users created every day to zero.
Anything with the source code freely available out there right now is possible to be defeated by spammers once one of them stars sharing code with the other spammers, but if you run something at least someone custom, their bots will pass you over.
Here's a page that shows the different filters in action
http://www.phpied.com/image-fun-with-php-part-2/
Also shows some quick ways to do sepia.
PHP Sepia Effect
 $myImage = imagecreatefromjpeg($f);
 imagefilter($myImage,IMG_FILTER_GRAYSCALE);
 imagefilter($myImage,IMG_FILTER_BRIGHTNESS,-30);
 imagefilter($myImage,IMG_FILTER_COLORIZE, 90, 55, 30); 
 header("Content-type: image/jpeg");
 imagejpeg($myImage );
 imagejpeg($myImage,$f);
 imagedestroy( $myImage );
filtertype is an integer. So if you want to use it as a variable and need to use, e.g. preg_match function you can do it in this way:

The order of the filtertypes in this manual determines the number of each filter, from 0 to 11. E.g. IMG_FILTER_NEGATE=0.
Simple pixelate function, just in case you are 
If you're looking for fast sepia effect that can be used for on-the-fly thumbnails generation you can't use sophisticated functions. The faster and much better way than described by webmaster at qudi dot de in the note from 31-Jan-2006 is applying colorize filter AFTER grayscale.

I used (90,60,40) for my sepia after couple of tests, however, if you need darker or lighter just check what suits you best.
IMG_FILTER_COLORIZE doesn't seem to work on palette image, here's a way to achieve same result with palette image:

Here's also a function that work on both truecolor and palette images that try to do something similar to greyscale with a given color

with hope that someone will find this useful
Searching for a way to easily change the color of the image, I tried IMG_FILTER_COLORIZE. I was unable to get the quality results I wanted. It turns out PHP's Colorize is the equivalent of Photoshop's "Linear Dodge" layer filter. 
Hue adjustments have always worked well for me, so I figured I could try with PHP.
This function is kind of slow on larger images, but on small images like what I'm using it for, the difference is trivial.
The script calculates the ratio or red, to green, to blue in the color provided, then scales the image appropriately... unfortunately, it does it pixel by pixel.
Here's a demo and comparison of this function, to photoshop's hue function, to PHP's colorize. http://img146.imageshack.us/img146/3167/imagefilterhuedemo.png 
Examples using imagefilter():

/////////////////////////////

///////////////////////////// 
This will only work if you have php5. For php4, you'll have to use the sepia function set webmaster at qudi dot de suggested.
This routine was just what I was looking for, I wanted web admin users to be able to recolour their uploaded photos (to go with a news item) either a blue tint or sepia to match the appearance of other colours used on the website. 
Using a form with a select box containing the RGB values, I can give them the option of either of the two tints or no colourization at all, plus resize their images to the viewing size and a thumbnail image on the fly without having to use any other image editing software.
for a quick, ok-looking, sepia-effect (also in php4) I just use this little fellow, since a real implementation of sepia was just way too slow.
function pseudosepia(&$im,$percent){
   $sx=imagesx($im);
   $sy=imagesy($im);
   $filter=imagecreatetruecolor($sx,$sy);
   $c=imagecolorallocate($filter,100,50,50);
   imagefilledrectangle($filter,0,0,$sx,$sy,$c);
   imagecopymerge($im,$filter,0,0,0,0,$sx,$sy,$percent);
}
http://www.hudzilla.org/phpbook/read.php/11_2_15
for more detailed info, and some arg guidelines.
A colorize algorithm wich preserves color luminosity (i.e black 
will output black, and white will output white).
This works in PHP4 and is great for customizing interfaces 
dinamically. 
I played with IMG_FILTER_SMOOTH and tried some negative
values.
-1 to -7: looks like a mix of smoothness and edgedetect
-8: image seems to be completely broken
-9 and lower: kind of sharpening effect (-9 sharper than -10)
I think the sharpening effect in particular could be useful.
// With transparent PNG file you can colorize the "positive" items and stand the transparent has it is - Beta code 
It appears that imagefilter doesn't play nice with apha. If you run an imagefilter on a transparent image it'll return a black image...similar to a lot of Photoshop plugins do.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)