chladog

payload webp

by chladog
Media & images
v1
^1.6.13
GPL-3.0

Overview

Summary generated from the README

Adds a webp field to upload-enabled collections and converts uploaded JPEG, PNG and WebP images to WebP using sharp. Conversion runs in the background by default or can be awaited, and is configured through plugin options. It targets Payload 1.x and recommends pinning a matching sharp version in package resolutions.

Features

  • Converts images to WebP including each configured image size
  • Configurable sharp WebP options, mime types and target collections
  • Custom resize options per image size and a maximum size for the main image
  • Optional sync mode, overwrite behavior and EXIF metadata retention
  • Hooks after conversion and after storage for external storage adapters
  • GraphQL mutation to regenerate WebP images for existing uploads

Use cases

  • Reduce image file sizes on a Payload 1.x site by serving WebP versions
  • Regenerate WebP files for an existing media collection after changing compression settings
  • Send converted WebP buffers to an external storage provider through hooks

From the author

payloadcms/payload plugin for automatic image conversion to webp format

Installation

npm i payload-webp

89

Stars

6

Forks

2

Open Issues

3 years ago

Last Updated

1.0k

Weekly Downloads

v1.1.6

npm Version

99.1 KB

Unpacked Size

3

Dependencies

GitHub stars

89+4 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 202689
Dec 4, 202689
Dec 3, 202689
Dec 2, 202689
Dec 1, 202689
Nov 30, 202689
Nov 29, 202689
Nov 28, 202689
Nov 27, 202689
Nov 26, 202689
Nov 25, 202689
Nov 24, 202690
Nov 23, 202690
Nov 22, 202690
Nov 21, 202690
Nov 20, 202690
Nov 19, 202690
Nov 18, 202690
Nov 17, 202690
Nov 16, 202690
Nov 15, 202690
Nov 14, 202690
Nov 13, 202690
Nov 12, 202690
Nov 11, 202690
Nov 10, 202690
Nov 9, 202690
Nov 8, 202690
Nov 7, 202690
Nov 6, 202690
Nov 5, 202690
Nov 4, 202690
Nov 3, 202690
Nov 2, 202690
Nov 1, 202690
Oct 31, 202690
Oct 30, 202690
Oct 29, 202690
Oct 28, 202690
Oct 27, 202690
Oct 26, 202690
Oct 25, 202690
Oct 24, 202690
Oct 23, 202690
Oct 22, 202690
Oct 21, 202689
Oct 20, 202689
Oct 19, 202689
Oct 18, 202689
Oct 17, 202689
Oct 16, 202689
Oct 15, 202689
Oct 14, 202689
Oct 13, 202689
Oct 12, 202689
Oct 11, 202688
Oct 10, 202688
Oct 9, 202688
Oct 8, 202688
Oct 7, 202688
Oct 6, 202688
Oct 5, 202688
Oct 4, 202688
Oct 3, 202688
Oct 2, 202688
Oct 1, 202688
Sep 30, 202688
Sep 29, 202688
Sep 28, 202688
Sep 27, 202688
Sep 26, 202688
Sep 25, 202688
Sep 24, 202688
Sep 23, 202688
Sep 22, 202688
Sep 21, 202688
Sep 20, 202688
Sep 19, 202688
Sep 18, 202688
Sep 17, 202688
Sep 16, 202688
Sep 15, 202688
Sep 14, 202688
Sep 13, 202687
Sep 12, 202687
Sep 11, 202687
Sep 10, 202687
Sep 9, 202687
Sep 8, 202686
Sep 7, 202685
Sep 6, 202685
npm weekly downloads

845+679 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 2026845
Dec 4, 2026706
Dec 3, 2026624
Dec 2, 2026665
Dec 1, 2026679
Nov 30, 2026676
Nov 29, 2026688
Nov 28, 2026762
Nov 27, 2026890
Nov 26, 20261,068
Nov 25, 20261,196
Nov 24, 20261,261
Nov 23, 20261,269
Nov 22, 20261,294
Nov 21, 20261,132
Nov 20, 20261,127
Nov 19, 20261,036
Nov 18, 2026992
Nov 17, 2026913
Nov 16, 2026941
Nov 15, 2026817
Nov 14, 2026922
Nov 13, 2026898
Nov 12, 2026760
Nov 11, 2026599
Nov 10, 2026601
Nov 9, 2026565
Nov 8, 2026734
Nov 7, 2026707
Nov 6, 2026645
Nov 5, 2026642
Nov 4, 2026904
Nov 3, 2026905
Nov 2, 2026972
Nov 1, 20261,032
Oct 31, 20261,185
Oct 30, 20261,134
Oct 29, 20261,044
Oct 28, 2026735
Oct 27, 2026758
Oct 26, 2026686
Oct 25, 2026574
Oct 24, 2026320
Oct 23, 2026234
Oct 22, 2026268
Oct 21, 2026321
Oct 20, 2026297
Oct 19, 2026298
Oct 18, 2026144
Oct 17, 2026168
Oct 16, 2026183
Oct 15, 2026193
Oct 14, 2026197
Oct 13, 2026198
Oct 12, 2026209
Oct 11, 2026319
Oct 10, 2026331
Oct 9, 2026398
Oct 8, 2026435
Oct 7, 2026491
Oct 6, 2026498
Oct 5, 2026486
Oct 4, 2026428
Oct 3, 2026560
Oct 2, 2026514
Oct 1, 2026534
Sep 30, 2026468
Sep 29, 2026513
Sep 28, 2026518
Sep 27, 2026457
Sep 26, 2026296
Sep 25, 2026284
Sep 24, 2026206
Sep 23, 2026171
Sep 22, 2026165
Sep 21, 2026160
Sep 20, 2026230
Sep 19, 2026289
Sep 18, 2026284
Sep 17, 2026457
Sep 16, 2026487
Sep 15, 2026443
Sep 14, 2026445
Sep 13, 2026422
Sep 12, 2026340
Sep 11, 2026331
Sep 10, 2026164
Sep 9, 2026150
Sep 8, 2026147
Sep 7, 2026145
Sep 6, 2026166

Each point is npm's rolling 7-day download count.

Health Score

53/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/457314719-root.svg)](https://payloaddirectory.dev/plugins/457314719-root)
HTML
<a href="https://payloaddirectory.dev/plugins/457314719-root"><img src="https://payloaddirectory.dev/badge/457314719-root.svg" alt="Payload Directory"></a>
Updated Jul 25, 2023Created Feb 9, 2022Published Jun 27, 2023

README

payload-webp

NPM Version Download Count

payloadcms/payload plugin for automatic image conversion to webp format.

Getting started

  1. Add sharp to resolutions field in package.json to prevent versions mismatch*:
  "resolutions": {
    "sharp": "latest"
  }

*PayloadCMS' sharp dependency version is often behind. This is a problem since sharp does not work with multiple versions in a single project and old versions are also a security risk. Solution is setting the wanted sharp version in resolutions field.

  1. Install the package with npm i payload-webp OR yarn add payload-webp

  2. Import the plugin to your payload.config.ts:

import webp from "payload-webp";

// you may use this as fallback in your resizeOptsFactory
import { defaultResizeFactory } from "payload-webp";

export default buildConfig({
  ...
  plugins: [
      webp(webpPluginOptions)
  ]
)};
  1. After uploading images to your upload-enabled collection new field called webp is added with converted image => webp file meteadata and its sizes. Access webp field with graphql like so:
query {
  allMedia {
    docs {
      url               # url of original file [jpeg/png/webp]
      filesize          # filesize of original file
      webp {
        url             # url of webp processed file [webp]
        filesize        # filesize of webp processed file
        sizes {
          thumbnail {
            width
            height
            url
          }
        }
      }
    }
  }
}

By default webp images are being processed to reduce their filesize as well.

Plugin options

Optionally you can pass JSON with following plugin options to tweak compression or limit conversion to particular mimeTypes or specific upload-enabled collections.

interface WebpPluginOptions {
  /**
   * Function that takes current image size to be resized and returns a sharp resize options object.
   * Can be used to modify particular image size sizing, cropping, fitting etc.
   *
   * Example:
```JS
  resize: (imageSize) => {
      // fit to preserve aspect ratio if image size is xs
      if (imageSize.name === 'xs') {
        return {
          width: imageSize.width,
          height: imageSize.width,
          options: {
            fit: "inside"
          }
        }
      }
      // fallback to payload's default behavior
      return defaultResizeFactory(imageSize);
  }
```*/
  resizeOptsFactory?: (imageSize: ImageSize) => {
    width: number;
    height: number;
    options?: sharp.ResizeOptions;
  };

  /**
   *  If present the main webp image will be sized to the given dimensions.
   */
  maxResizeOpts?: { width?: number; height?: number; options?: sharp.ResizeOptions };

  /**
   * Which mime types convert to webp.
   * Defaults to: ```["image/jpeg", "image/png", "image/webp"]```
   *
   * ***image/webp** is compressed*
   */
  mimeTypes?: string[];

  /**
   * sharp webp options
   * defaults to:
   * ```
   * {
   *    nearLossless: true,
   *    quality: 50,
   *    force: true,
   * }```
   */
  sharpWebpOptions?: sharp.WebpOptions;

  /**
   * Array of collection slugs that should have images converted to webp.
   * By default all collections with upload property will convert images to webp.
   */
  collections?: CollectionConfig['slug'][];

  /**
   * By default image conversion happens asynchronously in the background for faster UX.
   * By switching this flag the hook and following request response will await for the image conversion.
   */
  sync?: boolean;

  /**
   * When true log messages for debugging purposes.
   */
  debug?: boolean;

  /**
   * Filename conflict behavior.
   *
   * When ```true``` the existing files will be overwritten.
   *
   * When ```false``` ```+i``` will be added to the filename.
   *
   */
  overwrite?: boolean;

/**
 * Default: When ```false``` EXIF metadata will be removed in the output.
 *
 * When ```true``` EXIF metadata will be kept in the output.
 * _```orientation``` tag will be striped in either case as the image will be rotated based on its value during processing._
 */
  metadata?: boolean;

    /**
   * Hooks that run for each file at specific processing step.
   */
  hooks?: {
    /**
     * This hook is run immediatelly after image conversion. The converted image file is in memory in ```bufferObject``` property.
     * You can use this hook to store the files in the cloud.
     */
    afterConversion?: (result: ResultObject) => any;
    /**
     * This hook is run immediatelly after storing files successfully. The converted image file is still in the memory in ```bufferObject``` property.
     * You can use this hook to run post-processing on the stored file.
     */
    afterStorage?: (result: ResultObject) => any;
  };
}

Regenerate Webp images

You can regenerate existing images with following GraphQL mutation:

mutation {
  WebpRegenerate(slug: String!, sort: String) {
    currentFile
    current
    total
  }
}

Arguments

ArgumentDescription
slugUpload collection slug in camelCase
sortYou can pass the sort parameter to set the direction in which images will be regenerated. Default: createdAt

Fields and subsequent calls

You can use returned fields to show notify of current progress to user. Any subsequent call while regeneration of particular collection is in the progress will not start new regeneration process, but will return current progress.

Buffer objects

You can access each buffer object of processed image in hooks that you can set by Plugin Options. This way your adapter can store the files with external provider for an instance.

Similar plugins