Update
This commit is contained in:
@@ -7,8 +7,11 @@
|
||||
use Artesaos\SEOTools\Facades\OpenGraph;
|
||||
use Artesaos\SEOTools\Facades\TwitterCard;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response as HttpResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Intervention\Image\Drivers\Imagick\Driver;
|
||||
|
||||
class FrontMemeController extends Controller
|
||||
{
|
||||
@@ -164,18 +167,232 @@ public function show(string $slug): Response
|
||||
OpenGraph::setTitle($title);
|
||||
OpenGraph::setDescription($description);
|
||||
OpenGraph::setUrl(request()->url());
|
||||
//OpenGraph::addProperty('type', 'video.other');
|
||||
//OpenGraph::addImage($meme->webp_url);
|
||||
OpenGraph::addProperty('type', 'video.other');
|
||||
OpenGraph::addImage(route('memes.og', $meme->ids));
|
||||
|
||||
// Twitter Card
|
||||
TwitterCard::setTitle($title);
|
||||
TwitterCard::setDescription($description);
|
||||
//TwitterCard::setType('summary_large_image');
|
||||
//TwitterCard::setImage($meme->webp_url);
|
||||
TwitterCard::setType('summary_large_image');
|
||||
TwitterCard::setImage(route('memes.og', $meme->ids));
|
||||
|
||||
return Inertia::render('memes/show', [
|
||||
'meme' => $meme,
|
||||
'relatedMemes' => $relatedMemes,
|
||||
]);
|
||||
}
|
||||
|
||||
public function generateOG(string $ids): HttpResponse
|
||||
{
|
||||
// Decode the hashids to get the meme media ID
|
||||
$memeId = hashids_decode($ids);
|
||||
|
||||
if (!$memeId) {
|
||||
abort(404, 'Meme not found');
|
||||
}
|
||||
|
||||
// Get the meme media
|
||||
$meme = MemeMedia::where('id', $memeId)
|
||||
->where('is_enabled', true)
|
||||
->first();
|
||||
|
||||
if (!$meme) {
|
||||
abort(404, 'Meme not found');
|
||||
}
|
||||
|
||||
// Load the template image
|
||||
$templatePath = public_path('memefast-og-template.jpg');
|
||||
|
||||
if (!file_exists($templatePath)) {
|
||||
abort(404, 'Template image not found');
|
||||
}
|
||||
|
||||
// Create ImageManager with Imagick driver
|
||||
$manager = new ImageManager(new Driver());
|
||||
|
||||
// Load the template image
|
||||
$image = $manager->read($templatePath);
|
||||
|
||||
// Ensure RGB colorspace for proper color rendering
|
||||
$imagick = $image->core()->native();
|
||||
$imagick->setColorspace(\Imagick::COLORSPACE_SRGB);
|
||||
|
||||
// Load the meme image from URL
|
||||
if ($meme->webp_url) {
|
||||
try {
|
||||
// Use cURL to get the image content with proper headers
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $meme->webp_url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MemeFast OG Generator)');
|
||||
|
||||
$imageContent = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($imageContent !== false && $httpCode === 200) {
|
||||
$memeImage = $manager->read($imageContent);
|
||||
|
||||
// Image positioning variables
|
||||
$imageX = 0; // Horizontal offset
|
||||
$imageY = 100; // Vertical offset
|
||||
|
||||
// Resize meme image to 1.5x the template height while maintaining aspect ratio
|
||||
$memeImage = $memeImage->scale(height: 1350);
|
||||
|
||||
// Place the meme image vertically centered, horizontally right-justified
|
||||
$image->place($memeImage, 'center-right', $imageX, $imageY);
|
||||
} else {
|
||||
$image->text('HTTP Error: ' . $httpCode, 200, 200, function ($font) {
|
||||
$font->size(20);
|
||||
$font->color('#ff0000');
|
||||
$font->align('center');
|
||||
});
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// If meme image fails to load, add debug text
|
||||
$image->text('Image failed to load: ' . $e->getMessage(), 200, 200, function ($font) {
|
||||
$font->size(20);
|
||||
$font->color('#ff0000');
|
||||
$font->align('center');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Add the meme name as text with proper kerning and line wrapping
|
||||
if ($meme->name) {
|
||||
$fontPath = resource_path('fonts/Geist/Geist-Bold.ttf');
|
||||
|
||||
// Fallback to built-in font if TTF not found
|
||||
if (file_exists($fontPath)) {
|
||||
// Use native Imagick for text kerning with line wrapping
|
||||
$imagick = $image->core()->native(); // Get the native Imagick object
|
||||
|
||||
// First draw the green outline
|
||||
$drawOutline = new \ImagickDraw();
|
||||
$drawOutline->setFillColor('#00FF00');
|
||||
$drawOutline->setFont($fontPath);
|
||||
$drawOutline->setFontSize(70);
|
||||
$drawOutline->setTextAlignment(\Imagick::ALIGN_LEFT);
|
||||
$drawOutline->setStrokeColor('#00FF00');
|
||||
$drawOutline->setStrokeWidth(20); // Thicker stroke for outline effect
|
||||
|
||||
// Then draw the black text on top
|
||||
$draw = new \ImagickDraw();
|
||||
$draw->setFillColor('#000000');
|
||||
$draw->setFont($fontPath);
|
||||
$draw->setFontSize(70);
|
||||
//$draw->setTextKerning(-4); // Negative for tighter kerning
|
||||
$draw->setTextAlignment(\Imagick::ALIGN_LEFT);
|
||||
|
||||
// Text wrapping - 70% of canvas width (1600 * 0.7 = 1120px)
|
||||
$maxWidth = 1120;
|
||||
$text = "Make meme videos instantly with " . $meme->name . " meme with";
|
||||
|
||||
// Split text into words
|
||||
$words = explode(' ', $text);
|
||||
$lines = [];
|
||||
$currentLine = '';
|
||||
|
||||
foreach ($words as $word) {
|
||||
$testLine = $currentLine . ($currentLine ? ' ' : '') . $word;
|
||||
|
||||
// Get text metrics for the test line
|
||||
$metrics = $imagick->queryFontMetrics($draw, $testLine);
|
||||
|
||||
if ($metrics['textWidth'] > $maxWidth && $currentLine !== '') {
|
||||
// Line is too long, save current line and start new one
|
||||
$lines[] = $currentLine;
|
||||
$currentLine = $word;
|
||||
} else {
|
||||
$currentLine = $testLine;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the last line
|
||||
if ($currentLine) {
|
||||
$lines[] = $currentLine;
|
||||
}
|
||||
|
||||
$lines[] .= "MEMEFAST";
|
||||
|
||||
// Text positioning variables
|
||||
$textX = 100; // Horizontal position
|
||||
$textY = 320; // Base vertical position
|
||||
|
||||
// Calculate line height and starting Y position
|
||||
$lineHeight = 75; // Tighter line spacing
|
||||
$totalHeight = count($lines) * $lineHeight;
|
||||
$startY = $textY - ($totalHeight / 2); // Center vertically
|
||||
|
||||
// Draw each line (left-aligned) - first the outline, then the text
|
||||
foreach ($lines as $index => $line) {
|
||||
$y = $startY + ($index * $lineHeight);
|
||||
|
||||
// Check if this is the MEMEFA.ST line
|
||||
if ($line === "MEMEFAST") {
|
||||
|
||||
$extraSizing = 40;
|
||||
|
||||
// Use Bungee font for MEMEFA.ST
|
||||
$bungeeFontPath = resource_path('fonts/Bungee/Bungee-Regular.ttf');
|
||||
|
||||
if (file_exists($bungeeFontPath)) {
|
||||
// Create separate draw objects for Bungee font
|
||||
$bungeeOutline = new \ImagickDraw();
|
||||
$bungeeOutline->setFillColor('#00FF00');
|
||||
$bungeeOutline->setFont($bungeeFontPath);
|
||||
$bungeeOutline->setFontSize(70);
|
||||
$bungeeOutline->setTextAlignment(\Imagick::ALIGN_LEFT);
|
||||
$bungeeOutline->setStrokeColor('#00FF00');
|
||||
$bungeeOutline->setStrokeWidth(20);
|
||||
|
||||
$bungeeText = new \ImagickDraw();
|
||||
$bungeeText->setFillColor('#000000');
|
||||
$bungeeText->setFont($bungeeFontPath);
|
||||
$bungeeText->setFontSize(70 + $extraSizing);
|
||||
$bungeeText->setTextAlignment(\Imagick::ALIGN_LEFT);
|
||||
|
||||
// Draw with Bungee font
|
||||
$bungeeOutline->annotation($textX, $y + $extraSizing, $line);
|
||||
$bungeeText->annotation($textX, $y + $extraSizing, $line);
|
||||
|
||||
$imagick->drawImage($bungeeOutline);
|
||||
$imagick->drawImage($bungeeText);
|
||||
} else {
|
||||
// Fallback to regular font
|
||||
$drawOutline->annotation($textX, $y + $extraSizing, $line);
|
||||
$draw->annotation($textX, $y + $extraSizing, $line);
|
||||
}
|
||||
} else {
|
||||
// Use regular font for other lines
|
||||
$drawOutline->annotation($textX, $y, $line);
|
||||
$draw->annotation($textX, $y, $line);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the drawing to the image (only for non-Bungee lines)
|
||||
$imagick->drawImage($drawOutline); // Draw outline first
|
||||
$imagick->drawImage($draw); // Draw text on top
|
||||
} else {
|
||||
$image->text($meme->name, 400, 450, function ($font) {
|
||||
$font->size(80);
|
||||
$font->color('#000000');
|
||||
$font->align('center');
|
||||
$font->valign('middle');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generate the image and return as response
|
||||
$encodedImage = $image->toJpeg(100);
|
||||
|
||||
return response($encodedImage, 200, [
|
||||
'Content-Type' => 'image/jpeg',
|
||||
'Cache-Control' => 'public, max-age=3600',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user