Квартиры, дома, земельные участки Краснодарского края без посредников. Объявления собственников недвижимости.
Реклама здесь
Главная
Примеры PHP Примеры JavaScript Примеры Ajax Примеры CSS,HTML

Конвертирование изображения в черно-белое на PHP

Функция позволяет конвертировать цветное изображение в черно-белое.


<?php
#this file outputs a grey version of specified image
#use of this file:
#  in the image tag, <img border=0 src=greyimage.php3?src=imagesrc&col=colno >
# where imagesrc is the source of the original colour version
# where colno is 0 for grey, 1 for red, 2 green, 3 blue

function MakeColoursGrey($im,$col){
 
$total=ImageColorsTotal($im);
 for(
$i=0;$i<$total;$i++){
   
$old=ImageColorsForIndex($im,$i);
  
   
#trying to keep proper saturation when converting
   
$commongrey=(int)($old[red]+$old[green]+$old[blue])/3;
   if(!
$col){
    
ImageColorSet($im,$i,$commongrey,$commongrey,$commongrey);
   }elseif(
$col==1){
    
ImageColorSet($im,$i,$commongrey,0,0);
   }elseif(
$col==2){
    
ImageColorSet($im,$i,0,$commongrey,0);
   }elseif(
$col==3){
    
ImageColorSet($im,$i,0,0,$commongrey);
   }
 }
}

$img=imagecreatefromgif($src);

#change the colours to grey
MakeColoursGrey($img,$col);

#send the http header, this outputs an image of type gif
Header("Content-Type: image/gif");

#send the image
ImageGif($img);
?>
Скопировать в буфер

© Copyright 2008-2012 by KDG