Convert an image into digits with PHP? -
i trying figure out how can convert color image
numbers.
for example, if have image 1 shown in figure (b)
, how can output figure (a)
in text file?
i took in here mehdi karamosly mentioned in answer , modified little bit simipiler , made simple html output can modify outputs liking. might want add more data checking make sure file/real image if looking add security. i've checked out on computer , works , work need modification(s).
function 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, $ii, $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 check image color $iscolor = false; list($width, $height) = getimagesize($img); for($i = 0 ; $i < $times && !$iscolor; $i++){ $color = imagecolorat($im, rand(0, $width), rand(0, $height));#get random cords $color = imagecolorsforindex($im, $color);#get 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
Comments
Post a Comment