/** * Reformat the photo's id into a fixed width string that can be sorted. * * @param object Phlickr_Photo $photo * @return string */ function stringFromPhoto(Phlickr_Photo $photo) { return sprintf('%012d', $photo->getId()); }
/** * Return a sortable string of the photos average color. * * Steps: * - Resample the image to single pixel. * - Convert the pixel's RGB value to an HSV value, * - Convert the HSV to a string. * * @param object Phlickr_Photo $photo * @return string or false in case of error. */ public function stringFromPhoto(Phlickr_Photo $photo) { // cache the RGB value so that multiple color sort strategies can // share the average color. $key = 'avg_color:' . $photo->getId(); if ($this->_cache->has($key)) { $rgb = $this->_cache->get($key); } else { $url = $photo->buildImgUrl(Phlickr_Photo::SIZE_100PX); if ($rgb = self::getAverageRgbColor($url)) { // add the average color to the cache and mark it as // non-expiring because the photo can't be changed. $this->_cache->set($key, $rgb, -1); } else { // if there's an error getting the average value don't add it // to the cache, just bail. return false; } } $hsv = self::HsvFromRgb($rgb); return sprintf("%02d,%02d,%02d", round($hsv[0] / 30), round($hsv[1] * 10), round($hsv[2] * 10)); }