Search code examples
phpandroidandroid-imageuniqueidentifier

How to uniquely identify an image, created by Android camera?


I'm building my own image backup app, with PHP and Nativescript. The app will be simple, and will allow me to manually/automatically upload any image from my camera, to a server.

The problem is, that i need to somehow "identify" each image, to let the app know if a specific image has been backed-up already. This should take care of a scenario, where i manually move the images from one phone to a new one, and then install the app on that new phone again. How would my backend recognize, that this picture was already backed-up?, I don't think relying on the name is a good idea. What i have done so far is this:

I create a string, that is a combination of the image size and name:

public function createFileIdentifier(String $fileSize, String $fileName): String
    {
        return '_' . $fileSize . '_' . $fileName;
    }  

Everytime an image is uploaded, i use this to check if such an image "already exists". This would fail if two images have the same name, for example.

Is there any meta-data created by Android for each picture the camera takes, that is 100% unique, which i could use? Any other ideas?


Solution

  • You could create a hash of the image file, and use that to uniquely identify the content of the image. Even a MD5 hash would do in this situation, and it is quite quick.

    public function createFileIdentifier(String $fileName): String
    {
       // read only the first megabyte
       $content = file_get_contents($fileName, FALSE, 0, 1024*1024);
       // make a simple hash
       return md5($content);
    }  
    

    MD5 hashes are virtually guaranteed to be unique, as long as the part of the image you hash differs.