read()) { if (preg_match('!^p.*\.jpg$!', $file)) $paths[] = $file; } $d->close(); $img = $paths[array_rand($paths)]; /* The original image is the average colors */ $average = new Imagick( $img ); /* Reduce the amount of colors to 10 */ $average->quantizeImage( 1, Imagick::COLORSPACE_RGB, 0, false, false ); //header( "Content-Type: image/jpeg" ); echo $average; exit; /* Only save one pixel of each color */ $average->uniqueImageColors(); function getPixColors(Imagick $im) { /* Get ImagickPixelIterator */ $it = $im->getPixelIterator(); /* Reset the iterator to begin */ $it->resetIterator(); $colors = array(); /* Loop trough rows */ while( $row = $it->getNextIteratorRow() ) { /* Loop trough columns */ foreach ( $row as $pixel ) { $colors[] = $pixel->getHSL(); } } return $colors; } $colors = getPixColors($average); function getHSL($color, $brightness = 0) { if ($brightness != 0) { $color['luminosity'] += ($brightness / 100); } return 'hsl('.round($color['hue']*360).', '.round($color['saturation']*100).'%, '.round($color['luminosity']*100).'%)'; } $i = ceil(count($colors) / 2); if ($i < 0) $i = 0; if ($i > count($colors)) $i = count($colors) - 1; echo '
'; echo '