1

i'm having trouble with a script i modified, i used this class https://github.com/thenakos/compare-images since i wanted to check if in a determined folder there were only uniques photos.

public function scanDir($d)
{
    /*function to find same photos in a dir*/
    $tabImg = array();
    $bitsList = array();

    if(is_dir($d))
    {

        $dir = opendir($d); 
        $i = 0;

        while($file = readdir($dir))
        {

            $path_parts = pathinfo($file);

            if($file != '.' && $file != '..' && isset($path_parts['extension']) && $path_parts['extension'] == 'jpg')
            {

                $tabImg[] = $file;
                $i++;

            }

        }

    }
    $i=0;
    foreach($tabImg as $keyA => $imgA)
    {
        if($i<700) {
        if(file_exists($d.$imgA))
        {

            $i1 = $this->createImage($d.$imgA);

            if(!$i1){return false;}

            $i1 = $this->resizeImage($i1,$d.$imgA);

            imagefilter($i1, IMG_FILTER_GRAYSCALE);

            $colorMean1 = $this->colorMeanValue($i1);

            $bits1 = $this->bits($colorMean1);

            $bitsList[$keyA] = $bits1;

            imagedestroy($i1);
            $i++;
        }
        }
    }

    $bitsListToCompare = $bitsList;

    foreach($bitsList as $keyList => $valueList)
    {

        foreach($bitsListToCompare as $keyListToCompare => $valueListToCompare)
        {

            if($keyList != $keyListToCompare)
            {

                $hammeringDistance = 0;

                for($b = 0;$b<64;$b++)
                {

                    if($valueList[$b] != $valueListToCompare[$b])
                    {
                        $hammeringDistance++;
                    }

                }

                if($hammeringDistance < 5)
                {

                    if(isset($arraySame[$tabImg[$keyList]])) $arraySame[$tabImg[$keyList]] = $arraySame[$a[$keyList]].';'.$tabImg[$keyListToCompare]; else $arraySame[$tabImg[$keyList]] = $tabImg[$keyListToCompare];

                }

            }

        }

        unset($bitsListToCompare[$keyList]);

    }

    return $arraySame;

}

i've added this function wich basically returns an array of duplicates images. This way it works fine, i'm checking 700 images. But if i don't limit the number of photos to check, i'm getting an error.

Warning: getimagesize() [function.getimagesize]: Read error!

This error is about the following function ( getimagesize )

private function mimeType($i)
{
    /*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/
    $mime = getimagesize($i);
    $return = array($mime[0],$mime[1]);

    switch ($mime['mime'])
    {
        case 'image/jpeg':
            $return[] = 'jpg';
            return $return;
        case 'image/png':
            $return[] = 'png';
            return $return;
        default:
            return false;
    }
}  

i think it's something about the memory but i don't know how to make it work !

Thanks

ToniZ
  • 21
  • 1

1 Answers1

0

As for memory - this line seems suspicious:

$i1 = $this->resizeImage($i1,$d.$imgA);

I don't know what's inside resizeImage() but it could be that it takes one GD resource as first argument, doesn't destroy it and returns another GD resource. Reference to the new resource replaces reference to the old resource that stays in memory. While resource without references to it will be eventually freed by garbage collector, it's not guaranteed it will do it in time.

So I would do:

$i2 = $this->resizeImage($i1,$d.$imgA);
imagedestroy($i1);

But there may be simpler reason. As PHP manual states on getimagesize():

If accessing the filename image is impossible getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.

Then in changelog:

5.2.3 Read errors generated by this function downgraded to E_NOTICE from E_WARNING.

So perhaps some photo has permission issue or something like that?

Forseti
  • 2,587
  • 6
  • 21
  • 32