1
0

added album blacklist, support for PNG covers

This commit is contained in:
anon 2024-10-26 12:26:44 +02:00
parent 23560bb1fc
commit ed1b9fdb17

View File

@ -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);
$artist_placeholders = array_fill(0, count($this->blacklisted_artists), '%s');
$artist_placeholders_str = implode(', ', $artist_placeholders);
// Merge all parameters for $wpdb->prepare
// 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 '%%¥%%'
@ -310,12 +330,64 @@ class AlbumCollageGenerator {
$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);
}
@ -553,5 +625,3 @@ class AlbumCollageGenerator {
}
}
new AlbumCollageGenerator();