Is there any code I can use to change a colour image on a web page to a black & white one, without having to have 2 of the same images on our server?
<?php // The file you are grayscaling $file = 'yourfile.jpg'; // This sets it to a .jpg, but you can change this to png or gif if that is what you are working with header('Content-type: image/jpeg'); // Get the dimensions list($width, $height) = getimagesize($file); // Define our source image $source = imagecreatefromjpeg($file); // Creating the Canvas $bwimage= imagecreate($width, $height); //Creates the 256 color palette for ($c=0;$c<256;$c++) { $palette[$c] = imagecolorallocate($bwimage,$c,$c,$c); } //Creates yiq function function yiq($r,$g,$b) { return (($r*0.299)+($g*0.587)+($b*0.114)); } //Reads the origonal colors pixel by pixel for ($y=0;$y<$height;$y++) { for ($x=0;$x<$width;$x++) { $rgb = imagecolorat($source,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette $gs = yiq($r,$g,$b); imagesetpixel($bwimage,$x,$y,$palette[$gs]); } } // Outputs a jpg image, but you can change this to png or gif if that is what you are working with imagejpeg($bwimage); ?> PHP: Got this from here. http://php.about.com/od/gdlibrary/ss/grayscale_gd.htm
So, as an example, where or how would I put the code in a page like this? http://www.photosales.co.nz/details.php?gid=62&pid=5050 It would be nice to have a button for people to click to view as greyscale.