Download the PHP package bonniss26/lqip without Composer

On this page you can find all versions of the php package bonniss26/lqip. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package lqip

bonniss26/lqip

tests Packagist

Show something in the right colours while a photo is still loading.

Give this library an image and it returns a tiny CSS value, a low-quality image placeholder (LQIP), that you put in background-image. The visitor sees a colour, a gradient or a blurred preview instead of an empty box.

Every mode on four photos and a transparent cut-out

Using Winter CMS? Install bonniss26/wn-imagery-plugin instead. It wraps this library with Twig filters, caching and a settings page.

Contents

Quick start

The placeholder shows behind the <img> until the photo has loaded and covers it. For a CSS background photo, layer the two: the photo paints over the placeholder once it arrives.

Always set background-size: cover. The blur placeholder is a 16 px image, and without it the browser tiles it across the box at 16 px instead of stretching it. toStyleAttribute() (below) includes it for you.

Cache the result. Generating a placeholder decodes the whole photo, at about 20 ms per megapixel with GD. Do it once per image, when it is uploaded or on first use, and store the string. Never generate one on every page view.

Choosing a mode

Pass ['mode' => ...] as the second argument. The picture at the top shows every mode on real images.

Mode Looks like Size Use it when
dominant_color (default) One flat colour ~32 chars You want the cheapest thing that works everywhere
blur A blurred thumbnail ~130 chars You want it to look like the photo. Best choice for most sites
blur + smooth The same, without visible blocks ~530 chars You want the nicest look and can spare the bytes
gradient Four soft colour patches ~256 chars Your PHP cannot encode WebP (see below)
thumbhash A blurred thumbnail, drawn by the client ~28 chars An API or app that decodes ThumbHash itself. Not CSS
css_only Colour plus coarse light/dark layout ~13 chars Experimental. Read the caveats first

dominant_color picks the colour that covers the most of the image. It does not average, so a blue sofa in a white room comes out white, not grey-blue. It has no sense of shape. On a photo with a dark background, like the parrot above, you get the background.

blur is a 16 px thumbnail, blurred and encoded as a WebP data URI. The browser scales it up smoothly, so it reads as an out-of-focus version of the photo. It costs about four times the characters of dominant_color, which is still less than one line of CSS. If your GD or Imagick has no WebP support it falls back to JPEG, which is about 9× larger (~1 KB per image). On such a server use gradient instead. Check with:

Enlarging a 16 px image leaves faint soft blocks (visible in the room photo above). 'smooth' => true wraps the thumbnail in an SVG that blurs it again at display size, the way next/image does, at about four times the size.

gradient samples a 2×2 grid and paints four radial gradients. There is no image data, only CSS colours. Where WebP works, blur is both smaller and better, so gradient mainly exists for servers without WebP.

thumbhash returns a ThumbHash, a base64 string of about 28 characters (a few more for an image with transparency), for clients that decode it themselves: a JSON API feeding a JavaScript front end or a mobile app (decoders exist for JS, Swift, Kotlin and Rust). It is not CSS, so get it with resultForFile() and read ->value(). For a server-rendered page, blur needs no script.

css_only (experimental) packs the whole placeholder into one integer, --lqip:87524, which a stylesheet expands into gradients. The format is Lean Rada's. It is tiny, but:

Unless you are excited about the format, dominant_color at 32 characters is the safer small option.

Usage

Both return a string that is valid inside a CSS background-image list, or null.

Laravel

No service provider is needed. Call it from a helper and let Laravel's cache hold the result:

For many images per page, generate in a queued job after upload rather than inside the request.

Options

Option Default Applies to Meaning
mode dominant_color all dominant_color, gradient, blur, css_only, thumbhash, or your own
driver auto all auto, gd, imagick. See GD or Imagick
background null all A colour ('#ffffff', '#fff', 0xFFFFFF) to put transparent images on first. See transparent images
long_edge 16 blur Longest edge of the thumbnail in px (1-64). Bigger is sharper and longer
quality 25 blur Encode quality (0-100)
blur true blur Blur the thumbnail before encoding
smooth false blur Blur again at display size with an SVG filter. No visible blocks, ~4× the size
format webp blur webp or jpg. Falls back to jpg without WebP support

Out-of-range numbers are clamped. An unknown mode or driver, or a background that is not a colour, returns null.

Image size

Every result also carries the image's size, so you can reserve its space and stop the page jumping when the photo arrives:

This is the size the photo is displayed at. A portrait phone photo is often stored sideways with an EXIF flag to rotate it; getimagesize() reports it the wrong way round, this does not.

Transparent images

Logos and cut-out product shots keep their transparency:

If you know the page colour behind the image, pass it as background. The image is put on that colour first and every mode treats it as opaque, which also gives css_only something to encode.

When something goes wrong

forFile() and forBytes() return null when:

Nothing throws, and nothing emits a PHP warning. Frameworks often turn a warning into an error page, and placeholders are usually rendered inside templates.

One side effect to know about: while GD decodes a large photo, the library raises memory_limit to 512M (a 24 MP photo needs ~100 MB as a bitmap) and puts it back afterwards.

The single exception is a programming mistake. css_only returns a CSS custom property and thumbhash a hash, neither of which is a background-image value, so asking forFile() for them throws NotCssRepresentable. Use resultForFile() / resultForBytes(), which work with every mode and tell you what you got:

For the CSS modes toStyleAttribute() gives background-image:linear-gradient(#b5b2a5,#b5b2a5);background-size:cover, and for thumbhash an empty string.

Content-Security-Policy

blur produces a data: URI, so a site with a strict CSP needs img-src data:. Without it the placeholder is silently not shown. The other modes are plain CSS colours.

GD or Imagick

You need one of ext-gd or ext-imagick. Almost every PHP host has GD.

Driver Speed on large JPEGs Notes
gd ~20 ms per megapixel Everywhere
imagick ~5.5 ms per megapixel, ~3.5× faster Decodes JPEGs at reduced size

The default auto uses Imagick when it is installed and GD otherwise. Both give the same placeholder, give or take a shade, so switching does not invalidate anything you have cached. To pin one:

Phone photos are rotated according to their EXIF orientation with either driver, so a portrait photo gets a portrait placeholder.

Requirements


For developers

The rest of this page is for people who want to know how it works or want to change it.

How it works

Measurements

The defaults come from measurements, not guesses. 40 photographs, 16 px long edge, length of the value actually sent; RMSE is against the source on a 16×16 grid (0-255, lower is closer):

Mode / variant Chars RMSE
dominant_color 32 56.3
blur WebP q25 (default) 131 28.3
blur WebP q50 138 27.3
blur JPEG q50 998 —
blur AVIF q50 478 —
gradient 256 —
css_only 13 —
thumbhash 28 —

Run the benchmark on your own photos:

Extending

Each mode is a class implementing Strategies\LqipStrategy. Register your own, or replace a built-in one, with extend():

PlaceholderGenerator::modes() lists every mode. A custom engine implements Drivers\Driver and Drivers\Image and is installed with useDriver().

Contributing

The test images are generated in code, so there are no binary fixtures. Tests for a driver whose extension is missing are skipped. To run them all, use Docker. This copies the checkout into the container, so your own vendor/ is left alone (bash or zsh; in Git Bash on Windows, put MSYS_NO_PATHCONV=1 in front):

Change 8.3 to test another PHP version (7.4 to 8.5).

CI runs every PHP version from 7.4 to 8.5, with GD and with GD + Imagick.

Ground rules that pull requests are held to:

If you change what a mode outputs, regenerate the picture at the top. It needs headless Chrome and ImageMagick's magick command; the three steps are at the top of docs/render-examples.php.

Ideas on the roadmap: a css_only decode stylesheet (it must be written independently, because the reference one has no licence), and a vips driver if it measures faster than Imagick. See CHANGELOG.md for what changed between versions.

Non-goals

Credits

License

MIT.


All versions of lqip with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bonniss26/lqip contains the following files

Loading the files please wait ...