header('Content-type: image/jpeg');
$source_file = isset($_REQUEST['path']) ? $_REQUEST['path'] : "index_01.jpg";
$imgname='hui/'.$source_file;//所有灰色图片保存在"hui"文件夹中
//如果已经生成灰色图片直接输出
if(file_exists($imgname)){
$im = @imagecreatefromjpeg($imgname);
imagejpeg($im);
}else{
$im = ImageCreateFromJpeg($source_file);
$imgw = imagesx($im);
$imgh = imagesy($im);
for ($i=0; $i
{
for ($j=0; $j
{
// get the rgb value for current pixel
$rgb = ImageColorAt($im, $i, $j);
// extract each value for r, g, b
$rr = ($rgb >> 16 ) & 0xFF;
$gg = ($rgb >> 8 ) & 0xFF;
$bb = $rgb & 0xFF;
// get the Value from the RGB value
$g = round(($rr + $gg + $bb) / 3);
// grayscale values have r=g=b=g
$val = imagecolorallocate($im, $g, $g, $g);
// set the gray value
imagesetpixel ($im, $i, $j, $val);
}
}
imagejpeg($im,$imgname);//生成灰色图片
$im = @imagecreatefromjpeg($imgname);//读取生成的灰色图片
imagejpeg($im);
}
?>