PHP 获取图片RGB
最近有点忙,在研究图片方面领域发现PHP在图像处理方面 能力有点弱,然后 写了 点基本的功能用来完善PHP 的库
获取方法有2 中 第一种也比较简单
$imagesize = getimagesize("自由网.jpg");//获取文件大小
$imageinfo = imagecreatefromjpeg("自由网.jpg");//获取图片
$RGB =array();
for($h=0; $h <$imagesize['1'] ; $h++) //循环高
{
for ($w=0; $w <$imagesize['0'] ; $w++)
{
$temp_rgb = imagecolorat($imageinfo,$w,$h);//获得某像素的颜色索引值
$RGB[$h][$w] = imagecolorsforindex($imageinfo,$temp_rgb);//取得某索引的颜色s
}
}
方法二
$imagesize = getimagesize("自由网.jpg");//获取文件大小
$img= imagecreatefromjpeg("自由网.jpg");//获取图片
$x = $imagesize['1'] ;
$y =$imagesize['0'] ;
for($y = 0; $y < $h; $y++) {
for($x = 0; $x < $w; $x++) {
$rgb = imagecolorat($img, $x, $y);
$r += $rgb >> 16;
$g += $rgb >> 8 & 255;
$b += $rgb & 255;
$RGB[$h][$w] = array($r,$g,$b);
}
}
RGB转图像
$im = imagecreatetruecolor(1500,700); //生成真彩图片
$black = imagecolorallocate($im,220,230,250);//设置颜色
imagefill($im,10,1,$black) ; //填充 // 从左上角开始填充灰色//背景
foreach ($RGB as $key => $value) {
foreach ($value as $keys => $values) {
$R = imagecolorallocate($im,$values['red'],$values['green'],$values['blue']);//设置颜色
ImageFilledRectangle($im, $keys,$key, $keys,$key, $R);//绘制图案
}
}
header("Content-type:image/png");//png格式
imagepng($im);//输出
imagedestroy($im);//释放内存