1

I have seen several websites where if you upload an image and an identical image already exists on there servers they will reject the submission. Using PNGs is there an easy way to check one image against a massive folder of images?

http://www.imagemagick.org/discourse-server/viewtopic.php?t=12618

I did find this with imagemagick, but I am looking for one vs many and not one to one a million

Case
  • 4,244
  • 5
  • 35
  • 53

3 Answers3

2

You can transform the file content into a sha1. That will give you a way to identify two pictures strictly identical.

see http://php.net/manual/fr/function.sha1-file.php

Then after you save it into a NFS, or use some kind of database to test if the hash already exists.

  • It would also be good to rename your image using the hash so that each image has a unique name. You can then also save the original image name in a database and when downloading the image, rename it back to the original name – Allen Butler May 17 '16 at 12:32
0

Details of the images are probably maintained in a database; while the images are stored in the filesystem. And that database probably has a hash column which is used to store an md5 hash of the image file itself, calculated when the image is first uploaded. When a new image is uploaded, it calculates the hash for that image, and then checks to see if any other image detail in the database has a matching hash. If not, it stores the newly uploaded image with that hash; otherwise it can respond with details of the previous upload. If the hash column is indexed in the table, then this check is pretty quick.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
-1

If I understood your question correctly. You want to find out if a specific image exists in a Directory with so many images, right? If so, take a look at the solution:

    <?php

        // CREATE A FUNCTION WHICH RETURNS AN ARRAY OF ALL IMAGES IN A SPECIFIC FOLDER
        function getAllImagesInFolder($dir_full_path){
            $returnable     = array();
            $files_in_dir   = scandir($dir_full_path);
            $reg_fx         = '#(\.png|\.jpg|\.bmp|\.gif|\.jpeg)#';
            foreach($files_in_dir as $key=>$val){
                $temp_file_or_dir = $dir_full_path . DIRECTORY_SEPARATOR . $val;
                if(is_file($temp_file_or_dir) && preg_match($reg_fx,  $val) ){
                    $regx_dot_wateva            = '/\.{2,4}$/';
                    $regx_dot                   = '/\./';
                    $regx_array                 = array($regx_dot_wateva, $regx_dot);
                    $replace_array              = array("", "_");
                    $return_val                 = preg_replace($regx_array, $replace_array, $val);
                    $returnable[$return_val]    = $temp_file_or_dir ;
                }else if(is_dir($temp_file_or_dir) && !preg_match('/^\..*/', $val) ){
                    getFilesInFolder($temp_file_or_dir);
                }
            }
            return $returnable;
        }

        // CREATE ANOTHER FUNCTION TO CHECK IF THE SPECIFIED IMAGE EXISTS IN THE GIVEN DIRECTORY.
        // THE FIRST PARAMETER SHOULD BE THE RESULT OF CALLING THE PREVIOUS FUNCTION: getAllImagesInFolder(...)
        // THE SECOND PARAMETER IS THE IMAGE YOU WANT TO SEARCH WHETHER IT EXISTS IN THE SAID FOLDER OR NOT
        function imageExistsInFolder($arrImagesInFolder, $searchedImage){
            if(!is_array($arrImagesInFolder) && count($arrImagesInFolder) < 1){
                return false;
            }
            foreach($arrImagesInFolder as $strKey=>$imgPath){
                if(stristr($imgPath, $searchedImage)){
                    return true;
                }
            }
            return false;
        }

        // NOW GET ALL THE IMAGES IN A SPECIFIED FOLDER AND ASSIGN THE RESULTING ARRAY TO A VARIABLE: $imgFiles
        $imgFolder      = "/path/to/directory/where/there/are/images";
        $arrImgFiles    = getAllImagesInFolder($imgFolder);
        $searchedImage  = "sandwich.jpg";       //<== OR EVEN WITHOUT THE EXTENSION, JUST "sandwich"

        // ASSUMING THE SPECIFIC IMAGE YOU WANT TO MATCH IS CALLED sandwich.jpg
        // YOU CAN USE THE imageExistsInFolder(...) FUNCTION TO RETURN A BOOLEAN FLAG OF true OR false
        // DEPENDING ON IF IT DOES OR NOT.

        var_dump($arrImgFiles);
        var_dump( imageExistsInFolder($arrImgFiles, $searchedImage) );
Poiz
  • 7,611
  • 2
  • 15
  • 17