Solution: Error getimagesize():

Development | Web Development
Author
Description
I get getimagesize(): error:

My code:

 $image = 'https://exampleurl.com';
 if ( @getimagesize($image)) {} 
                else {
                    $image='';
                }
                if(!empty($image)){
                $image1 = 'noImage.jpg';
                $compareMachine = new compareImages($image1);
                $image1Hash = $compareMachine->getHasString(); 
                $image2_url = $image;
                $image2 = 'current_image_url.jpg';
                file_put_contents($image2, $this->get_page($image2_url));
                $image2Hash = $compareMachine->hasStringImage($image2); 
                $diff = $compareMachine->compareHash($image2Hash); 
                echo 'Different rates (orginalImage Vs currentImageUrl): '.$diff;
                if($diff>11){
                    echo ' => 2 different image';
                }
                else{
                    echo ' => duplicate image';
                    $image='';
                }
            }
Attachments
No attachments.
Solutions
Bull shit code: Fatal error: Class 'compareImages' not found in /file.php  on line 12

Use my code for compare images:

<?php
class compareImages
{
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;
}
}  
    
private function createImage($i)
{
/*retuns image resource or false if its not jpg or png*/
$mime = $this->mimeType($i);
      
if($mime[2] == 'jpg')
{
return imagecreatefromjpeg ($i);

else if ($mime[2] == 'png') 
{
return imagecreatefrompng ($i);

else 
{
return false; 

}
    
private function resizeImage($i,$source)
{
/*resizes the image to a 8x8 squere and returns as image resource*/
$mime = $this->mimeType($source);
      
$t = imagecreatetruecolor(8, 8);

$source = $this->createImage($source);

imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]);

return $t;
}
    
    private function colorMeanValue($i)
{
/*returns the mean value of the colors and the list of all pixel's colors*/
$colorList = array();
$colorSum = 0;
for($a = 0;$a<8;$a++)
{

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

$rgb = imagecolorat($i, $a, $b);
$colorList[] = $rgb & 0xFF;
$colorSum += $rgb & 0xFF;

}

}

return array($colorSum/64,$colorList);
}
    
    private function bits($colorMean)
{
/*returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1*/
$bits = array();
 
foreach($colorMean[1] as $color){$bits[]= ($color>=$colorMean[0])?1:0;}
return $bits;
}

    public function compare($a,$b)
{
/*main function. returns the hammering distance of two images' bit value*/
$i1 = $this->createImage($a);
$i2 = $this->createImage($b);

if(!$i1 || !$i2){return false;}

$i1 = $this->resizeImage($i1,$a);
$i2 = $this->resizeImage($i2,$b);

imagefilter($i1, IMG_FILTER_GRAYSCALE);
imagefilter($i2, IMG_FILTER_GRAYSCALE);

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

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

$hammeringDistance = 0;

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

if($bits1[$a] != $bits2[$a])
{
$hammeringDistance++;
}

}
  
return $hammeringDistance;
}
}

?>


on July 26th, 2019 (5:25 pm)
What masters said
You must sign in to comment!!
All comments
This solution has not been commented yet.