Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

laravelio/laravel.io

2497 stars
1 code files
View laravelio/laravel.io on GitHub

app/Jobs/GenerateSocialShareImage.php

Open in GitHub
use App\Models\Article;
use Intervention\Image\ImageManager;
 
final class GenerateSocialShareImage
{
private $article;
 
const TEXT_X_POSITION = 50;
 
const TEXT_Y_POSITION = 100;
 
const TEXT_COLOUR = '#161e2e';
 
const FONT = 'inter.ttf';
 
const FONT_SIZE = 50;
 
const CHARACTERS_PER_LINE = 30;
 
const TEMPLATE = 'social-share-template.png';
 
const CACHE_LIFETIME = 43200;
 
public function __construct(Article $article)
{
$this->article = $article;
}
 
public function handle(ImageManager $image)
{
$text = wordwrap($this->article->title(), self::CHARACTERS_PER_LINE);
 
return $image->cache(function ($image) use ($text) {
$image->make(resource_path('images/'.self::TEMPLATE))
->text($text, self::TEXT_X_POSITION, self::TEXT_Y_POSITION, function ($font) {
$font->file(resource_path('fonts/'.self::FONT));
$font->size(self::FONT_SIZE);
$font->color(self::TEXT_COLOUR);
});
}, self::CACHE_LIFETIME, true)->response('png');
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.