8721 sujets

Développement web côté serveur, CMS

utilisé une base de donnée, et une table intermediaire qui lie les tableaux à des couleurs dominante choisir pour ton clients.
Bonsoir, Jencal, et merci de ta fidélité.

Les images sont dans un répertoire, et les descriptions, dans une base de données.

L'idéé, c'est de ne rien avoir à ajouter à la base de données. De trouver un manière que PHP ou JS détecte automatiquement la couleur dominante.
Voilà le genre de script que je recherche :

$source_file = "test_image.jpg";
//histogram options
$maxheight = 300;
$barwidth = 2;
$im = ImageCreateFromJpeg($source_file);
$imgw = imagesx($im);
$imgh = imagesy($im);
//n = total number or pixels
$n = $imgw*$imgh;
$histo = array();
for ($i=0; $i<$imgw; $i++)
{
for ($j=0; $j<$imgh; $j++)
{
//get the rgb value for current pixel
$rgb = ImageColorAt($im, $i, $j);
//extract each value for r, g, b
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
//get the Value from the RGB value
$V = round(($r + $g + $b) / 3);
//add the point to the histogram
$histo[$V] += $V / $n;
}
}
//find the maximum in the histogram in order to display a normated graph
$max = 0;
for ($i=0; $i<255; $i++)
{
if ($histo[$i] > $max)
{
$max = $histo[$i];
}
}
Je ne résiste pas plus longtemps à la satisfaction narcissique de vous soumettre le fruit de mes cogitations vespérales :

ini_set( "display_errors" , TRUE ) ; error_reporting( E_ALL ) ;

$image = ImageCreateFromJpeg( "chauffe-eau.jpg" ) ;
$rouges = $verts = $bleus = array() ; 

$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;

$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$nombre = $width * $height ;

$rouge = round( array_sum( $rouges ) / $nombre ) ;
$vert = round(  array_sum( $verts ) / $nombre ) ;
$bleu = round(  array_sum( $bleus ) / $nombre ) ;

$couleur = "rgb( $rouge, $vert, $bleu )" ; 

echo "<hr style=\"width:1cm; height:1cm; background:$couleur;\" />" ;  


Des réactions ?
Un chouia plus élaboré; sous forme de fonction :

function dominanteextraire( $path )
{
$image = ImageCreateFromJpeg( $path ) ;
$rouges = $verts = $bleus = array() ; 

$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;

$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$nombre = $width * $height ;

$rouge = round( array_sum( $rouges ) / $nombre ) ;
$vert = round(  array_sum( $verts ) / $nombre ) ;
$bleu = round(  array_sum( $bleus ) / $nombre ) ;

return "rgb( $rouge, $vert, $bleu )" ; 
}


A vos tests !!!

Et bonne nuit...
Lister les jpg proches d'une couleur donnée ( $rouge, $vert, $bleu ) :

function pericolorlister( $liste, $rouge, $vert, $bleu )
{
$files = $bornes = array() ; 

$colors = array( "rouge", "vert", "bleu" ) ; 
foreach( $colors as $color ) $bornes[$color] = array( $$color * .9, $$color * 1.1 ) ;

foreach( $liste as $file )
{
$image = ImageCreateFromJpeg( "Paints/$file" ) ;
$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;

$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$nombre = $width * $height ;
$rouge = round( array_sum( $rouges ) / $nombre ) ;
$vert = round(  array_sum( $verts ) / $nombre ) ;
$bleu = round(  array_sum( $bleus ) / $nombre ) ;

if( $rouge > $bornes["rouge"][0] && $rouge < $bornes["rouge"][1]
&& $vert > $bornes["vert"][0] && $vert < $bornes["vert"][1]
&& $bleu > $bornes["bleu"][0] && $bleu < $bornes["bleu"][1]
) 
$files[] = $file ;
}

return implode( SPACE, $files ) ; // exploitable par un fetch JS ;
}


Qu'en pensez-vous ?
L'étape d'après :

function pericolorlister( $liste, $path, $rouge, $vert, $bleu )
{
$files = $bornes = $rouges = $verts = $bleus = array() ; $delta = 25 ; 

$colors = array( "rouge", "vert", "bleu" ) ; 
foreach( $colors as $color ) $bornes[$color] = array( $$color-$delta, $$color+$delta ) ;

foreach( $liste as $file )
{
$image = ImageCreateFromJpeg( "$path/$file" ) ;
$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;

$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$nombre = $width * $height ;
$rouge = round( array_sum( $rouges ) / $nombre ) ;
$vert = round(  array_sum( $verts ) / $nombre ) ;
$bleu = round(  array_sum( $bleus ) / $nombre ) ;

if( $rouge > $bornes["rouge"][0] && $rouge < $bornes["rouge"][1]
&& $vert > $bornes["vert"][0] && $vert < $bornes["vert"][1]
&& $bleu > $bornes["bleu"][0] && $bleu < $bornes["bleu"][1]
) 
$files[] = tag( $file, "li" ) ;
}

return tag( "liste des images proches de la couleur indiquée", "h3" ).tag( $files, "ol" ) ;
}


Et la fonction tag :


function tag()
{
$arguments = func_get_args() ;
$curseur = trim( is_array( $arguments[0] ) ? join( NL, $arguments[0] ) : $arguments[0] ) ;
$intercalaire = pallsdenombrer( $curseur ) || strlen( $curseur ) > 200 ? NL : VIDE ;

foreach( $arguments as $no => $argument ) if( $no && $argument )
{
$commentaire = VIDE ;
$attributs = explode( SPACE, $argument ) ; 
if( ! defined( "??" ) ) define( "??", TRUE ) ; 

foreach( $attributs as $no => $attribut ) 
if( $no && $attribut ) { $commentaire = ?? ? commenter( $attribut ) : VIDE ; break ; }

$curseur = LT.$argument.GT.$intercalaire.$curseur.$intercalaire.LT.SLASH.$attributs[0].GT.$commentaire ;
}

return $curseur ;
}
Bonjour, les Alsanautes !!!

Je reviens à la charge, sur ce thème, qui n'est pas résolu. Je vous rappelle que la demande du client est que le visiteur puisse lister les images proches d'une couleur donnée. Voici ma dernière ponte :

<?php

ini_set( "display_errors" , TRUE ) ; error_reporting( E_ALL ) ;
foreach( $_POST as $nom => $value ) $$nom = $value ;
 
$couleurs = array(
"rouge" => hexdec( substr( $couleur, 1, 2 ) ),
"vert" => hexdec( substr( $couleur, 3, 2 ) ),
"bleu" => hexdec( substr( $couleur, 5, 2 ) ),
) ;

if( ! $precision ) $precision = 0 ; $bornes = array() ; $delta = 255 / ( $precision + 1 ) ;
foreach( $couleurs as $nom => $value ) $bornes[$nom] = array( $value-$delta, $value+$delta ) ;

$files = array() ; $dirpath = "../Paints" ; $scandir = scandir( $dirpath ) ;
for( $n=1, $size=count($scandir), $max=$size/9; $n<$size ; )
{
$n += mt_rand( 1, $max ) ; if( isset( $scandir[$n] ) ) $file = $scandir[$n] ; else break ;
  
$image = ImageCreateFromJpeg( "$dirpath/$file" ) ;
$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
$total = $width * $height ;

if( $total < 2500000 )
{
$rouges = $verts = $bleus = array() ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;
 
$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$rouge = array_sum( $rouges ) / $total ;
$vert = array_sum( $verts ) / $total ;
$bleu = array_sum( $bleus ) / $total ;

if( 
$rouge > $bornes["rouge"][0] && $rouge < $bornes["rouge"][1]
&& $vert > $bornes["vert"][0] && $vert < $bornes["vert"][1]
&& $bleu > $bornes["bleu"][0] && $bleu < $bornes["bleu"][1]
) 
$files[] = $file ;
}
}
 
echo join( "|||", $files ) ;
 
?>

Il y a un premier problème, grave, mais qui concerne, avant tout, le client : ses images sont trop grosses. De plus, il y en a 600. D'où l'idée de n'en considérer qu'un échantillon; environ un dixième.

Envie d'en dire ?
https://grom.doobee.fun/?search-form
Modifié par dagobert (23 Sep 2022 - 11:58)
Bonsoir (il fait gris, et il pleut),

le but du jeu est de détecter les images proches d'une couleur donnée.
Voilà où j'en suis :
<?php

ini_set( "display_errors" , TRUE ) ; error_reporting( E_ALL ) ;
foreach( $_POST as $nom => $value ) $$nom = $value ;
 
$couleurs = array(
"rouge" => hexdec( substr( $couleur, 1, 2 ) ),
"vert" => hexdec( substr( $couleur, 3, 2 ) ),
"bleu" => hexdec( substr( $couleur, 5, 2 ) ),
) ;

$bornes = array() ; $delta = 125 ;
foreach( $couleurs as $nom => $value ) $bornes[$nom] = array( $value-$delta, $value+$delta ) ;

$files = array() ; $dirpath = "../Paints" ; $scandir = scandir( $dirpath ) ;
for( $n=1, $size=count($scandir), $max=$size/50; $n<$size ; )
{
$n += mt_rand( 1, $max ) ; if( isset( $scandir[$n] ) ) $file = $scandir[$n] ; else break ;
  
$image = ImageCreateFromJpeg( "$dirpath/$file" ) ;a
$width = imagesx( $image ) ;
$height = imagesy( $image ) ;
$total = $width * $height ;

if( $total < 700000 )
{
$rouges = $verts = $bleus = array() ;
 
for( $x=0; $x<$width; $x++ ) for( $y=0; $y<$height; $y++ )
{
$rgb = ImageColorAt( $image, $x, $y ) ;
 
$rouges[] = ( $rgb >> 16 ) & 0xFF ;
$verts[] = ( $rgb >> 8 ) & 0xFF ;
$bleus[] = $rgb & 0xFF ;
}

$rouge = array_sum( $rouges ) / $total ;
$vert = array_sum( $verts ) / $total ;
$bleu = array_sum( $bleus ) / $total ;

if( 
$rouge > $bornes["rouge"][0] && $rouge < $bornes["rouge"][1]
&& $vert > $bornes["vert"][0] && $vert < $bornes["vert"][1]
&& $bleu > $bornes["bleu"][0] && $bleu < $bornes["bleu"][1]
) 
$files[] = $file ;
}
imagedestroy( $image ); 
}
 
echo join( "|||", $files ) ;
 
?>

Des commentaires ?
https://www.jerome-turpin-peintre.net/?search-form