function convert2grayscale ($image
{
$height = imagesy$image);
$width = imagesx$image);
$tc = imageistruecolor$image);
$new_image = imagecreatetruecolor$width, $height);
imagealphablending$new_image, false);
imagesavealpha$new_image, true);
for ($ii=0; $ii$width; $ii++) {
for ($jj=0; $jj$height; $jj++) {
$rgb = imagecolorat$image, $ii, $jj);
if ($tc) {
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb >> 24) & 0xFF;
} else {
$rgb = imagecolorsforindex$image, $rgb);
$r = $rgb'red'];
$g = $rgb'green'];
$b = $rgb'blue'];
$alpha = $rgb'alpha'];
}
$avg = round(($r$g$b)/3);
$color = imagecolorallocatealpha$new_image, $avg, $avg, $avg, $alpha);
imagesetpixel$new_image, $ii, $jj, $color);
}
}
return $new_image
}