Skip to content

Instantly share code, notes, and snippets.

@nuernbergerA
Created December 22, 2023 15:13
Show Gist options
  • Save nuernbergerA/01ada31ab1ba135314cb46321f0bdace to your computer and use it in GitHub Desktop.
Save nuernbergerA/01ada31ab1ba135314cb46321f0bdace to your computer and use it in GitHub Desktop.
OgImageController
<?php
namespace App\Http\Controllers;
use App\Models\PhpFunction;
use Intervention\Image\AbstractFont;
use Intervention\Image\Facades\Image;
class OgImageController
{
public function __invoke(PhpFunction $phpFunction)
{
$iconPath = $phpFunction->origin === 'internal' ? resource_path('images/php.png'): resource_path('images/laravel.png');
$repositoriesCount = $phpFunction->references()->groupBy('code_repository_id')->count();
$examplesCount = $phpFunction->references()->count();
return
//Image::make(resource_path('images/gh.png'))
Image::canvas(1200, 600, '#ffffff')
->insert($iconPath)
->text($phpFunction->name, 320, 128, function(AbstractFont $font) use ($phpFunction) {
$length = mb_strlen($phpFunction->name);
$size = 64;
$limit = 23;
if($length > $limit) {
$size = $size / $length * $limit;
}
$font->file(resource_path('fonts/arial_bold.ttf'));
$font->size($size);
$font->color('#2f363d');
$font->align('left');
$font->valign('center');
})
->insert(resource_path('images/code.png'), 'bottom-left', 75, 225)
->text("$examplesCount snippets", 325, 335, function(AbstractFont $font) {
$font->file(resource_path('fonts/arial.ttf'));
$font->size(32);
$font->color('#6e7681');
$font->align('right');
$font->valign('center');
})
->insert(resource_path('images/repo.png'), 'bottom-left', 475, 225)
->text("$repositoriesCount repositories", 765, 335, function(AbstractFont $font) {
$font->file(resource_path('fonts/arial.ttf'));
$font->size(32);
$font->color('#6e7681');
$font->align('right');
$font->valign('center');
})
->insert(resource_path('images/footer.png'), 'bottom-center')
->response();
}
}
@nuernbergerA
Copy link
Author

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