imagefilter

(PHP 5)

imagefilter -- 对图像使用过滤器

说明

bool imagefilter ( resource src_im, int filtertype [, int arg1 [, int arg2 [, int arg3]]] )

imagefilter() 把过滤器 filtertype 应用到图像上,在需要时使用 arg1arg2arg3

filtertype 可以是下列中的一个:

  • IMG_FILTER_NEGATE:将图像中所有颜色反转。

  • IMG_FILTER_GRAYSCALE:将图像转换为灰度的。

  • IMG_FILTER_BRIGHTNESS:改变图像的亮度。用 arg1 设定亮度级别。

  • IMG_FILTER_CONTRAST:改变图像的对比度。用 arg1 设定对比度级别。

  • IMG_FILTER_COLORIZE:与 IMG_FILTER_GRAYSCALE 类似,不过可以指定颜色。用 arg1arg2arg3 分别指定 redbluegreen。每种颜色范围是 0 到 255。

  • IMG_FILTER_EDGEDETECT:用边缘检测来突出图像的边缘。

  • IMG_FILTER_EMBOSS:使图像浮雕化。

  • IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊图像。

  • IMG_FILTER_SELECTIVE_BLUR:模糊图像。

  • IMG_FILTER_MEAN_REMOVAL:用平均移除法来达到轮廓效果。

  • IMG_FILTER_SMOOTH:使图像更柔滑。用 arg1 设定柔滑级别。

注: 本函数仅在 PHP 与其捆绑的 GD 库一起编译时可用。

如果成功则返回 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);
?>


add a note add a note User Contributed Notes
nancy at hypertextdigital dot com
04-Aug-2006 05:16
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.
santibari at fibertel dot com
28-Feb-2006 03:37
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.

<?php
function colorize($img_src,$img_dest, $r, $g, $b)
{
if(!
$im = imagecreatefromgif($img_src))
  return
"Could not use image $img_src";
  
//We will create a monochromatic palette based on
//the input color
//which will go from black to white
//Input color luminosity: this is equivalent to the
//position of the input color in the monochromatic
//palette
$lum_inp=round(255*($r+$g+$b)/765); //765=255*3

//We fill the palette entry with the input color at its
//corresponding position

$pal[$lum_inp]['r']=$r;
$pal[$lum_inp]['g']=$g;
$pal[$lum_inp]['b']=$b;

//Now we complete the palette, first we'll do it to
//the black,and then to the white.

//FROM input to black
//===================
//how many colors between black and input
$steps_to_black=$lum_inp;       

//The step size for each component
if($steps_to_black)
 {
 
$step_size_red=$r/$steps_to_black;   
 
$step_size_green=$g/$steps_to_black;   
 
$step_size_blue=$b/$steps_to_black;   
 }

for(
$i=$steps_to_black;$i>=0;$i--)
 {
 
$pal[$steps_to_black-$i]['r']=$r-round($step_size_red*$i);
 
$pal[$steps_to_black-$i]['g']=$g-round($step_size_green*$i);
 
$pal[$steps_to_black-$i]['b']=$b-round($step_size_blue*$i);
 }

//From input to white:
//===================
//how many colors between input and white
$steps_to_white=255-$lum_inp;

if(
$steps_to_white)
  {
 
$step_size_red=(255-$r)/$steps_to_white;   
 
$step_size_green=(255-$g)/$steps_to_white;   
 
$step_size_blue=(255-$b)/$steps_to_white;   
  }
 else
 
$step_size_red=$step_size_green=$step_size_blue=0;

 
//The step size for each component
 
for($i=($lum_inp+1);$i<=255;$i++)
  {
 
$pal[$i]['r']=$r + round($step_size_red*($i-$lum_inp));
 
$pal[$i]['g']=$g + round($step_size_green*($i-$lum_inp));
 
$pal[$i]['b']=$b + round($step_size_blue*($i-$lum_inp));
  }
//--- End of palette creation

//Now,let's change the original palette into the one we
//created
for($c = 0; $c < $palette_size; $c++)
 {
 
$col = imagecolorsforindex($im, $c);         
 
$lum_src=round(255*($col['red']+$col['green']
               +
$col['blue'])/765);
 
$col_out=$pal[$lum_src];
 
imagecolorset($im, $c, $col_out['r'],
                              
$col_out['g'],
                              
$col_out['b']);
 }

 
//save the image file
 
imagepng($im,$img_dest);
 
imagedestroy($im);
}
//end function colorize
?>
webmaster at qudi dot de
31-Jan-2006 10:53
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);
}
a php user at nowhere dot com
21-Dec-2005 04:48
http://www.hudzilla.org/phpbook/read.php/11_2_15
for more detailed info, and some <i>arg</i> guidelines.
vdepizzol at hotmail dot com
05-Sep-2004 04:36
Examples using 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);
?>

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

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

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

<?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);
?>
kees at tweakers dot net
21-Jul-2004 09:26
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.