Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image gen changes #34

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.ko.footballupdater.configuration.TeamProperties;
import com.ko.footballupdater.models.ImageStatEntry;
import com.ko.footballupdater.models.PlayerMatchPerformanceStats;
import com.ko.footballupdater.models.PostType;
import com.ko.footballupdater.models.Post;
import com.ko.footballupdater.models.PostType;
import com.ko.footballupdater.models.Team;
import com.ko.footballupdater.models.form.HorizontalTranslation;
import com.ko.footballupdater.models.form.ImageGenParams;
import com.ko.footballupdater.models.form.StatisticEntryGenerateDto;
import com.ko.footballupdater.models.form.VerticalTranslation;
import com.ko.footballupdater.repositories.TeamRepository;
import com.ko.footballupdater.utils.DateTimeHelper;
import com.ko.footballupdater.utils.LogHelper;
import com.ko.footballupdater.utils.PostHelper;
Expand All @@ -22,7 +21,13 @@
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -51,6 +56,7 @@ public class ImageGeneratorService {

@Autowired
private TeamHelpers teamHelpers;

@Autowired
private ImageGeneratorProperties imageGeneratorProperties;

Expand All @@ -59,6 +65,7 @@ public class ImageGeneratorService {

@Autowired
private TeamProperties teamProperties;

private final Map<String, BufferedImage> teamLogoCache = new HashMap<>();
private final Map<String, BufferedImage> iconCache = new HashMap<>();
private final Map<String, BufferedImage> baseImageCache = new HashMap<>();
Expand All @@ -77,28 +84,33 @@ public void generatePlayerStatImage(Post post) throws Exception {
BufferedImage selectedBaseImage = null;
try {
if (post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam() != null) {
image = getImageUsingCache(post.getPlayer().getName().replaceAll(" ", "") + BASE_IMAGE_FILE_NAME, imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam().replaceAll(" ", "") + "/", baseImageCache);
image = cloneBufferImage(getImageUsingCache(post.getPlayer().getName().replaceAll(" ", "") + BASE_IMAGE_FILE_NAME, imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam().replaceAll(" ", "") + "/", baseImageCache));
selectedBaseImage = cloneBufferImage(image);
}
} catch (IOException | IllegalArgumentException ex) {
log.atDebug().setMessage("No team specific base image found" + post.getPlayer().getName()).log();
}

if (image == null) {
if (selectedBaseImage == null) {
// Use default base image for player
try {
image = getImageUsingCache(post.getPlayer().getName().replaceAll(" ", "") + BASE_IMAGE_FILE_NAME, imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + "/", baseImageCache);
image = cloneBufferImage(getImageUsingCache(post.getPlayer().getName().replaceAll(" ", "") + BASE_IMAGE_FILE_NAME, imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + "/", baseImageCache));
selectedBaseImage = cloneBufferImage(image);
} catch (IOException ex) {
log.atDebug().setMessage("No player base image found" + post.getPlayer().getName()).log();
}
if (image == null) {
// Generic base image
image = getImageUsingCache(imageGeneratorProperties.getGenericBaseImageFile(), imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + "/", baseImageCache);
// Clone image from cache to prevent cached image from being updated instead
image = cloneBufferImage(getImageUsingCache(imageGeneratorProperties.getGenericBaseImageFile(), imageGeneratorProperties.getExternalImageStoreUri() + BASE_IMAGE_DIRECTORY + imageGeneratorProperties.getInputPath() + "/", baseImageCache));
selectedBaseImage = cloneBufferImage(image);
}
}

if (selectedBaseImage == null) {
throw new Exception(post.getPlayer().getName() + " - Unable to determine base image");
}

drawAllStatsPlayerName(image, post);

// Match stats to image
Expand Down Expand Up @@ -750,9 +762,9 @@ public BufferedImage getImageUsingCache(String fileName, String fileLocation, Ma
return cachedImage;
}

public static BufferedImage cloneBufferImage(BufferedImage image) throws Exception {
public static BufferedImage cloneBufferImage(BufferedImage image) throws IllegalArgumentException {
if (image == null) {
throw new Exception("Unable to clone image");
throw new IllegalArgumentException("Unable to clone image");
}
BufferedImage clone = new BufferedImage(image.getWidth(),
image.getHeight(), image.getType());
Expand Down
48 changes: 48 additions & 0 deletions src/main/resources/application-sample.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
server.port=8081
logging.level.root=INFO

spring.jpa.hibernate.ddl-auto=update

simplejavamail.transport.mode.logging.only=false

ENDPOINT_SECRET=secret

DASHBOARD_PASSWORD=password

MAILER_IS_ENABLED=false
MAILER_SUBJECT=Subject
MAILER_FROM_NAME=FromName
MAILER_FROM_ADDRESS=Email
MAILER_FROM_PASSWORD=Password
MAILER_TO_NAME=ToName
MAILER_TO_ADDRESS=ToAddress

DATASOURCE_PRIORITY = SOFASCORE,FOTMOB,FBREF

IG_POST_ACCOUNT_NAME=IgAccountName
IG_POST_DEFAULT_HASHTAGS=#helloworld

IMAGE_GENERATOR_ENABLED=false
IMAGE_GENERATOR_INPUT_PATH=/input
IMAGE_GENERATOR_OUTPUT_PATH=/output
IMAGE_GENERATOR_EXTERNAL_IMAGE_STORE_URI=S3Bucket/dev

AWS_ACCESS_KEY=Key
AWS_SECRET_KEY=Secret
AWS_S3_OBJECT_KEY_PREFIX=generated-images/
AWS_S3_ENVIRONMENT=dev

FACEBOOK_API_CLIENT_ID=ClientId
FACEBOOK_API_CLIENT_SECRET=ClientSecret
FACEBOOK_API_RESPONSE_TYPE=code
FACEBOOK_API_SCOPE=instagram_basic,instagram_content_publish,pages_read_engagement,business_management,ads_management
FACEBOOK_API_REDIRECT_URI=http://localhost:8081/facebook/auth
FACEBOOK_API_INSTAGRAM_USER_ID=UserId

TIKTOK_API_CLIENT_KEY=ClientId
TIKTOK_API_CLIENT_SECRET=ClientSecret
TIKTOK_API_SCOPE=video.publish
TIKTOK_API_REDIRECT_URI=http://localhost:8081/tiktok/auth
TIKTOK_API_PRIVACY_LEVEL=SELF_ONLY

TEAM_NAME_SUFFIXES_TO_REMOVE=Any
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
logging.level.root=INFO

#spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${SPRING_DATASOURCE_HOST:localhost}:3306/${SPRING_DATASOURCE_NAME:db_football_updater}
spring.datasource.url=jdbc:mysql://${SPRING_DATASOURCE_HOST:localhost}:${SPRING_DATASOURCE_PORT:3306}/${SPRING_DATASOURCE_NAME:db_football_updater}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME:springuser}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD:password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Expand Down
Loading