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 URL your web server can serve, then add that full URL to the page’s og:image metadata. Generating the file and telling social platforms to use it are separate steps. GD must be enabled in your PHP build, and image-format support depends on that build.

How the PHP image and webpage metadata work together

The Open Graph Protocol lets a webpage provide information for a rich link preview. Its four basic properties are og:title, og:type, og:image, and og:url. The image file alone does not identify itself as the preview for a page: the page’s metadata must point to it. See the Open Graph Protocol.

This guide uses PHP GD to create a simple PNG, writes it to a public location, and adds Open Graph tags to a page. GD can create, manipulate, and output images, but PHP must be built with GD support and the required format support must be available. Check the PHP GD documentation for the capabilities available in your environment.

1. Check GD support in your PHP environment

Run this from the same PHP environment that will generate the image:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
var_dump(extension_loaded('gd'));
?>

If it prints bool(false), GD is not enabled for that runtime. Enable or install the GD extension using the instructions for your hosting environment, then check again. A command-line PHP installation and the PHP runtime used by a web server can differ, so verify the environment that will run the script.

2. Generate and save a PNG with GD

This standalone example creates a 1200 × 627 PNG with a background and text, then saves it as preview.png. Put the script somewhere PHP can execute it; make sure the destination directory exists and is writable by the PHP process.

<?php
declare(strict_types=1);

if (!extension_loaded('gd')) {
    http_response_code(500);
    exit('The GD extension is not enabled.');
}

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

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

$background = imagecolorallocate($image, 24, 35, 56);
$textColor = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $background);

$title = 'A page worth sharing';
imagestring($image, 5, 48, 48, $title, $textColor);

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

if (!$saved) {
    http_response_code(500);
    exit('Could not write preview.png. Check the path and permissions.');
}

echo 'Saved ' . $outputPath;

PHP’s imagepng() can save a PNG to a path or stream PNG data directly when no file argument is supplied. Refer to the PHP imagepng documentation for its arguments and return behavior. This example saves a file so it can be served independently at a stable URL.

Use a font for more control over typography

imagestring() uses GD’s built-in bitmap fonts, which are suitable for a minimal demonstration but offer limited styling. A design that needs a specific font or more precise layout can use a TrueType font with GD’s FreeType text functions, if FreeType support is available in the PHP build. Check the installed GD capabilities before depending on an optional format or library feature.

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

3. Serve the image and add Open Graph metadata

Deploy the generated file where your web server can serve it, then use its full, publicly fetchable URL in the page’s HTML <head>. Replace the example values with the page’s actual title, canonical URL, description, and image address.

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

Use an absolute image URL that a crawler can retrieve without relying on a visitor’s logged-in session. Keep one intended value for each basic property: the Open Graph Protocol says that when conflicting values are present, the first tag takes preference.

4. Choose dimensions for the destination platform

There is no universal image size established by the sources cited here for every social or messaging platform. LinkedIn’s website-sharing guidance specifies a minimum of 1200 × 627 pixels, recommends a 1.91:1 ratio, and sets a 5 MB maximum for its sharing module. These are LinkedIn-specific values, not general rules for all platforms. See LinkedIn’s sharing-module guidance.

The sample canvas is 1200 × 627, which aligns with those LinkedIn dimensions, but check the current requirements for each other destination you intend to support. LinkedIn’s guidance also says images narrower than 401 pixels display as thumbnails; that detail is relevant when diagnosing small previews.

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.

Save a file or stream the image?

Approach When it fits Considerations
Save a PNG file You want a stable image URL to place in og:image. The directory must be writable, and the web server must serve the file at the URL you publish. You can manage the asset separately from page requests.
Stream PNG output You want a PHP endpoint to return image bytes directly. Send an appropriate image response content type and ensure the endpoint returns only image data. Application routing, caching, and crawler behavior depend on your implementation.

Both output forms are supported by PHP’s imagepng() function. For a basic social preview, a saved file is often simpler because og:image can reference a stable asset URL.

Or skip the browser setup

If your preview image should be a screenshot of a rendered webpage rather than a custom GD design, ScreenshotNeo can return an image from one GET request. See the ScreenshotNeo API documentation for options.

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

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed; an MCP server lets AI agents take screenshots; and 1,000 screenshots a month are free with no card, with paid plans starting at $5 for 3,000. Learn more at ScreenshotNeo or sign up for the free plan.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting

The script reports that GD is not enabled

The PHP runtime running the script lacks the GD extension. Enable GD for that runtime and confirm with extension_loaded('gd'). If the script runs from a web request, check the web server’s PHP environment rather than assuming it matches command-line PHP.

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

The script cannot create or save the image

Check that imagecreatetruecolor() succeeded, that the destination directory exists, and that the PHP process can write there. The example checks the image creation result and the return value from imagepng() so it can report a failure rather than silently claiming success.

The preview has no image or uses an unexpected one

Inspect the rendered page source and confirm that og:image contains the intended full URL, with no duplicate conflicting value appearing earlier. Then check that the image URL is publicly fetchable and points to the generated file, not a local filesystem path.

The preview image is cropped, small, or rejected

Compare the image’s actual dimensions, ratio, and file size with the target platform’s guidance. For LinkedIn sharing, the cited guidance calls for at least 1200 × 627 pixels, recommends 1.91:1, and limits the image to 5 MB. Other platforms may use different specifications.

Frequently asked questions

Does PHP create the preview card itself?

No. PHP can generate the image asset. The webpage’s Open Graph metadata identifies that asset and supplies other preview properties; the destination platform determines how it presents the link.

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

Do I need a third-party PHP image library?

Not for the example here: GD is a PHP image extension that can create and output PNG images when enabled and built with the needed support. Whether another library is preferable depends on the project’s requirements and deployment environment.

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.