Récupérer les couleurs des bordures d'une image en PHP

Rédigé par BeHuman Aucun commentaire
Classé dans : Php Mots clés : image, border, bordure, couleur, color, détection, scan, corrélation, pourcentage, gd, rgb, png, jpg, gif

Salut,

Cette petite routine vous permettra de récupérer les couleurs des bordures d'une image et de retourner un pourcentage de corrélation. Cela me permet de détecter les images n'ayant pas de fond blanc pour le catalogue Google par exemple.

function scanImgBorderColor($filename, $ro=255, $go=255, $bo=255) {
    if ( ( preg_match('/.*\.png$/',$filename) || preg_match('/.*\.jpg$/',$filename) || preg_match('/.*\.gif$/',$filename) ) &&  file_exists($filename) ) {
        if (preg_match('/.*\.png$/',$filename)) {
            $im = imagecreatefrompng($filename);
        } else if (preg_match('/.*\.jpg$/',$filename) || preg_match('/.*\.jpeg$/',$filename) ) {
            $im = imagecreatefromjpeg($filename);
        } else if (preg_match('/.*\.gif$/',$filename)) {
            $im = imagecreatefromgif($filename);
        }
        $npx=0;
        $size = getimagesize($filename);
        for ($i=1; $i<=$size[0]-1; $i++) {
            $rgb = imagecolorat($im, 1, $i);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            if ($r < $ro && $g < $go && $b < $bo ) {
                $npx++;
            }
            $rgb = imagecolorat($im, $size[1]-1, $i);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            if ($r < $ro && $g < $go && $b < $bo) {
                $npx++;
            }
        }
        
        for ($i=1; $i<=$size[1]-1; $i++) {
            $rgb = imagecolorat($im, $i, 1);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            if ($r < $ro && $g < $go && $b < $bo) {
                $npx++;
            }
            $rgb = imagecolorat($im, $i, $size[0]-1);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            if ($r < $ro && $g < $go && $b < $bo) {
                $npx++;
            }
        }
        $allpx=(($size[0]-1)*2)+(($size[1]-1)*2);
        $endpx=100-(($npx/$allpx)*100);
        return $endpx;
    }
}

if (scanImgBorderColor('img001.png', 250, 250, 250)>=90) {
    //image ok
} else {
    //image trop sombre
}

voilii voiloo ++

Écrire un commentaire

Quelle est la sixième lettre du mot cxs9218 ?

Fil RSS des commentaires de cet article