diff --git a/album-collage-generator.php b/album-collage-generator.php index e31847b..fb52317 100644 --- a/album-collage-generator.php +++ b/album-collage-generator.php @@ -24,7 +24,8 @@ class AlbumCollageGenerator { $this->text_color = $this->hex_to_rgb('#ececec'); $this->western_font = plugin_dir_path(__FILE__) . 'assets/co1251n.ttf'; $this->japanese_font = plugin_dir_path(__FILE__) . 'assets/BIZUDPMincho-Regular.ttf'; - $this->blacklisted_artists = array('Drake', 'Mac Miller'); // placeholder values + $this->blacklisted_artists = array('Drake', 'Mac Miller', 'Kendrick Lamar'); + $this->blacklisted_albums = array('OK Computer OKNOTOK 1997 2017'); } public function register_endpoint() { @@ -111,20 +112,39 @@ class AlbumCollageGenerator { private function get_top_albums($year, $month) { // Prepare placeholders for blacklisted artists - $placeholders = array_fill(0, count($this->blacklisted_artists), '%s'); - $placeholders_str = implode(', ', $placeholders); - - // Merge all parameters for $wpdb->prepare + $artist_placeholders = array_fill(0, count($this->blacklisted_artists), '%s'); + $artist_placeholders_str = implode(', ', $artist_placeholders); + + // Prepare placeholders for blacklisted albums + $album_placeholders = array_fill(0, count($this->blacklisted_albums), '%s'); + $album_placeholders_str = implode(', ', $album_placeholders); + + // Initialize query parts + $artist_condition = ''; + $album_condition = ''; $query_params = array($year, $month); - $query_params = array_merge($query_params, $this->blacklisted_artists); - + + // Handle blacklisted artists + if (!empty($this->blacklisted_artists)) { + $artist_condition = "AND author NOT IN ($artist_placeholders_str)"; + $query_params = array_merge($query_params, $this->blacklisted_artists); + } + + // Handle blacklisted albums + if (!empty($this->blacklisted_albums)) { + $album_condition = "AND album_name NOT IN ($album_placeholders_str)"; + $query_params = array_merge($query_params, $this->blacklisted_albums); + } + + // Prepare the SQL query with updated ORDER BY in ranked_covers $query = $this->db->prepare( "WITH ranked_covers AS ( SELECT album_name, author, cover_url, - ROW_NUMBER() OVER (PARTITION BY album_name, author ORDER BY time DESC) as rn + id, -- Ensure id is selected for ordering + ROW_NUMBER() OVER (PARTITION BY album_name, author ORDER BY id DESC) as rn FROM song_scrobbles WHERE cover_url != '' AND cover_url IS NOT NULL ), @@ -137,8 +157,8 @@ class AlbumCollageGenerator { FROM song_scrobbles WHERE YEAR(time) = %d AND MONTH(time) = %d AND album_name != '' AND album_name IS NOT NULL - -- Exclude blacklisted artists - AND author NOT IN ($placeholders_str) + $artist_condition + $album_condition -- Exclude albums with '¥' symbol in album_name or author AND album_name NOT LIKE '%%¥%%' AND author NOT LIKE '%%¥%%' @@ -179,7 +199,7 @@ class AlbumCollageGenerator { ); return $this->db->get_results($query); - } + } public function get_or_generate_image($request) { $month = $request->get_param('month'); @@ -306,16 +326,68 @@ class AlbumCollageGenerator { for ($i = 0; $i < min(count($albums), 25); $i++) { $row = floor($i / 5); $col = $i % 5; - + $x = $left_margin + $col * ($album_size + $gap); $y = $top_margin + $row * ($album_size + $gap); - - $album_cover = @imagecreatefromjpeg($albums[$i]->cover_url); + + // Detect the image type + $image_type = @exif_imagetype($albums[$i]->cover_url); + $album_cover = false; + + switch ($image_type) { + case IMAGETYPE_JPEG: + $album_cover = @imagecreatefromjpeg($albums[$i]->cover_url); + break; + case IMAGETYPE_PNG: + $album_cover = @imagecreatefrompng($albums[$i]->cover_url); + break; + default: + // Unsupported image type + $album_cover = false; + break; + } + if ($album_cover !== false) { + // If the image is PNG, preserve transparency + if ($image_type === IMAGETYPE_PNG) { + imagealphablending($album_cover, true); + imagesavealpha($album_cover, true); + } + $this->apply_retro_effects($album_cover); - imagecopyresampled($image, $album_cover, $x, $y, 0, 0, $album_size, $album_size, imagesx($album_cover), imagesy($album_cover)); + + // If the main image has transparency and the album cover is PNG, preserve it + if ($image_type === IMAGETYPE_PNG) { + imagecopyresampled( + $image, + $album_cover, + $x, + $y, + 0, + 0, + $album_size, + $album_size, + imagesx($album_cover), + imagesy($album_cover) + ); + } else { + imagecopyresampled( + $image, + $album_cover, + $x, + $y, + 0, + 0, + $album_size, + $album_size, + imagesx($album_cover), + imagesy($album_cover) + ); + } + imagedestroy($album_cover); } else { + // Draw placeholder rectangle $placeholder_color = imagecolorallocate($image, 100, 100, 100); imagefilledrectangle($image, $x, $y, $x + $album_size, $y + $album_size, $placeholder_color); } @@ -552,6 +624,4 @@ class AlbumCollageGenerator { return preg_match('/[\x{4E00}-\x{9FBF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}]/u', $text); } -} - -new AlbumCollageGenerator(); \ No newline at end of file +} \ No newline at end of file