600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php 图片 变灰色 php[图片变灰]生成灰色图片代码

php 图片 变灰色 php[图片变灰]生成灰色图片代码

时间:2023-05-05 07:10:29

相关推荐

php 图片 变灰色 php[图片变灰]生成灰色图片代码

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);

}

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。