/** * Checks if the image is a logo, by checking if the outer pixels all have the same color * This method returns true when: * - The image is a png or gif * - The colors of the outermost pixels are the same color * @param string $path * @return bool */ public static function IsLogo($path) { if (!file_exists($path)) { return false; } try { $info = getimagesize($path); $img = null; $transparent = false; switch ($info[2]) { case IMAGETYPE_GIF: case IMAGETYPE_PNG: $img = $info[2] == IMAGETYPE_GIF ? imagecreatefromgif($path) : imagecreatefrompng($path); $rgba = imagecolorat($img, 0, 0); $alpha = ($rgba & 0x7f000000) >> 24; $transparent = $alpha == 127; break; case IMAGETYPE_JPEG: case IMAGETYPE_JPEG2000: $img = imagecreatefromjpeg($path); break; default: // Unknown image type throw new \Exception("invalid image"); } $sameColor = 0; $totalPixels = $info[0] * 2 + $info[1] * 2 - 4; if ($transparent) { // Image has transparency, logo for sure return true; } else { $color = ColorUtils::dec2hex(imagecolorat($img, 0, 0)); // Check top and bottom line for color differences, for ($x = 0; $x < $info[0]; $x++) { if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $x, 0)), 10, $x, 0)) { $sameColor++; } if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $x, $info[1] - 1)), 10, $x, $info[1] - 1)) { $sameColor++; } } // Check left and right line for color differences, for ($y = 0; $y < $info[1]; $y++) { if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, 0, $y)), 10, 0, $y)) { $sameColor++; } if (ColorUtils::compareColors($color, ColorUtils::dec2hex(imagecolorat($img, $info[0] - 1, $y)), 10, $info[0] - 1, $y)) { $sameColor++; } } } imagedestroy($img); // 90% of the outer pixels are the same color return $sameColor / $totalPixels > 0.9; } catch (\Exception $e) { } return false; }