Coder Social home page Coder Social logo

webp-convert's Introduction

WebP Convert

Latest Stable Version Minimum PHP Version Build Status Quality Score Software License

Convert JPEG & PNG to WebP with PHP

This library enables you to do webp conversion with PHP using cwebp, gd, imagick, ewww cloud converter or the open source wpc cloud converter. It also allows you to try a whole stack โ€“ useful if you do not have control over the environment, and simply want the library to do everything it can to convert the image to webp.

In addition to converting, the library also has a method for serving converted images, and we have instructions here on how to set up a solution for automatically serving webp images to browsers that supports webp.

NOTE: This master branch contains code for the upcoming 2.0 release. It is not stable at all.

Installation

Require the library with Composer, like this:

composer require rosell-dk/webp-convert

Converting images

To convert an image, using a stack of converters, use the WebPConvert::convert method. It is documented in docs/api/convert.md.

Here is an example:

<?php

// Initialise your autoloader (this example is using Composer)
require 'vendor/autoload.php';

use WebPConvert\WebPConvert;

$source = __DIR__ . '/logo.jpg';
$destination = __DIR__ . '/logo.jpg.webp';

$success = WebPConvert::convert($source, $destination, [
    // It is not required that you set any options - all have sensible defaults.
    // We set some, for the sake of the example.
    'quality' => 'auto',
    'max-quality' => 80,
    'converters' => ['cwebp', 'gd', 'imagick', 'wpc', 'ewww'],  // Specify conversion methods to use, and their order

    'converter-options' => [
        'ewww' => [
            'key' => 'your-api-key-here'
        ],
        'wpc' => [
            'api-version' => 1,
            'url' => 'https://example.com/wpc.php',
            'api-key' => 'my dog is white'
        ]
    ]

    // more options available! - see the api
]);

NOTE: In 2.0.0, WebPConvert will not return any value. Failure is handled purely by exceptions (1.3, also throws exceptions, but not when the failure is that no converters were operational)

To convert using a specific conversion method, simply set the converters option so it only has that method.

The conversion methods (aka "converters") are documented here: docs/converters.md.

Serving converted images

The convertAndServe method tries to serve a converted image. If there already is an image at the destination, it will take that, unless the original is newer or smaller. If the method cannot serve a converted image, it will serve original image, a 404, or whatever the 'fail' option is set to - and return false. It also adds a X-WebP-Convert-Status header, which allows you to inspect what happened.

Example:

<?php
$success = WebPConvert::convertAndServe($source, $destination, [
    'fail' => 'original',     // If failure, serve the original image (source).
    //'fail' => '404',        // If failure, respond with 404.
    //'show-report' => true,  // Generates a report instead of serving an image

    // Besides the specific options for convertAndServe(), you can also use the options for convert()
]);

To see all options, look at the API: docs/api/convert-and-serve.md

WebP on demand

The library can be used to create a WebP On Demand solution, which automatically serves WebP images instead of jpeg/pngs for browsers that supports WebP. To set this up, follow what's described in this tutorial.

WebP Convert in the wild

WebP Convert is used in the following projects:

Wordpress integration

A cloud service based on WebPConvert

Kirby CMS integration

Supporting WebP Convert

Bread on the table don't come for free, even though this library does, and always will. I enjoy developing this, and supporting you guys, but I kind of need the bread too. Please make it possible for me to have both:

Become a backer or sponsor on Patreon.

webp-convert's People

Contributors

rosell-dk avatar s1syphos avatar quentin-st avatar bologer avatar lucaslealdev avatar mrbig00 avatar

Watchers

James Cloos avatar Marcin Krawiec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.