Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update your twitter profile banner with your last 5 followers profile images
<?php
namespace App\Commands;
use Abraham\TwitterOAuth\TwitterOAuth;
use Imagick;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateTwitterBanner extends Command
{
protected static $defaultName = 'twitter:banner:update';
public function __construct(
private string $TWITTER_CONSUMER_KEY,
private string $TWITTER_CONSUMER_SECRET,
private string $TWITTER_OAUTH_SECRET,
private string $TWITTER_OAUTH_KEY
)
{
parent::__construct();
}
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = new TwitterOAuth(
$this->TWITTER_CONSUMER_KEY,
$this->TWITTER_CONSUMER_SECRET,
$this->TWITTER_OAUTH_KEY,
$this->TWITTER_OAUTH_SECRET
);
$file = new Imagick();
$file->newImage(1500, 500, '#fefefe');
$ids = array_slice($connection->get('followers/ids')->ids, 0, 5);
$initialX = 1500 - (140 * 5);
foreach ($ids as $i => $id) {
$profileImageURL = $connection->get('users/show', ['user_id' => $id])->profile_image_url_https;
$imageBlob = file_get_contents($profileImageURL);
$profileImage = new Imagick();
$profileImage->readImageBlob($imageBlob);
$profileImage->borderImage('#1da1f1', 3, 3);
$profileImage->scaleImage(120, 120);
$file->compositeImage($profileImage, Imagick::COMPOSITE_OVER, $initialX + $i * 140, 500 - 185);
$profileImage->clear();
}
$draw = new \ImagickDraw();
$draw->setFillColor(new \ImagickPixel('#0a0a0a'));
$draw->setFontSize(40);
$draw->setFontFamily('Helvetica');
$draw->setFontWeight(700);
$file->annotateImage($draw, $initialX, 295, 0, 'Latest Followers:');
$draw->setFillColor(new \ImagickPixel('#666666'));
$draw->setFontSize(30);
$draw->setFontFamily('Helvetica');
$draw->setFontStyle(Imagick::STYLE_ITALIC);
$draw->setFontWeight(400);
$file->annotateImage($draw, $initialX + 305, 472, 0, 'Updates every 60 seconds');
$file->setImageFormat('jpg');
$connection->post('account/update_profile_banner', [
'width' => 1500,
'height' => 500,
'offset_top' => 0,
'offset_left' => 0,
'banner' => base64_encode($file->getImageBlob())
]);
$file->clear();
if ($connection->getLastHttpCode() !== 200) {
var_dump($connection->getLastHttpCode());
}
return Command::SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment