The WebP Mime Type

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

23208 views
d

By. Jacob

Jacob Kristensen (Turbulentarius) is a Web Developer based in Denmark. He is currently pursuing a Bachelor's degree in Web Development at Zealand, focusing on learning React and refining his existing skills.

Edited: 2026-10-16 10:17

MIME type of WebP images

The MIME type (also known as the Content-Type) 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. Consequently, browser compatibility is generally no longer a significant concern for ordinary websites.

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 is particularly important 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.

Historically, some services, including LinkedIn, have had problems processing WebP images. Because support can change, it is sensible to test the relevant services rather than assume they behave like ordinary browsers.

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.

Note. You can use Beamtic's file handler class to automate image delivery. It can redirect clients to alternative image formats, including JPEG and AVIF, depending on their capabilities.

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 'WebP is explicitly accepted by the client.';
}

On older versions of PHP, you can use strpos():

if (strpos($accept, 'image/webp') !== false) {
    echo 'WebP is explicitly accepted by the client.';
}

Notice the strict comparison with false. The strpos() function returns 0 when the substring appears at the beginning of the string, so using a loose comparison could produce incorrect results.

However, this simple check is only suitable for detecting an explicitly listed format. It does not fully interpret HTTP content negotiation rules, including wildcards and quality values.

For example, a client might explicitly reject WebP using image/webp;q=0, or accept it through a wildcard such as image/*. A production implementation should account for these possibilities rather than relying exclusively on substring matching.

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 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');
}

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

imagedestroy($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 should first composite it onto a background color before converting.

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 also deliver it directly to the client:

$image = imagecreatefromwebp('/var/www/mysite/images/example.webp');

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

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

imagejpeg($image, null, 80);
imagedestroy($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 that do not explicitly support WebP to its JPEG version.

For example, once a JPEG alternative exists, a PHP application could perform a temporary redirect:

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.

In my earlier tests, LinkedIn had problems retrieving WebP preview images, including situations where the image URL redirected to JPEG.

These were observations of the service's behavior at the time and should not be treated as a permanent limitation. Support and crawler behavior can change.

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.

Deleting unnecessary image files

If you have generated alternative JPEG files that are no longer required, you can locate them using the Linux find command.

For example, to list all JPEG files in a directory and its subdirectories:

find /path/to/directory -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -print

After reviewing the results and confirming that the files are no longer needed, you can delete them:

find /path/to/directory -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -delete

The parentheses ensure that both file extensions are matched correctly. Without proper grouping, the logical operators in a find expression may not behave as expected.

Warning. Do not delete JPEG files merely because WebP alternatives exist. They may still be referenced by older pages, social media metadata, external websites, or other applications. Make sure that the files are genuinely unused before deleting them, and keep a backup if necessary.

Which image delivery method is best?

For most modern websites, using the HTML picture element is the simplest and most reliable method for delivering alternative image 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.

Ultimately, 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