Add Type Hints: Improve code readability and help with static type checking by adding type hints.
Modular Logging Setup: Move the logging setup to a separate function for better modularity.
Error Handling Improvements: Add more specific error handling and informative logging.
Environment Variable Loading: Ensure environment variables are loaded only once and not inside functions.
Check for API Key Validity: Add a check to validate the Tinyfy API key before using it.
Optimize Image Saving: Use a temporary file to avoid saving directly to the filesystem.