= ($GRAY_VALUE - 15) * 3)) $ref = $transRef; // imagecolortransparent($image,$ref); return $ref; } /** * @param ressource $image Image * @param int $x X position * @param int $y Y position * @return Get the RGB array for a given position */ function img_get_color_array($image, $x, $y) { $pos = imagecolorat($image, $x, $y); $f = imagecolorsforindex($image, $pos); $col = NULL; if (count($f) == 3) { $col = array($f['red'], $f['green'], $f['blue']); } else { $gst = $f['red'] * 0.15 + $f['green'] * 0.5 + $f['blue'] * 0.35; $col = array($gst, $gst, $gst); } return $col; } ////// Color modifications pattern ////// /** * @param array $color RGB color * @return the RGB opposite */ function img_get_invert_color(array $color) { return array(255 - $color[0], 255 - $color[1], 255 - $color[2]); } /** * @param array $color RGB color * @param int $gray Gray Level (0-255) Lower is darker * @return a RGB color darker than the original */ function img_get_Darker_color(array $color, $gray) { $color[0] -= $color[0] / 255 * (255 - $gray); // Base on the assumption the color are grayscale $color[2] = $color[1] = $color[0]; return $color; } /** * */ function img_change_dark_to_spec_color(array $color, array $newColor) { global $GRAY_VALUE; $sum = array_sum($color); if ($sum < ($GRAY_VALUE - 15) * 3) { $color[0] += $newColor[0] * (255 - $color[0]) / 255; $color[1] += $newColor[1] * (255 - $color[1]) / 255; $color[2] += $newColor[2] * (255 - $color[2]) / 255; } return $color; } /** * Output the image with the logos of each artist */ function show_one_col($artists, $artists_no, $color, $width) { global $MARGIN_TOP, $MARGIN_SIDE, $SEPERATION; $totalHeight=0; $listImageRessources = array(); foreach ($artists as $name) { if (count($listImageRessources) == $artists_no) break; $image = get_artist_image($name); if ($image !== null) { $totalHeight += imagesy($image) + $SEPERATION; array_push($listImageRessources, $image); } } $totalHeight += $MARGIN_TOP; $container = imagecreatetruecolor($width, $totalHeight); // Some colors needed $colWhite = imagecolorallocate($container, 255, 255, 255); // Background color imagefilledrectangle($container, 0, 0, $width, $totalHeight, $colWhite); $expectWidth = $width - (2 * $MARGIN_SIDE); $currentHeight = $MARGIN_TOP; foreach ($listImageRessources as $image) { $widthImg = imagesx($image); imagecopymerge($container, $image, $MARGIN_SIDE + ($widthImg < $expectWidth ? ($expectWidth - $widthImg) * 0.5 : 0), $currentHeight, 0, 0, imagesx($image), imagesy($image), 100); $currentHeight += imagesy($image) + $SEPERATION; } img_apply_filter($container, $color); imagepng($container); } /** * Output the image with the logos of each artist */ function show_two_col($artists, $artists_no, $color, $width) { global $MARGIN_TOP, $MARGIN_SIDE, $SEPERATION; if (count($artists) == 0) throw new Exception("Missing some informations (artists)."); $totalHeight = array($MARGIN_TOP, $MARGIN_TOP); $listImageRessources = array(array(), array()); $forcedWidth = ($width / 2) - ($SEPERATION / 2); foreach ($artists as $name) { if (count($listImageRessources[0]) + count($listImageRessources[1]) == $artists_no) break; $image = get_artist_image($name); if ($image !== null) { $i = 0; if ($totalHeight[1] < $totalHeight[0]) $i = 1; $x = imagesx($image); $y = imagesy($image); $newHeight = floor(($y * $forcedWidth) / $x); $totalHeight[$i] += $newHeight + $SEPERATION; array_push($listImageRessources[$i], $image); } } $container = imagecreatetruecolor($width, max($totalHeight)); // Some colors needed $colWhite = imagecolorallocate($container, 255, 255, 255); // Background color imagefilledrectangle($container, 0, 0, $width, max($totalHeight), $colWhite); $currentHeight = array($MARGIN_TOP, $MARGIN_TOP); for ($l = 0; $l < count($listImageRessources); $l++) { $list = $listImageRessources[$l]; foreach ($list as $image) { $x = imagesx($image); $y = imagesy($image); $newHeight = floor(($y * $forcedWidth) / $x); $resized = imagecreatetruecolor($forcedWidth, $newHeight); imagecopyresampled($resized, $image, 0, 0, 0, 0, $forcedWidth, $newHeight, $x, $y); imagecopymerge($container, $resized, ($forcedWidth + $SEPERATION) * $l, $currentHeight[$l], 0, 0, $forcedWidth, $newHeight, 100); $currentHeight[$l] += $newHeight + $SEPERATION; } } img_apply_filter($container, $color); imagepng($container); } /** * Optain the list of Top artists from a user * @param string $accountName Unique name * @param int $nbArtists Number of artists to list */ function get_list_top_artists($username, $type='overall', $nbArtists=0) { global $API_KEY; if (strlen($username) == 0 || strlen($username) > 15) throw new Exception("Invalid username."); $content = file_get_contents("http://ws.audioscrobbler.com/2.0/" . "?method=user.gettopartists" . "&user=" . $username. "&period=" . $type. "&api_key=" . $API_KEY); $lines = explode("\n", $content); if (count($lines) == 0) throw new Exception("Feed is empty."); $artists = array(); $noartist = 0; foreach ($lines as $line) { // Skip useless line if (strrpos($line, "http://www.last.fm/music/") === false) continue; // Extract the encode name from the url preg_match("(http://www.last.fm/music/([^<]+))", $line, $proper); array_push($artists, $proper[1]); $noartist++; if ($noartist == $nbArtists) // Stop when max break; } if (count($artists) == 0) throw new Exception("No Artist found. Invalid feed??"); return $artists; } $artists = get_list_top_artists($USER, $PERIOD); if (count($artists) > 0) if ($COLUMNS == 1) show_one_col($artists, $N_ARTISTS, $STYLE, 168); elseif ($COLUMNS == 2) show_two_col($artists, $N_ARTISTS, $STYLE, 300); else throw new Exception("Invalid number of columns."); else throw new Exception("No artist found."); ?>