Skip to content

Instantly share code, notes, and snippets.

@themsaid
Created March 23, 2020 09:49
Show Gist options
  • Save themsaid/713d938de53a0ed31c73700c0d3a5564 to your computer and use it in GitHub Desktop.
Save themsaid/713d938de53a0ed31c73700c0d3a5564 to your computer and use it in GitHub Desktop.
OG post images

require "stil/gd-text": "^1.1",

class ControllerClass extends Controller
{
    public function __invoke($slug)
    {
        $post = WinkPost::where('slug', $slug)->first();
        $quickDip = $post->tags()->whereSlug('quick-dip')->first() ;

        $border = 40;

        $image = imagecreatetruecolor(1200, 630);
        $backgroundColor = imagecolorallocate($image, 1, 22, 39);
        imagefill($image, 0, 0, $backgroundColor);

        $box = new Box($image);

        // Diving Laravel
        $box->setFontFace(
            resource_path('fonts/Roboto-Light.ttf')
        );
        $box->setFontColor(new Color(255, 255, 255));
        $box->setTextShadow(new Color(0, 0, 0, 50), 2, 2);
        $box->setFontSize(40);
        $box->setBox($border, $border, 1200 - $border - $border, 630 - $border - $border);
        $box->setTextAlign('left', 'top');
        $box->draw('Diving Laravel'.($quickDip ? ' - Quick Dip' : ''));


        // Title
        $box->setFontFace(
            resource_path('fonts/Roboto-Black.ttf')
        );
        $box->setFontColor(
            $quickDip ? new Color(228, 64, 54) : new Color(255, 255, 255)
        );
        $box->setFontSize(60);
        $box->setBox($border, $border + 130, 1200 - $border - $border, 630 - 120 - $border);
        $box->draw($post->title);

        // Update Date
        $box->setFontFace(
            resource_path('fonts/Roboto-Light.ttf')
        );
        $box->setFontColor(new Color(153, 182, 210));
        $box->setFontSize(35);
        $box->setBox($border, 630 - $border - 38*1.3, 1200 - $border - $border, 630 - $border - $border);
        if ($quickDip) {
            $box->draw('60 sec read - by Mohamed Said');
        } else {
            $box->draw(ReadingTimeCounter::generate(strip_tags($post->body)).' read - by Mohamed Said');
        }

        header("Content-type: image/png");
        imagepng($image);
    }
}
@egorsmkv
Copy link

If someone interested in a server that does the same task but in more general solution, check out my project here: https://github.com/egorsmkv/share-image-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment