A PHP GD + TwitterOAuth CLI app to dynamically generate Twitter header images and optionally upload them via the API. This enables you to build cool little tricks, like showing your latest followers or sponsors, your latest content created, a qrcode to something, a progress bar for a goal, and whatever you can think of.
Other types of dynamic banners can also be generated. Dynacover uses erikaheidi/gdaisy for image manipulation based on templates.
The demo is explained in detail in this guide: How to Dynamically Update Twitter Cover Image to Show Latest Followers Using PHP GD and TwitterOAuth. Please notice that the version that is compatible with this tutorial is 0.1.0, the initial release. Newer versions use erikaheidi/gdaisy as dependency to work with JSON templates.
- PHP (cli only) 7.4+
- GD / PHP-GD
- Curl
- Composer
You also need to register an application within the Twitter Developers Portal and obtain 4 tokens:
- Consumer / App Token
- Consumer / App Secret
- User / Access Token
- User / Access Token Secret
Additionally, you can set up a Personal GitHub API key to fetch your sponsors, in case you are enrolled in the GitHub Sponsors program, and you want to use the included cover_sponsors.json
template. This is optional.
Installation is done in a few steps: clone, install dependencies, set up credentials, and you are ready to go.
Start by cloning this repository to your local PHP dev environment or remote PHP server:
git clone https://github.com/erikaheidi/dynacover.git
cd dynacover
composer install
This will install a few dependencies and generate a credentials.php
file in the root folder of your application.
Open the generated credentials.php
file and set up your credentials:
#credentials.php
<?php
return [
'twitter_consumer_key' => 'APP_CONSUMER_KEY',
'twitter_consumer_secret' => 'APP_CONSUMER_SECRET',
'twitter_user_token' => 'USER_ACCESS_TOKEN',
'twitter_token_secret' => 'USER_ACCESS_TOKEN_SECRET',
'github_api_bearer' => 'GITHUB_API_BEARER_TOKEN'
];
The github_api_bearer
token is optional, only in case you want to use the GitHub API to fetch Sponsors.
Replace the keys accordingly and save the file.
To test that your credentials are valid, you can list your latest followers with:
php dynacover fetch followers
If everything is set up correctly, you will see a list with your 10 latest followers.
To preview your cover without uploading it to Twitter, run:
php dynacover generate twitter
This will use the default cover_basic.json
template. You can specify a template with the template=template_path
parameter:
php dynacover generate twitter template=app/Resources/templates/cover_neon.json
Built-in templates are located in the app/Resources/templates
directory. You can also create your own templates in any preferred location and pass the template json path as parameter, relative to the application root folder. Check the included templates to see how it works. Ideally, you should choose a default template and set it up within the dynacover
script configuration.
Covers are generated in the root of the application folder, with the name latest_header.png
. Check the generated image before uploading it to Twitter to confirm it has your latest followers, and it looks like you expect.
The default template shows latest 5 Twitter followers.
Similar to the basic cover, but with a more colorful background. Shows latest 5 Twitter followers.
This template shows your latest 5 Twitter followers in smaller size, in a blue-neon style header.
This template uses the Github image source to obtain sponsors and include them in the banner. Make sure you have set up your GH token on the credentials.php
file.
To upload the latest generated cover to twitter, run:
php dynacover cover upload
To generate and update your cover using the template that is configured as default in the app config, run:
php dynacover cover update
For this to be completely dynamic and update frequently, you'll need to include the script to your Crontab or equivalent. First make sure you set up the default_template
config parameter in the dynacover
script to your preferred template. By default, this is app/Resources/templates/cover_basic.json
.
To open the current user's crontab, run:
crontab -e
This will open up a text editor. You should include the full paths to both the php
executable and the dynacover
script, like in this example which will update the cover every 5 minutes:
*/5 * * * * /usr/bin/php /home/erika/dynacover/dynacover cover update > /dev/null 2>&1
The "interactions banner" is generated based on your recent interactions and can be limited to only include mutuals (people that follows you and you follow them back).
php dynacover generate interactions
For mutuals only, include the --mutuals
flag:
php dynacover generate interactions --mutuals
Please notice that the "mutuals" version may have a limited set of results after filtering your latest interactions (~200 mentions).