4 $PERIOD = '7day'; // 7day, 3moth, 6month, 12month, overall
5 // black, gray, blue, red, orange, turquoise, trans, white
6 // or array(R, G, B) for an arbitrary color
7 $STYLE = array(246,146,30);
9 $COLUMNS = 1; // 1 or 2
10 $API_KEY = "db109383394640f2b5e174b4514f0014";
12 $MARGIN_SIDE = 4; // Side margin (in px)
13 $MARGIN_TOP = 4; // Between the first logo and top
14 $SEPERATION = 5; // Separation between logos
18 * @param string Name of the artist
19 * @return Artist Infos of the artist
21 function get_artist_image($name)
24 foreach (glob($LOGOS_DIR . '/' . $name . '.*') as $file) {
25 $info = pathinfo($file);
26 $ext = strtolower($info['extension']);
28 return imagecreatefromgif($file);
29 if ($ext == 'jpeg' || $ext == 'jpg')
30 return imagecreatefromjpeg($file);
32 return imagecreatefrompng($file);
38 * Invert color from an image ressource.
40 * @param ressource $image Image
41 * "param string $color ID of a filter
43 function img_apply_filter($image, $color)
45 if ($color == 'black')
46 img_add_invert_filter($image);
47 elseif ($color == 'gray')
48 img_add_gray_filter($image);
49 elseif ($color == 'blue')
50 img_add_color_filter($image, array(40,79,148));
51 elseif ($color == 'red')
52 img_add_color_filter($image, array(214,17,2));
53 elseif ($color == 'orange')
54 img_add_color_filter($image, array(222,54,0));
55 elseif ($color == 'turquoise')
56 img_add_color_filter($image, array(69,168,196));
57 elseif ($color == 'trans')
58 img_add_trans_filter($image);
59 elseif ($color == 'white')
61 elseif (is_array($color))
62 img_add_color_filter($image, $color);
64 throw new Exception("Unknown filter.");
69 * Invert color from an image ressource.
71 * @param ressource $image Image
73 function img_add_invert_filter($image)
75 $width = imagesx($image);
76 $height = imagesy($image);
78 for ($x = 0; $x < $width; $x++) {
79 for ($y = 0; $y < $height; $y++) {
80 $oldCol = img_get_color_array($image,$x,$y);
81 $newColor = img_get_invert_color($oldCol);
82 imagesetpixel($image, $x, $y,
83 img_get_col_ref($image,$newColor));
89 * Make the image darker and make the background transparent.
91 * @param ressource $image Image
93 function img_add_trans_filter($image) {
95 $height = imagesy($image);
96 // Set gray as the transparent color
97 $ref = imagecolorresolve($image, $GRAY_VALUE, $GRAY_VALUE, $GRAY_VALUE);
98 imagecolortransparent($image, $ref);
100 for ($x = 0; $x < $width; $x++) {
101 for ($y = 0; $y < $height; $y++) {
102 $oldCol = img_get_color_array($image, $x, $y);
103 $newColor = img_get_darker_color($oldCol, $GRAY_VALUE);
104 imagesetpixel($image, $x, $y,
105 img_get_col_ref($image,$newColor,$ref));
111 * Make the image darker.
112 * - White pixel become darker.
113 * - Black pixel stay the same.
115 * @param ressource $image Image
117 function img_add_gray_filter($image)
121 $width = imagesx($image);
122 $height = imagesy($image);
124 for ($x = 0; $x < $width; $x++) {
125 for ($y = 0; $y < $height; $y++) {
126 $oldCol = img_get_color_array($image, $x, $y);
127 $newColor = img_get_darker_color($oldCol, $GRAY_VALUE);
128 imagesetpixel($image, $x, $y,
129 img_get_Col_Ref($image,$newColor));
135 * Replace the black color and dark tone to the given color.
137 * @param ressource $image Image
138 * @param array $color Color that will replace black
140 function img_add_color_filter($image, array $color)
144 $width = imagesx($image);
145 $height = imagesy($image);
147 $ref = imagecolorresolve($image, $GRAY_VALUE, $GRAY_VALUE, $GRAY_VALUE);
148 imagecolortransparent($image,$ref);
150 for ($x = 0; $x < $width; $x++) {
151 for ($y = 0; $y < $height; $y++) {
152 $oldCol = img_get_color_array($image, $x, $y);
153 $newColor = img_change_dark_to_spec_color(
154 img_get_darker_color($oldCol, $GRAY_VALUE),$color);
155 imagesetpixel($image, $x, $y,
156 img_get_col_ref($image, $newColor, $ref));
161 ////// Color basic util. //////
164 * Simple shortcut to get a color ref for the current image.
166 * @param ressource $image Image
167 * @param array $newColor RGB color array of the color added
168 * @param int $transRef If set should reference the transparent color id
169 * @return Return the ref id to the new color (can only be use for the given image)
171 function img_get_col_ref($image, array $newColor, $transRef=NULL)
175 $ref = imagecolorresolve($image, $newColor[0], $newColor[1],
177 if ($transRef && (array_sum($newColor) >= ($GRAY_VALUE - 15) * 3))
178 $ref = $transRef; // imagecolortransparent($image,$ref);
183 * @param ressource $image Image
184 * @param int $x X position
185 * @param int $y Y position
186 * @return Get the RGB array for a given position
188 function img_get_color_array($image, $x, $y)
190 $pos = imagecolorat($image, $x, $y);
191 $f = imagecolorsforindex($image, $pos);
194 if (count($f) == 3) {
195 $col = array($f['red'], $f['green'], $f['blue']);
198 $gst = $f['red'] * 0.15 + $f['green'] * 0.5 + $f['blue'] * 0.35;
199 $col = array($gst, $gst, $gst);
205 ////// Color modifications pattern //////
208 * @param array $color RGB color
209 * @return the RGB opposite
211 function img_get_invert_color(array $color)
213 return array(255 - $color[0], 255 - $color[1], 255 - $color[2]);
217 * @param array $color RGB color
218 * @param int $gray Gray Level (0-255) Lower is darker
219 * @return a RGB color darker than the original
221 function img_get_Darker_color(array $color, $gray)
223 $color[0] -= $color[0] / 255 * (255 - $gray);
225 // Base on the assumption the color are grayscale
226 $color[2] = $color[1] = $color[0];
234 function img_change_dark_to_spec_color(array $color, array $newColor)
238 $sum = array_sum($color);
240 if ($sum < ($GRAY_VALUE - 15) * 3) {
241 $color[0] += $newColor[0] * (255 - $color[0]) / 255;
242 $color[1] += $newColor[1] * (255 - $color[1]) / 255;
243 $color[2] += $newColor[2] * (255 - $color[2]) / 255;
249 * Output the image with the logos of each artist
251 function show_one_col($artists, $artists_no, $color, $width)
253 global $MARGIN_TOP, $MARGIN_SIDE, $SEPERATION;
256 $listImageRessources = array();
258 foreach ($artists as $name) {
259 if (count($listImageRessources) == $artists_no)
262 $image = get_artist_image($name);
264 if ($image !== null) {
265 $totalHeight += imagesy($image) + $SEPERATION;
266 array_push($listImageRessources, $image);
269 $totalHeight += $MARGIN_TOP;
271 $container = imagecreatetruecolor($width, $totalHeight);
273 // Some colors needed
274 $colWhite = imagecolorallocate($container, 255, 255, 255);
277 imagefilledrectangle($container, 0, 0, $width, $totalHeight, $colWhite);
279 $expectWidth = $width - (2 * $MARGIN_SIDE);
280 $currentHeight = $MARGIN_TOP;
281 foreach ($listImageRessources as $image) {
282 $widthImg = imagesx($image);
283 imagecopymerge($container, $image,
284 $MARGIN_SIDE + ($widthImg < $expectWidth ?
285 ($expectWidth - $widthImg) * 0.5 : 0),
286 $currentHeight, 0, 0, imagesx($image), imagesy($image),
288 $currentHeight += imagesy($image) + $SEPERATION;
291 img_apply_filter($container, $color);
293 imagepng($container);
297 * Output the image with the logos of each artist
299 function show_two_col($artists, $artists_no, $color, $width)
301 global $MARGIN_TOP, $MARGIN_SIDE, $SEPERATION;
303 if (count($artists) == 0)
304 throw new Exception("Missing some informations (artists).");
306 $totalHeight = array($MARGIN_TOP, $MARGIN_TOP);
307 $listImageRessources = array(array(), array());
309 $forcedWidth = ($width / 2) - ($SEPERATION / 2);
311 foreach ($artists as $name) {
312 if (count($listImageRessources[0]) +
313 count($listImageRessources[1]) == $artists_no)
316 $image = get_artist_image($name);
318 if ($image !== null) {
320 if ($totalHeight[1] < $totalHeight[0])
323 $x = imagesx($image);
324 $y = imagesy($image);
325 $newHeight = floor(($y * $forcedWidth) / $x);
327 $totalHeight[$i] += $newHeight + $SEPERATION;
328 array_push($listImageRessources[$i], $image);
332 $container = imagecreatetruecolor($width, max($totalHeight));
334 // Some colors needed
335 $colWhite = imagecolorallocate($container, 255, 255, 255);
338 imagefilledrectangle($container, 0, 0, $width, max($totalHeight), $colWhite);
340 $currentHeight = array($MARGIN_TOP, $MARGIN_TOP);
341 for ($l = 0; $l < count($listImageRessources); $l++) {
342 $list = $listImageRessources[$l];
344 foreach ($list as $image) {
345 $x = imagesx($image);
346 $y = imagesy($image);
348 $newHeight = floor(($y * $forcedWidth) / $x);
350 $resized = imagecreatetruecolor($forcedWidth, $newHeight);
351 imagecopyresampled($resized, $image, 0, 0, 0, 0,
352 $forcedWidth, $newHeight, $x, $y);
354 imagecopymerge($container, $resized,
355 ($forcedWidth + $SEPERATION) * $l,
356 $currentHeight[$l], 0, 0,
357 $forcedWidth, $newHeight, 100);
359 $currentHeight[$l] += $newHeight + $SEPERATION;
363 img_apply_filter($container, $color);
365 imagepng($container);
369 * Optain the list of Top artists from a user
370 * @param string $accountName Unique name
371 * @param int $nbArtists Number of artists to list
373 function get_list_top_artists($username, $type='overall', $nbArtists=0)
376 if (strlen($username) == 0 || strlen($username) > 15)
377 throw new Exception("Invalid username.");
379 $content = file_get_contents("http://ws.audioscrobbler.com/2.0/" .
380 "?method=user.gettopartists" .
381 "&user=" . $username.
383 "&api_key=" . $API_KEY);
384 $lines = explode("\n", $content);
385 if (count($lines) == 0)
386 throw new Exception("Feed is empty.");
390 foreach ($lines as $line) {
392 if (strrpos($line, "<url>http://www.last.fm/music/") ===
396 // Extract the encode name from the url
397 preg_match("(http://www.last.fm/music/([^<]+))", $line,
399 array_push($artists, $proper[1]);
402 if ($noartist == $nbArtists) // Stop when max
406 if (count($artists) == 0)
407 throw new Exception("No Artist found. Invalid feed??");
412 $artists = get_list_top_artists($USER, $PERIOD);
414 if (count($artists) > 0)
416 show_one_col($artists, $N_ARTISTS, $STYLE, 168);
417 elseif ($COLUMNS == 2)
418 show_two_col($artists, $N_ARTISTS, $STYLE, 300);
420 throw new Exception("Invalid number of columns.");
422 throw new Exception("No artist found.");