我拿了
here中的Mehdi Karamosly在他的回答中提到的并修改了一些有点simipiler并做了一个简单的html输出,你可以修改你的输出喜欢.如果要添加安全性,可能需要添加更多数据检查以确保其文件/实际图像.我已经在我的计算机上检查了它并且它可以工作,并且希望它可以通过一些修改来满足您的需求.
原文链接:https://www.f2er.com/php/137237.htmlfunction graycolor($img){ list($width,$height) = getimagesize($img); $mime = pathinfo($img,PATHINFO_EXTENSION); switch($mime){#use appropriate function depending on image type case 'jpg': case 'jpeg': $im = imagecreatefromjpeg($img); break; case 'png': $im = imagecreatefrompng($img); break; case 'gif': $im = imagecreatefromgif($img); break; default: #invalid type exit; } if(!is_color($img,$im)){#$img = string; $im = resource string $ret = ""; for($i = 0; $i < $height; $i++){#loop height pixels for($ii = 0; $ii < $width; $ii++){#loop width pixels $color = imagecolorat($im,$ii,$i); $color = imagecolorsforindex($im,$color); $ret .= $color['red'] . " "; } $ret .= "\n"; } return $ret; }else{ #$ret = ""; #for($i = 0; $i < $height; $i++){#loop height pixels #for($ii = 0; $ii < $width; $ii++){#loop width pixels #$color = imagecolorat($im,$i); #$color = imagecolorsforindex($im,$color); #$ret .= $color['red'] . "," . $color['green'] . "," . $color['blue'] . " "; #} #$ret .= "\n"; #} return "color image"; } } function is_color($img,$im){ $times = 10;#number of times to check image for color $iscolor = false; list($width,$height) = getimagesize($img); for($i = 0 ; $i < $times && !$iscolor; $i++){ $color = imagecolorat($im,rand(0,$width),$height));#get random cords $color = imagecolorsforindex($im,$color);#get the random color's values if($color['red'] !== $color['green'] || $color['green'] !== $color['blue']){ $iscolor = true; break; } } return $iscolor; } echo graycolor('color.jpg');#outputs color image echo graycolor('gray.jpg');#outputs numbers