The WebP Mime Type

Information about the WebP content type, and how we might handle webp images in PHP.

23229 views

Edited: 2026-10-29 21:04

MIME type of WebP images

The MIME type used in the HTTP Content-Type header for WebP images is image/webp, and the standard file extension is .webp.

WebP is a modern image format developed by Google that provides efficient compression for images on the web. It supports both lossy and lossless compression, alpha transparency, and animation.

Compared with traditional JPEG and PNG files, WebP can often achieve significantly smaller file sizes while maintaining similar visual quality. This reduces bandwidth consumption and can improve website loading performance.

WebP is supported by all major modern browsers, including Chrome, Firefox, Edge, and Safari. Apple introduced WebP support with Safari 14 and iOS 14 in September 2020. For ordinary websites, browser compatibility is generally no longer a significant concern.

Serving WebP images with the correct Content-Type

A typical HTTP response for a WebP image may look like this:

HTTP/1.1 200 OK
Content-Type: image/webp
Content-Length: 22454

To deliver an image/webp Content-Type from PHP, you can use the header() function:

header('Content-Type: image/webp');
readfile('/var/www/mysite/images/my-image.webp');
exit();

This tells the client that the response contains a WebP image. When serving static images directly, Apache and Nginx should normally handle the MIME type automatically, provided the server is configured correctly.

Delivering alternative image formats

Although WebP is widely supported, there may still be situations where alternative formats are necessary. Older browsers, image-processing applications, and some social media crawlers may have limited support for particular formats.

This also matters when images are used for social media previews. When a link is shared, a crawler may retrieve the image specified by the page's metadata, such as the og:image property.

There are several ways to provide alternative formats:

  • Use the HTML picture element to let browsers choose a supported format.
  • Use HTTP content negotiation to serve different formats based on the client's capabilities.
  • Provide separate image URLs and use JPEG or PNG where maximum compatibility is required.

Using the picture element

The HTML picture element allows you to provide several versions of an image and let the browser select a format it supports.

For example, you could offer AVIF first, WebP second, and JPEG as a fallback:

<picture>
  <source srcset="/images/example.avif" type="image/avif">
  <source srcset="/images/example.webp" type="image/webp">
  <img src="/images/example.jpg" alt="Example image" width="800" height="600" loading="lazy">
</picture>

The browser examines the type attributes and selects the first format it supports. If none of the sources are suitable, the image specified by the img element is used.

This is generally the simplest approach for supporting multiple image formats in HTML because it requires no PHP processing, HTTP redirects, or browser detection.

The img element also provides the image's alternative text and can specify its dimensions to help prevent layout shifts while the page loads.

Detecting WebP support using the Accept header

For dynamically served images, the HTTP Accept request header can be used to determine which formats a client explicitly accepts.

For example, a browser might send:

Accept: image/avif,image/webp,image/png,image/*,*/*;q=0.8

This indicates that the client accepts several image formats, including AVIF and WebP.

In PHP 8, you can check whether the header explicitly contains image/webp using str_contains():

$accept = $_SERVER['HTTP_ACCEPT'] ?? '';

if (str_contains($accept, 'image/webp')) {
    echo 'The Accept header contains image/webp.';
}

This is only a quick check for the presence of image/webp, not a complete way to determine whether WebP is acceptable. For example, it also matches image/webp;q=0, which explicitly rejects the format.

A production implementation should interpret quality values and wildcards such as image/* rather than rely on substring matching alone. An accepted media type also does not guarantee that the client can process every feature of the format.

Converting WebP to JPEG with PHP

If you need to provide a JPEG alternative to a WebP image, you can convert it using PHP's GD Library.

GD is available as a PHP extension and must be installed and enabled with WebP support. You can check the available image formats using the imagetypes() function:

if (imagetypes() & IMG_WEBP) {
    echo 'WebP is supported by GD.';
}

You can then use imagecreatefromwebp() to load an image and imagejpeg() to convert it to JPEG.

Converting and saving a JPEG file

The following example converts a WebP image to JPEG, places it on a white background, and saves it on the server:

$source = '/var/www/mysite/images/example.webp';
$destination = '/var/www/mysite/images/example.jpg';

$image = imagecreatefromwebp($source);

if ($image === false) {
    throw new RuntimeException('Unable to load WebP image');
}

$jpeg = imagecreatetruecolor(imagesx($image), imagesy($image));

if ($jpeg === false) {
    throw new RuntimeException('Unable to create JPEG canvas');
}

$white = imagecolorallocate($jpeg, 255, 255, 255);
imagefill($jpeg, 0, 0, $white);
imagecopy($jpeg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));

if (!imagejpeg($jpeg, $destination, 80)) {
    throw new RuntimeException('Unable to save JPEG image');
}

The third argument to imagejpeg() specifies the output quality, ranging from 0 to 100. A value of 80 is often a reasonable starting point, although the best setting depends on the image.

One important difference between WebP and JPEG is that JPEG does not support transparency. If the original image contains transparent areas, you need to place it on a background color before converting, as shown above.

Also note that PHP's GD Library does not support decoding animated WebP files with imagecreatefromwebp(). Animated images require a different conversion approach.

Outputting a converted image directly

Instead of saving the converted image, you can deliver it directly to the client. After creating $jpeg as shown above, replace the file-saving step with:

header('Content-Type: image/jpeg');

if (!imagejpeg($jpeg, null, 80)) {
    throw new RuntimeException('Unable to output JPEG image');
}

exit();

In this case, the converted JPEG image is sent directly in the HTTP response without creating a separate file.

Technically, this can work even if the requested URL ends with .webp, because HTTP clients normally use the response's Content-Type to determine the format.

However, delivering JPEG data from a URL ending in .webp may confuse users and applications that rely on file extensions.

It is usually better to serve the JPEG from its own URL or use a format-neutral endpoint when performing dynamic content negotiation.

Redirecting clients to alternative image formats

Another approach is to save the converted image and redirect clients to its JPEG version when that format is more suitable.

For example, once a JPEG alternative exists, a PHP application could perform a temporary redirect. This example only shows the redirect; the application must decide when to use it:

header('Location: /images/example.jpg', true, 307);
exit();

This sends an HTTP 307 Temporary Redirect, instructing the client to retrieve the JPEG version from its own URL.

While this approach keeps file extensions consistent, it also introduces an additional HTTP request. Furthermore, not all crawlers and external services handle redirects in the same way.

For ordinary website images, the picture element is generally preferable. Redirects are more appropriate when you need to preserve existing image URLs or when your application already handles image delivery dynamically.

Why use a 307 redirect?

The 307 Temporary Redirect status code indicates that the requested resource is temporarily available at a different URL.

Unlike a permanent redirect, it does not instruct clients to treat the alternative URL as the permanent location of the resource.

A 307 response also preserves the original HTTP method when the client follows the redirect. For ordinary image requests using GET, a 302 redirect will often behave similarly, but 307 has more explicit method-preservation semantics.

For format-specific redirection, a temporary redirect can be useful because the destination may depend on the client's capabilities rather than a permanent change to the resource's location.

However, be careful with caching. If the redirect depends on the HTTP Accept header, your caching configuration must account for that variation. Otherwise, a redirect intended for one client could incorrectly be reused for another.

Content negotiation and caching

If your application serves different image formats from the same URL based on the client's Accept header, the response should generally include:

Vary: Accept

This informs compatible caches that the response may change depending on the request's Accept header.

Without appropriate cache handling, a proxy or CDN could serve a JPEG response to a client expecting WebP, or serve a WebP response to a client that cannot decode it.

When using separate URLs such as example.webp and example.jpg, this type of content negotiation is usually unnecessary.

WebP and social media preview images

Social media crawlers do not necessarily behave like regular browsers. Even when a service supports WebP uploads or displays WebP images on its website, its link-preview crawler may have different requirements.

For example, a crawler may reject certain image formats, fail to follow redirects, or require a directly accessible image URL.

For maximum compatibility, it is generally sensible to provide a separate JPEG or PNG image for social media previews:

<meta property="og:image" content="https://example.com/images/preview.jpg">

This avoids relying on WebP detection or redirects when external services retrieve your preview images.

Which image delivery method is best?

For ordinary website images, the HTML picture element is usually the simplest way to offer alternative formats.

It allows browsers to select a supported format without requiring server-side image conversion during each request or introducing additional HTTP redirects.

Dynamic content negotiation can still be useful when building an image delivery service or file handler, particularly when you want to manage image formats automatically without modifying the HTML of individual pages.

For social media previews, providing a dedicated JPEG image is a practical way to avoid compatibility problems with external crawlers.

WebP is sufficiently well supported that most websites can use it directly. Whether you need additional fallbacks depends on the browsers, applications, and external services you intend to support.

Links

  1. Downloading and Installing WebP - Google
  2. Image file type and format guide - MDN
  3. The HTML picture element - MDN
  4. PHP imagecreatefromwebp() - PHP Manual

Tell us what you think:

  1. How to use the AVIF image format in PHP; A1 or AVIF is a new image format that offers better compression than WebP, JPEG and PNG, and that already works in Google Chrome.
  2. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  3. How to create a router in PHP to handle different request types, paths, and request parameters.
  4. Tutorial on how to use proxy servers with cURL and PHP
  5. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.

More in: PHP Tutorials