This PHP function uses the GD library to invert color or black and white
images. You can choose wether you want to save the negative of the image
or just display it. choose color or black and white output - works with
.jpg and .png images if your GD-Library supports it.
01<?php 02 03functioninvert_image($input,$output,$color=false,$type='jpeg') 04{ 05if($type=='jpeg')$bild=imagecreatefromjpeg($input); 06else$bild=imagecreatefrompng($input); 07 08$x=imagesx($bild); 09$y=imagesy($bild); 10 11for($i=0;$i<$y;$i++) 12{ 13for($j=0;$j<$x;$j++) 14{ 15$pos=imagecolorat($bild,$j,$i); 16$f=imagecolorsforindex($bild,$pos); 17if($color==true) 18{ 19$col=imagecolorresolve($bild,255-$f['red'],255-$f['green'],255-$f['blue']); 20}else{ 21$gst=$f['red']*0.15+$f['green']*0.5+$f['blue']*0.35; 22$col=imagecolorclosesthwb($bild,255-$gst,255-$gst,255-$gst); 23} 24imagesetpixel($bild,$j,$i,$col); 25} 26} 27if(empty($output))header('Content-type: image/'.$type); 28if($type=='jpeg')imagejpeg($bild,$output,90); 29elseimagepng($bild,$output); 30} 31 32$input='path/to/image.jpg'; 33 34// for a color negative image, set the optional flag 35invert_image($input,'',true); 36 37// for a black and withe negative image use like this 38// 39// invert_image($input,''); 40 41// if you want to save the output instead of just showing it, 42// set the output to the path where you want to save the inverted image 43// 44// invert_image('path/to/original/image.jpg','path/to/save/inverted-image.jpg'); 45 46// if you want to use png you have to set the color flag as 47// true or false and define the imagetype in the function call 48// 49// invert_image('path/to/image.png','',false,'png'); 50 51 52?>