Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

Generate Social Media Link Preview Images From a Webpage With PHP

Generate a webpage’s social preview image with PHP GD, publish it publicly, and connect it to the page using Open Graph metadata.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use PHP’s GD extension to create a PNG, save it at a stable public URL, then put that URL in the webpage’s og:image metadata. Image creation and social preview metadata are separate steps: generating a file alone does not tell a platform to use it. GD’s availability and supported formats depend on how PHP was built.

How the PHP image and webpage metadata work together

The Open Graph Protocol lets a webpage describe itself as a rich object. Its four basic properties are og:title, og:type, og:image, and og:url. The image is generated by your PHP code; the page HTML points social platforms to it.

As an Amazon Associate I earn from qualifying purchases.

  1. Create the image with GD and save it as a PNG, or stream it directly with PHP’s imagepng function.
  2. Make the saved image accessible at a stable, publicly fetchable URL.
  3. Add that full URL as the page’s og:image value alongside the other Open Graph properties.
  4. Check image dimensions and file size against the rules of the platform where you intend to share the page.

The examples below use a saved asset because a persistent URL is straightforward to place in page metadata and serve independently of the PHP generation request.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check that PHP supports GD and PNG

PHP’s image functions require GD support in the PHP build. Available formats can vary with the installed GD version and related libraries, so confirm support in the same environment that will run the code. Consult the PHP GD documentation and the PHP imagepng documentation.

If GD is unavailable, enable or install a PHP build with GD support through your hosting environment, or use an image-generation route already supported by your application. Do not assume that a format supported on a development machine is supported on production.

Create a PNG with PHP GD

This runnable example creates a 1200 × 627 PNG with a solid background and text, then saves it as social-preview.png beside the PHP script. It uses PHP’s built-in bitmap font for a minimal dependency-free example; for production design, you can use GD’s text functions with a TrueType font and your own layout.

<?php
$width = 1200;
$height = 627;
$image = imagecreatetruecolor($width, $height);

if ($image === false) {
    http_response_code(500);
    exit('Could not create image');
}

$background = imagecolorallocate($image, 22, 44, 74);
$foreground = imagecolorallocate($image, 255, 255, 255);

imagefill($image, 0, 0, $background);
imagestring($image, 5, 50, 50, 'A generated social preview', $foreground);

$outputPath = __DIR__ . '/social-preview.png';
$saved = imagepng($image, $outputPath);
imagedestroy($image);

if (!$saved) {
    http_response_code(500);
    exit('Could not save PNG');
}

echo 'Saved ' . $outputPath;

Deploy the resulting file where your web server can serve it publicly, then use its full HTTPS URL in the page metadata. The example checks image creation and save success; production code should also handle its chosen output directory, permissions, and any design-specific text or image inputs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add Open Graph tags to the webpage

Put metadata in the page’s <head>. Replace the example URLs and text with values for the actual page. Do not emit duplicate conflicting values: the Open Graph Protocol says the first tag takes preference when values conflict.

<head>
  <title>A useful page title</title>
  <meta property="og:title" content="A useful page title">
  <meta property="og:type" content="website">
  <meta property="og:url" content="https://example.com/article">
  <meta property="og:image" content="https://example.com/social-preview.png">
  <meta property="og:description" content="A short description of the page.">
</head>

The URL in og:image must identify the image, not the PHP source file, unless that PHP endpoint is deliberately configured to return the image. Keep the image reachable to social crawlers; a path that works only in a logged-in browser will not provide a usable public preview.

Save the PNG or stream it from PHP

imagepng can write to a filesystem path or stream; omitting its file argument sends image data directly as output. Saving gives you a stable asset URL to reference from metadata. Streaming is useful when an endpoint is intentionally responsible for serving the generated image, but then the endpoint’s response must be handled as an image rather than as an HTML page.

For a basic streaming endpoint, send the image content type before output and ensure no warnings, whitespace, or other text is printed into the response:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
$image = imagecreatetruecolor(1200, 627);
$background = imagecolorallocate($image, 22, 44, 74);
imagefill($image, 0, 0, $background);

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

Streaming does not itself add the endpoint URL to a webpage’s Open Graph metadata. If you choose this approach, put the endpoint’s publicly accessible URL in og:image.

Choose dimensions for the target platform

There is no single platform-wide image size established by the sources cited here. LinkedIn’s website-sharing guidance lists a minimum of 1200 × 627 pixels, recommends a 1.91:1 ratio, and sets a 5 MB maximum for sharing-module images. These are LinkedIn-specific figures, not universal social preview requirements. LinkedIn also says images under 401 pixels wide display as thumbnails.

LinkedIn’s help page was last updated in 2024, according to the page’s update label as accessed in 2026. Its separate specifications for single-image advertisements are not the same as the website-sharing module rules. For other social and messaging platforms, check their current official guidance rather than assuming LinkedIn’s requirements apply.

Common problems and fixes

  • Undefined GD functions: The PHP runtime executing the script may not have GD enabled. Check the deployment’s PHP build and enable GD there; a different local PHP configuration does not establish production support.
  • PNG creation or save fails: Confirm that the build supports PNG and that PHP can write to the target directory. Check the result of imagepng rather than assuming the file was created.
  • The social card has no image: Confirm that og:image contains the full public image URL and that the URL serves the image rather than a PHP error or an HTML page.
  • The page shows outdated or unexpected metadata: Inspect the rendered page head for duplicate Open Graph properties. The protocol gives the first conflicting value preference, so remove duplicates or make their order and values intentional.
  • The image is rejected or displayed as a thumbnail on LinkedIn: Compare it with LinkedIn’s sharing-module requirements: minimum 1200 × 627 pixels, recommended 1.91:1 ratio, and maximum 5 MB; images below 401 pixels wide are identified as thumbnail display.
  • Output is corrupted when streaming: Ensure the content-type header is sent before the image bytes and prevent notices, debug text, or whitespace from being emitted into the response.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is to capture a webpage as an image rather than design a branded graphic with GD, ScreenshotNeo is a website screenshot API and MCP server. A GET request returns a screenshot or PDF; it can also remove known consent banners, newsletter popups, and chat widgets before capture. The generated image still needs to be referenced as og:image on your page.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, this cURL request captures a webpage as WebP:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

See the ScreenshotNeo documentation for API details and response behavior. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for the free plan.

Implementation checklist

  • Verify GD and PNG support in the PHP environment that will run the generator.
  • Generate and save a valid image, checking for creation and write errors.
  • Serve the image at a stable public URL.
  • Set one intentional value each for og:title, og:type, og:url, and og:image.
  • Check the image against the intended platform’s own current limits.

Frequently Asked Questions

Can PHP create the preview image dynamically on every request?

Yes. PHP’s imagepng can stream image data, but the page still needs to reference the image-serving endpoint in og:image.

Does creating a PNG automatically make it appear in a social link preview?

No. The webpage must expose the image URL through metadata such as og:image.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Is 1200 × 627 the right size for every social platform?

No. That is LinkedIn’s stated minimum for its website-sharing module; requirements vary by platform.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.