Here's a better grayscale, sepia, and general tinting function. This function is better because:
1) Works with true color images (the other sepia code didn't).
2) Provides a more gooder grayscale conversion (yes, I said "more gooder"). The other grayscale code used imagetruecolortopalette, which just doesn't work well for grayscale conversion.
3) The other sepia code was really colorful, a little too much for my taste. This function allows you to optionally set the tinting of the grayscale to anything you wish.
4) Single function for grayscale, sepia, and any other tinting you can dream up.
Here's some examples:
imagegrayscaletint ($img); // Grayscale, no tinting
imagegrayscaletint ($img,304,242,209); // What I use for sepia
imagegrayscaletint ($img,0,0,255); // A berry blue image
The RGB values for tinting are normally from 0 to 255. But, you can use values larger than 255 to lighten and "burn" the image. The sepia example above does this a little, the below example provides a better example of lightening the image and burning the light areas out a little:
imagegrayscaletint ($img,400,400,400); // Lighten image
imagegrayscaletint ($img,127,127,127); // Darken image
<?
function imagegrayscaletint (&$img, $tint_r = 255, $tint_g = 255, $tint_b = 255) {
$width = imagesx($img); $height = imagesy($img);
$dest = imagecreate ($width, $height);
for ($i=0; $i<256; $i++) imagecolorallocate ($dest, $i, $i, $i);
imagecopyresized ($dest, $img, 0, 0, 0, 0, $width, $height, $width, $height);
for ($i = 0; $i < 256; $i++) imagecolorset ($dest, $i, min($i * abs($tint_r) / 255, 255), min($i * abs($tint_g) / 255, 255), min($i * abs($tint_b) / 255, 255));
$img = imagecreate ($width, $height);
imagecopy ($img, $dest, 0, 0, 0, 0, $width, $height);
imagedestroy ($dest);
}
?>