Home » Php » Convert an image into digits with PHP?

Convert an image into digits with PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to figure out how I can convert a color image into numbers.

For example, if I have an image like the one shown in figure (b), how can I get a output like figure (a) in to a text file?

enter image description here

How to&Answers:

I took what was in here which Mehdi Karamosly mentioned in his answer and modified it some to be a little bit simipiler and made a simple html output which you can modify to your outputs liking. You might want to add more data checking to make sure its a file/real image if you are looking to add security. I’ve checked it out on my computer and it works and hopefully it will work for what you need with some 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 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), rand(0, $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

Answer:

Now you are talking about gray level image, if you want to do that to a colored image each pixel would have 3 numbers from 0 to 255. (red: 0-255, green: 0-255, blue : 0-255)

If you are interested in grey scale image one important rule you would need to know is :

// A Pixel is Greyscale if the r = B = G

based on this post.

Take a look at this php function:

http://php.net/manual/en/function.imagecolorat.php