Skip to main content
deleted 19 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Best practices in PHP Finding similarities between images

I've posted here before and got pretty helpful reviews. So, howHow does my code look now.? To keep it really helpful, I just copied this from the middle of one of my projects. Review please?

 <?php function findSimilarityBetweenImages($imagePathA, $imagePathB, $accuracy){ //if images are png $imageA = imagecreatefrompng($imagePathA); $imageB = imagecreatefrompng($imagePathB); //if images are jpeg //$imageA = imagecreatefromjpeg($imagePathA); //$imageB = imagecreatefromjpeg($imagePathB); //get image resolution $imageX = imagesx($imageA); $imageY = imagesy($imageA); $pointsX = $accuracy*5; $pointsY = $accuracy*5; $sizeX = round($imageX/$pointsX); $sizeY = round($imageY/$pointsY); //Compare the color of each point while looping through. $y = 0; $match = 0; $num = 0; for ($i=0; $i < $pointsY; $i++) { $x = 0; for($n=0; $n < $pointsX; $n++){ $rgba = imagecolorat($imageA, $x, $y); $colorsa = imagecolorsforindex($imageA, $rgba); $rgbb = imagecolorat($imageB, $x, $y); $colorsb = imagecolorsforindex($imageB, $rgbb); if(colorComp($colorsa['red'], $colorsb['red']) && colorComp($colorsa['green'], $colorsb['green']) && colorComp($colorsa['blue'], $colorsb['blue'])){ $match ++; //Match between points. } $x += $sizeX; $num++; } $y += $sizeY; } //A similarity or percentage of match between the two images. $similarity = $match*(100/$num); return $similarity; } function colorComp($color, $c){ //To see if the points match if($color >= $c-2 && $color <= $c+2) { return true; } else { return false; } } ?> 

Best practices in PHP

I've posted here before and got pretty helpful reviews. So, how does my code look now. To keep it really helpful, I just copied this from the middle of one of my projects. Review please?

 <?php function findSimilarityBetweenImages($imagePathA, $imagePathB, $accuracy){ //if images are png $imageA = imagecreatefrompng($imagePathA); $imageB = imagecreatefrompng($imagePathB); //if images are jpeg //$imageA = imagecreatefromjpeg($imagePathA); //$imageB = imagecreatefromjpeg($imagePathB); //get image resolution $imageX = imagesx($imageA); $imageY = imagesy($imageA); $pointsX = $accuracy*5; $pointsY = $accuracy*5; $sizeX = round($imageX/$pointsX); $sizeY = round($imageY/$pointsY); //Compare the color of each point while looping through. $y = 0; $match = 0; $num = 0; for ($i=0; $i < $pointsY; $i++) { $x = 0; for($n=0; $n < $pointsX; $n++){ $rgba = imagecolorat($imageA, $x, $y); $colorsa = imagecolorsforindex($imageA, $rgba); $rgbb = imagecolorat($imageB, $x, $y); $colorsb = imagecolorsforindex($imageB, $rgbb); if(colorComp($colorsa['red'], $colorsb['red']) && colorComp($colorsa['green'], $colorsb['green']) && colorComp($colorsa['blue'], $colorsb['blue'])){ $match ++; //Match between points. } $x += $sizeX; $num++; } $y += $sizeY; } //A similarity or percentage of match between the two images. $similarity = $match*(100/$num); return $similarity; } function colorComp($color, $c){ //To see if the points match if($color >= $c-2 && $color <= $c+2) { return true; } else { return false; } } ?> 

Finding similarities between images

I've posted here before and got pretty helpful reviews. How does my code look now? To keep it really helpful, I just copied this from the middle of one of my projects.

 <?php function findSimilarityBetweenImages($imagePathA, $imagePathB, $accuracy){ //if images are png $imageA = imagecreatefrompng($imagePathA); $imageB = imagecreatefrompng($imagePathB); //if images are jpeg //$imageA = imagecreatefromjpeg($imagePathA); //$imageB = imagecreatefromjpeg($imagePathB); //get image resolution $imageX = imagesx($imageA); $imageY = imagesy($imageA); $pointsX = $accuracy*5; $pointsY = $accuracy*5; $sizeX = round($imageX/$pointsX); $sizeY = round($imageY/$pointsY); //Compare the color of each point while looping through. $y = 0; $match = 0; $num = 0; for ($i=0; $i < $pointsY; $i++) { $x = 0; for($n=0; $n < $pointsX; $n++){ $rgba = imagecolorat($imageA, $x, $y); $colorsa = imagecolorsforindex($imageA, $rgba); $rgbb = imagecolorat($imageB, $x, $y); $colorsb = imagecolorsforindex($imageB, $rgbb); if(colorComp($colorsa['red'], $colorsb['red']) && colorComp($colorsa['green'], $colorsb['green']) && colorComp($colorsa['blue'], $colorsb['blue'])){ $match ++; //Match between points. } $x += $sizeX; $num++; } $y += $sizeY; } //A similarity or percentage of match between the two images. $similarity = $match*(100/$num); return $similarity; } function colorComp($color, $c){ //To see if the points match if($color >= $c-2 && $color <= $c+2) { return true; } else { return false; } } ?> 

Best practices in PHP function. I'm trying to improve my coding style.Where do I need to change? Any Tips?

Source Link
Ganz7
  • 45
  • 1
  • 5

PHP function. I'm trying to improve my coding style.Where do I need to change? Any Tips?

I've posted here before and got pretty helpful reviews. So, how does my code look now. To keep it really helpful, I just copied this from the middle of one of my projects. Review please?

 <?php function findSimilarityBetweenImages($imagePathA, $imagePathB, $accuracy){ //if images are png $imageA = imagecreatefrompng($imagePathA); $imageB = imagecreatefrompng($imagePathB); //if images are jpeg //$imageA = imagecreatefromjpeg($imagePathA); //$imageB = imagecreatefromjpeg($imagePathB); //get image resolution $imageX = imagesx($imageA); $imageY = imagesy($imageA); $pointsX = $accuracy*5; $pointsY = $accuracy*5; $sizeX = round($imageX/$pointsX); $sizeY = round($imageY/$pointsY); //Compare the color of each point while looping through. $y = 0; $match = 0; $num = 0; for ($i=0; $i < $pointsY; $i++) { $x = 0; for($n=0; $n < $pointsX; $n++){ $rgba = imagecolorat($imageA, $x, $y); $colorsa = imagecolorsforindex($imageA, $rgba); $rgbb = imagecolorat($imageB, $x, $y); $colorsb = imagecolorsforindex($imageB, $rgbb); if(colorComp($colorsa['red'], $colorsb['red']) && colorComp($colorsa['green'], $colorsb['green']) && colorComp($colorsa['blue'], $colorsb['blue'])){ $match ++; //Match between points. } $x += $sizeX; $num++; } $y += $sizeY; } //A similarity or percentage of match between the two images. $similarity = $match*(100/$num); return $similarity; } function colorComp($color, $c){ //To see if the points match if($color >= $c-2 && $color <= $c+2) { return true; } else { return false; } } ?>