Php Programming Code Examples
Php > Graphics Code Examples
Grey Image with Coloured Mouse Over
Grey Image with Coloured Mouse Over
#####I have included the 2 functions MSFPhover and MSFPpreload so it works
#####without already having these declared elsewhere
#####you should also note that you will need the php gd extension library
#####on your server -- if you are sorted you can use this script with jpEg, gif
#####and png formats -- works fine with all
#####use: echo ImageGreyOver('image.gif','http://link') to define your image source
#####and its link -- the image should, if referenced in this manner, be in the
#####same directory as the two php files
#####NB:I have enclosed the main html-generating php file in html echoes to make it
#####easy to create a freestanding file for testing -- call this first part
#####file_something.php3
#####alternatively, strip the html echoes out
#####(NOT the echo ImageGreyOver('image.gif','http://link'))
#####and chuck in your script
#####HTH
<?
echo "<html>";
echo "<body>";
echo "<center>";
function ImageGreyOver($src,$href,$imageprops="border=0",$linkprops=""){
global $giop;
$giop++;
$greysrc="greyimggif.php3?src=$src";
$size=GetImageSize($src);
return "\n\n<script language=\"JavaScript\"><!--\n".
"MSFPhover =\n".
"(((navigator.appName == \"Netscape\") &&\n".
"(parseInt(navigator.appVersion) >= 3 )) ||\n".
"((navigator.appName == \"Microsoft Internet Explorer\") && \n".
"(parseInt(navigator.appVersion) >= 4 )));\n".
"function MSFPpreload(img) { var a=new Image(); a.src=img; return a; }\n".
"MSFPnav".$giop."n=MSFPpreload('$greysrc');".
"MSFPnav".$giop."h=MSFPpreload('$src'); ".
"// --></script>\n\n".
"<a href=".$href." $linkprops onmouseover=\"if(MSFPhover)
document['MSFPnav".$giop."'].src=MSFPnav".$giop."h.src\" onmouseout=\"if(MSFPhover)
document['MSFPnav".$giop."'].src=MSFPnav".$giop."n.src\"><IMG $size[3]
src=\"".$greysrc."\" $imageprops name=\"MSFPnav".$giop."\"></a>";
}
echo ImageGreyOver('image.gif','http://link');
echo "</center>";
echo "</body>";
echo "</html>";
?>
#####save as file greyimggif.php3 in same directory
<?
#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);
?>
###### end of greyimg.php3