PHPackages                             rosell-dk/webp-convert-and-serve - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. rosell-dk/webp-convert-and-serve

ActiveLibrary[Image &amp; Media](/categories/media)

rosell-dk/webp-convert-and-serve
================================

Convert and serve jpeg/png to webp with PHP (if at all possible)

0.4.0(7y ago)38941[5 issues](https://github.com/rosell-dk/webp-convert-and-serve/issues)1MITPHPCI failing

Since Apr 18Pushed 7y ago1 watchersCompare

[ Source](https://github.com/rosell-dk/webp-convert-and-serve)[ Packagist](https://packagist.org/packages/rosell-dk/webp-convert-and-serve)[ RSS](/packages/rosell-dk-webp-convert-and-serve/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (3)Versions (6)Used By (1)

WebP Convert and Serve
======================

[](#webp-convert-and-serve)

This library is now part of [WebP Convert](https://github.com/rosell-dk/webp-convert/) and thus obsolete!
=========================================================================================================

[](#this-library-is-now-part-of-webp-convert-and-thus-obsolete)

[![Build Status](https://camo.githubusercontent.com/fbd8ac5cc44341252d9ca43fd69e5e3e786b22cdfd6e253df02129bef4c54c73/68747470733a2f2f7472617669732d63692e6f72672f726f73656c6c2d646b2f776562702d636f6e766572742e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/rosell-dk/webp-convert-and-serve)

This library can be used for converting and serving WebP images instead of jpeg/png. It is based on [WebPConvert](https://github.com/rosell-dk/webp-convert), which takes care of the conversion. On top of that, it adds a method for serving the converted image with options on how to handle conversion failures.

The task of serving is in itself actually quite small. It can be implemented in a few lines, like this:

```
try {
    $success = WebPConvert::convert($source, $destination, $options);
    if ($success) {
        header('Content-type: image/webp');
        readfile($destination);
    }
}
```

***But then comes error handling.***

If conversion fails, it will make sense to serve the source image instead (if that exists, that is). For that, we need to inspect the extension in order to provide the correct Content-type header. Also, we want to add headers that tells the browser not to cache it. And what if the source file isn't even available? This should be handled as well. Tedious stuff, that this library takes care of.

API
---

[](#api)

### WebPConvertAndServe::convertAndServe($source, $destination, $options)

[](#webpconvertandserveconvertandservesource-destination-options)

*Note: This method was added in 0.4.0. The old one, `convertAndServeImage()` still works, but is depreciated.*

ParameterTypeDescription*$source*StringAbsolute path to source image (only forward slashes allowed)*$destination*StringAbsolute path to converted image (only forward slashes allowed)*$options*ArrayArray of conversion options. See below#### The *$options* argument

[](#the-options-argument)

The options argument is a named array. *WebPConvertAndServe* has just two available options (*fail* and *critical-fail*). However, the options will be handed over to *WebPConvert*. So Any option available in webp-convert are available here.

##### *fail*

[](#fail)

Indicate what to serve, in case of normal conversion failure. Default value: *"original"*

Possible valuesMeaning"original"Serve the original image."404"Serve 404 status (not found)"report-as-image"Serve an image with text explaining the problem"report"Serve a textual report explaining the problemInstead of the string values (ie "original"), you can also use the following constants: *WebPConvertAndServe::$ORIGINAL*, *WebPConvertAndServe::$HTTP\_404*, *WebPConvertAndServe::$REPORT\_AS\_IMAGE* and *WebPConvertAndServe::$REPORT*

##### critical-fail

[](#critical-fail)

Possible values: Same as above, except that "original" is not an option. Default value: *"404"*

##### Return value

[](#return-value)

Number indicating what was served. On fail or critical fail, the value will be one of the following constants: following constants *WebPConvertAndServe::$ORIGINAL*, *WebPConvertAndServe::$HTTP\_404*, *WebPConvertAndServe::$REPORT\_AS\_IMAGE* and *WebPConvertAndServe::$REPORT*. On success, it will be *WebPConvertAndServe::$CONVERTED\_IMAGE*. All fail constants are negative. The success constant is positive – so you can test success with a `if ($returnValue > 0)`

Example:
========

[](#example)

```
require 'vendor/autoload.php';

use WebPConvertAndServe\WebPConvertAndServe;

$source = __DIR__ . '/logo.jpg';
$destination = $source . '.webp';
$options = [
    'fail' => 'original',
    'critical-fail' => '404',

    // You can specify any WebPConvert option here - such as defining a converters array, which
    // is needed, if you need to use a cloud converter
    'converters' => [
        [
            'converter' => 'ewww',
            'options' => [
                'key' => 'blah',
            ],
        ],
        'cwebp',
        'gd'
    ];

$status = WebPConvertAndServe::convertAndServe($source, $destination, $options);
```

Installing
==========

[](#installing)

`composer require rosell-dk/webp-convert-and-serve`

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Total

5

Last Release

2815d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/25ffe2ed22c1d5b6b8e613362ec48025addf819a1a9ab04e6d5e11e5d33f7943?d=identicon)[rosell-dk](/maintainers/rosell-dk)

---

Top Contributors

[![rosell-dk](https://avatars.githubusercontent.com/u/17592708?v=4)](https://github.com/rosell-dk "rosell-dk (33 commits)")

---

Tags

gdimagickimagespngjpgWebpcwebpimage-conversionjpg2webppng2webp

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rosell-dk-webp-convert-and-serve/health.svg)

```
[![Health](https://phpackages.com/badges/rosell-dk-webp-convert-and-serve/health.svg)](https://phpackages.com/packages/rosell-dk-webp-convert-and-serve)
```

###  Alternatives

[rosell-dk/webp-convert

Convert JPEG &amp; PNG to WebP with PHP

6038.1M54](/packages/rosell-dk-webp-convert)[rosell-dk/webp-convert-cloud-service

Cloud service for converting JPEG &amp; PNG to WebP

545.6k3](/packages/rosell-dk-webp-convert-cloud-service)[codeblog/conveter-php-webp

Easy WebP conversion library for PHP

124.1k1](/packages/codeblog-conveter-php-webp)[intervention/image

PHP Image Processing

14.3k194.3M2.2k](/packages/intervention-image)[league/glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.

2.6k51.2M116](/packages/league-glide)[picqer/php-barcode-generator

An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.

1.8k25.5M88](/packages/picqer-php-barcode-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
