php-vips is a binding for libvips 8.7 and later that runs on PHP 7.4 and later

Overview

PHP binding for libvips

Build Status

php-vips is a binding for libvips 8.7 and later that runs on PHP 7.4 and later.

libvips is fast and needs little memory. The vips-php-bench repository tests php-vips against imagick and gd. On that test, and on my laptop, php-vips is around four times faster than imagick and needs 10 times less memory.

Programs that use libvips don't manipulate images directly, instead they create pipelines of image processing operations starting from a source image. When the pipe is connected to a destination, the whole pipeline executes at once and in parallel, streaming the image from source to destination in a set of small fragments.

Install

You need to install the libvips library. It's in the linux package managers, homebrew and MacPorts, and there are Windows binaries on the vips website. For example, on Debian:

sudo apt-get install libvips-dev

Or macOS:

brew install vips

You'll need to enable FFI in your PHP, then add vips to your composer.json:

"require": {
    "jcupitt/vips" : "2.0.0"
}

Example

width\n"; $image = $image->invert(); $image->writeToFile($argv[2]);">
#!/usr/bin/env php

require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

// fast thumbnail generator
$image = Vips\Image::thumbnail('somefile.jpg', 128);
$image->writeToFile('tiny.jpg');

// load an image, get fields, process, save
$image = Vips\Image::newFromFile($argv[1]);
echo "width = $image->width\n";
$image = $image->invert();
$image->writeToFile($argv[2]);

Run with:

$ composer install
$ ./try1.php ~/pics/k2.jpg x.tif

See examples/. We have a complete set of formatted API docs.

How it works

php-vips uses php-ffi to call directly into the libvips binary. It introspects the library binary and presents the methods it finds as members of the Image class.

This means that the API you see depends on the version of libvips that php-vips finds at runtime, and not on php-vips. php-vips documentation assumes you are using the latest stable version of the libvips library.

The previous php-vips version that relied on a binary extension and not on php-ffi is still available and supported in the 1.x branch.

Introduction to the API

Almost all methods return a new image as the result, so you can chain them. For example:

$new_image = $image->more(12)->ifthenelse(255, $image);

will make a mask of pixels greater than 12, then use the mask to set pixels to either 255 or the original image.

Note that libvips operators always make new images, they don't modify existing images, so after the line above, $image is unchanged.

You use long, double, array and image as parameters. For example:

$image = $image->add(2);

to add two to every band element, or:

$image = $image->add([1, 2, 3]);

to add 1 to the first band, 2 to the second and 3 to the third. Or:

$image = $image->add($image2);

to add two images. Or:

$image = $image->add([[1, 2, 3], [4, 5, 6]]);

To make a 2 x 3 image from the array, then add that image to the original.

Almost all methods can take an extra final argument: an array of options. For example:

90]);">
$image->writeToFile("fred.jpg", ["Q" => 90]);

php-vips comes with full API docs. To regenerate these from your sources, type:

$ vendor/bin/phpdoc

And look in docs/.

There are around 300 operations in the library, see the vips docs for an introduction:

https://libvips.org/API/current

Test and install

$ composer install
$ composer test
$ vendor/bin/phpdoc

Regenerate auto docs

$ cd src
$ ../examples/generate_phpdoc.py
Comments
  • final review, and release 1.0

    final review, and release 1.0

    I think everything has now been ticked off the TODO list, we can probably release 1.0.

    How about making a 0.1.2 release with what's in dev, and then leaving it for few weeks. If it seems to be working with no major issues, we'll bump again to 1.0. Use this issue to track minor polishing on the way.

    opened by jcupitt 31
  • writeToBuffer doesn't output edited/removed EXIF data

    writeToBuffer doesn't output edited/removed EXIF data

    Testcase: https://gist.github.com/kleisauke/5a9272198172fc95a47b9add54b0189d

    Test image: https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_6.jpg

    php test.php Landscape_6.jpg
    

    Should output:

    Rotate 90 degrees
    No EXIF Orientation
    No EXIF Orientation
    

    Instead the original orientation is outputted:

    Rotate 90 degrees
    No EXIF Orientation
    Rotate 90 degrees
    

    By the way, Maybe it's better to also include the vips_image_get_typeof in this PHP binding (for example $image->get_typeof($fieldName);). I'm now catching exceptions instead.

    opened by kleisauke 31
  • Having a terrible time trying to install.

    Having a terrible time trying to install.

    When I run "composer install" I get this:

    • Installation request for jcupitt/vips 1.0.2 -> satisfiable by jcupitt/vips[v1.0.2].
      • jcupitt/vips v1.0.2 requires ext-vips >=0.1.2 -> the requested PHP extension vips is missing from your system.

    I look at php-info, and vips installed...

    Then I re-run pecl install vips and it turns out I have a different version installed.

    pecl/vips is already installed and is the same as the released version 1.0.8

    So, I switch the version in my composer.json file, re-run "composer install" and I get this:

    Problem 1 - The requested package jcupitt/vips 1.0.8 exists as jcupitt/vips[dev-add-array-access, dev-add-header-get, dev-add-throws-decls, dev-dev, dev-master, 1.0.x-dev, dev-use-travis-containers, dev-vips-8.6, v0.1.2, v1.0.0, v1.0.1, v1.0.2] but these are rejected by your constraint.

    I followed your directions and end up version conflicts. How do I fix?

    Any help is much appreciated.

    Wyatt

    opened by wyatt121 24
  • FFI\Exception : Failed loading 'libvips.so.42' after upgrade to v2

    FFI\Exception : Failed loading 'libvips.so.42' after upgrade to v2

    Hello,

    I've just upgraded a PHP 8.1 app from v1 to v2 and am now getting the following exception:

    FFI\Exception : Failed loading 'libvips.so.42'
    .../vendor/jcupitt/vips/src/Config.php:310
    .../vendor/jcupitt/vips/src/Config.php:195
    .../vendor/jcupitt/vips/src/Config.php:259
    .../vendor/jcupitt/vips/src/Image.php:712
    

    I've enabled FFI like so:

    FFI support => enabled
    
    Directive => Local Value => Master Value
    ffi.enable => On => On
    ffi.preload => no value => no value
    

    I also removed the vips php extension, libvips is on version 8.12.2 and was installed via Homebrew.

    I'd appreciate any pointers when you find some time. Thanks!

    opened by boris-glumpler 23
  • PHP 8 Support

    PHP 8 Support

    Hi, I was wondering if there are installation instructions available for PHP 8. We use php-vips heavily while on 7.1, but are trying to upgrade to PHP 8. I'm struggling to find available documentation for this.

    enhancement 
    opened by jesseforrest 23
  • Apply image filters like sepia, black-white

    Apply image filters like sepia, black-white

    Hello @jcupitt

    I want to add sepia, black & white, vintage etc.. effect on my image. Is there any method for apply this filters on my image. I am trying it by using conv method is that method is for apply filters?

    question 
    opened by vnkapoor 20
  • Switch to php ffi

    Switch to php ffi

    This gets rid of php-vips-ext and reimplements everything with the new php-ffi module instead. It should be simpler to develop and support, and much easier to deploy.

    It now requires php 7.4 or later (when php-ffi was merged). I've bumped the version to 2.0 since this feels like a large change. The API is the same.

    All tests pass, and it seems stable.

    There are some obvious things we should still do, but I think they can wait until after this is merged.

    • Support preloading, see https://www.php.net/manual/en/class.ffi.php
    • Rewrite the enum and doc generator in php.
    • Add source/target API
    • Add progress callbacks etc.
    • Add mutable.
    opened by jcupitt 19
  • Travis CI integration

    Travis CI integration

    You can see the output of the Travis CI build(s) here: https://travis-ci.org/kleisauke/php-vips/builds/178825558

    It's failing on PHP 7.1 because of this:

    1) VipsConvenienceTest::testVipsMaxpos
    Failed asserting that two arrays are equal.
    --- Expected
    +++ Actual
    @@ @@
     Array (
    -    0 => null
    -    1 => null
    -    2 => null
    +    0 => 6
    +    1 => 2
    +    2 => 1
     )
     
    /home/travis/build/kleisauke/php-vips/tests/convenience.php:123
    

    I'm not sure why that test is failing. At first look I could not find any fault in that test case.

    opened by kleisauke 15
  • memory leak and errors

    memory leak and errors

    I have a server running Ubuntu 22.04 where I have apache and php installed. I have recently started using libvips for php, and I have noticed that every time a php script is run, the amount of ram used increases more and more, without being freed. I am forced to use Vips\Config::shutDown() at the end of the script otherwise the server ram gets saturated. I tried to use Vips\Config::cacheSetMax(5) but nothing changes (memory grows more and more).

    php file example:

    <?php
    require __DIR__ . '/vendor/autoload.php';
    use Jcupitt\Vips;
    
    $image = Vips\Image::newFromFile($inputImagePath);
    //some operations on the width and height of the image
    //.....
    $image = Vips\Image::thumbnail($inputImagePath, $width, ['height' => $height, 'crop' => 'centre']);
    $image->writeToFile($outputImagePath, ['Q' => $quality]);
    

    Also sometimes libvips returns an error:

    (process:211949): GLib-GObject-WARNING **: 16:54:47.542: cannot register existing type 'VipsObject'
    (process:211949): GLib-CRITICAL **: 16:54:47.542: g_once_init_leave: assertion 'result != 0' failed
    (process:211949): GLib-GObject-CRITICAL **: 16:54:47.542: g_type_register_static: assertion 'parent_
    type > 0' failed
    (process:211949): GLib-CRITICAL **: 16:54:47.542: g_once_init_leave: assertion 'result != 0' failed
    

    libvips42 version: 8.12.1 php-vips version: 2.0.3

    opened by GabrieleOlmi 14
  • Proper use of \Jcupitt\Vips\Image class

    Proper use of \Jcupitt\Vips\Image class

    Shoud class \Jcupitt\Vips\Image instance need to close or destroy? I found some php-fpm process was not terminated after use this class/extension...

    My Code as follow:

    $image = Vips\Image::newFromBuffer($imgBlobStr);
    $image = $image->crop(0, $y, $width, $height)->resize($ratio);
    $buffer = $image->jpegsave_buffer();
    $image = null;
    
    header('Content-Type: image/jpeg');
    echo $buffer;
    

    Please correct me if I am wrong. Thanks

    question 
    opened by bluesoulx 14
  • affine should have a background parameter, and not have jaggy edges

    affine should have a background parameter, and not have jaggy edges

    I try to rotate an image by arbitrary angle. Basically works fine with just similarity, but the borders look pretty jagged, compared to what for example imagick produces.

    Any hints to make them smoother? With VIPS: vips With IMagick: imagick

    I'd also like to have a user-definable "background" layer instead of just an fully transparent alpha channel. For now I do this with:

                            $aa = $this->vips->extract_band(3);
                            $this->vips = $aa->ifthenelse($this->vips, $background);
    

    But I assume this doesn't really work nicely, in case I happen to have smooth borders with hopefully also smoothness on the alpha channel for the rotated image. How would I put the rotated image on top of the background image (which can have an alpha channel as well)? Basically a compositeImage operation in IMagick, I assume.

    enhancement 
    opened by chregu 14
  • gifload: no property named dpi

    gifload: no property named dpi

    Hello and thank you for the amazing binding.

    I am trying to make a "universal" loader which accepts all kinds of images, but for example I would like to set those that have a property named dpi to a value of 300.

    If I add an option called dpi to the Vips\Image::newFromFile options, and I am loading a normal gif image, I get a gifload: no property named dpi error.

    I already use Vips\Image::findLoad to detect the kind of image I'm loading before I set the options. But I was wondering if it's possible to check if a dpi option exists for a specific loader (so I wouldn't have to hardcode the option exceptions for gif/jpg/etc)?

    opened by ethaniel 4
  • FFI\\Exception(code: 0): Failed loading 'libvips.42.dylib' after upgrade to v2 on M1 Mac

    FFI\\Exception(code: 0): Failed loading 'libvips.42.dylib' after upgrade to v2 on M1 Mac

    Have read through this issue and tried everything, but haven't had any luck...

    [2022-12-05 16:49:29] local.ERROR: Failed loading 'libvips.42.dylib' {"userId":1245,"exception":"[object] (FFI\\Exception(code: 0): Failed loading 'libvips.42.dylib' at ...vendor/jcupitt/vips/src/FFI.php:739)
    [stacktrace]
    .../vendor/jcupitt/vips/src/FFI.php(739): FFI::cdef('// we need the ...', 'libvips.42.dyli...')
    .../vendor/jcupitt/vips/src/FFI.php(116): Jcupitt\\Vips\\FFI::init()
    /vendor/jcupitt/vips/src/Utils.php(100): Jcupitt\\Vips\\FFI::vips()
    /vendor/jcupitt/vips/src/Image.php(709): Jcupitt\\Vips\\Utils::filenameGetFilename('/private/var/tm...')
    

    I've enabled FFI as instructed.

    I've removed the older vips php extension.

    libvips is 8.13.3

    php-vips is 2.1.0

    php is 8.1

    Any ideas on things I can try to troubleshoot? All out of ideas at the moment.

    opened by johnvoncolln 4
  • Sharpen images

    Sharpen images

    Hi @jcupitt Thanks for your help, and please bear with me, I have some questions.

    1 How do you sharpen images (avif)? the equivalent of Imagick::sharpenImage().

    2 Any other optimisation is welcome.

    3 I have a script that creates images on demand, when I create an image then I read it with file_get_contents() it does not send an image file (or at least so it looks to me) to the browser, instead the file is a bunch of gibberish characters. why? and how could I "echo" the resulting image without reading the created file?

    opened by sadektouati 5
  • AVIF file size is bigger than it's WebP equivalent

    AVIF file size is bigger than it's WebP equivalent

    Here's the code I use, require_once('vendor/autoload.php'); use Jcupitt\Vips; $image = Vips\Image::thumbnail(ORIGINAL_FILEPATH, $width, ['height' => 10000]); $image->writeToFile(FULL_FILEPATH); Why every single AVIF file has a bigger size than webp?

    And also I noticed that Gmagick is a lot faster, which is ironic for me. What I'm doing wrongly?

    opened by sadektouati 6
  • SEGFAULT after the inclusion of FFI::shutdown() to release memory consumption

    SEGFAULT after the inclusion of FFI::shutdown() to release memory consumption

    Running libvips 8.13.3 on Debian bullseye with PHP 8.1 under Apache using jcupitt/vips:2.10

    Calling FFI::shutdown() at the end of a script after an Image::Thumbnail() operation results in the process SEGFAULT-ing.

    The call to shutdown() is required as without it the memory consumption climbs and is never released back to the system (at least from our experiments).

    There are more details and a full reproduction case, including Dockerfile to match the environment: https://github.com/ingenerator/libvips-segfault-repro

    Thanks to @acoulton: Failing tests proving either case here: https://github.com/ingenerator/libvips-segfault-repro/actions/runs/3444654670/jobs/5747485403

    And a further attempt to debug the source of the SEGFAULT here: https://github.com/ingenerator/libvips-segfault-repro/pull/3

    We have followed as much of the documentation as we can find around usingphp-vips appropriately however, it may be that it is not suited to running in this environment, in this way (ie mpm-prefork / multiuser). While happy to spend a little more time debugging, if you have any suggestions of where to go next with it? It would be good to know if it is at least intended to work this way?

    opened by craig410 18
  • [question] How to use True Streaming in PHP?

    [question] How to use True Streaming in PHP?

    Hello @jcupitt hope to find you well.

    Could you please guide me how to use True Streaming in PHP?

    I read the release notes, but could not figure how to do in PHP, tried looking in the source code but did not go to far.

    My use case is a thumbnailing service on S3, source and target images stay on S3.

    I already have it working, but do the old way:

    • first I get the S3 object with AWS SDK PHP
    • then use newFromBuffer
    • then do the operations with thumbnail_image
    • then $image->writeToFile (I am storing on disk for now, but the goal is to store on S3)

    Could you please share some insights?

    Have a good day.

    enhancement help wanted 
    opened by andrefelipe 10
Owner
libvips
A fast image processing library with low memory needs.
libvips
The SensioLabs DeprecationDetector runs a static code analysis against your project's source code to find usages of deprecated methods, classes and interfaces

SensioLabs DeprecationDetector CAUTION: This package is abandoned and will no longer receive any updates. The SensioLabs DeprecationDetector runs a st

QOSSMIC GmbH 389 Nov 24, 2022
QaraTMS is open source test case, test suites, test plans and test runs management tool.

QaraTMS - Open Source Test Management System QaraTMS is open source test management software for managing test suites, test cases, test plans, test ru

Alex H 29 Dec 22, 2022
This Plugin runs commands when a player does a certain emote

The EmoteCommands Plugin This Plugin runs commands when a player does a certain emote You will need a pocketmine server of at least version 4.0.0 Usag

DiamondStrider1 1 Jan 24, 2022
Backwards compatibility Extension and Library for PHP 8.x and later

colopl_bc Provides various compatibility functions required for PHP (temporary) migration. WARNING This extension is intended for temporary use only.

COLOPL,Inc. 10 Jun 13, 2023
Allow any Discord user to sign in to your website and save their discord user information for later use.

Simple Discord SSO ( Single Sign-On ) Requires at least: 5.0 Tested up to: 5.8.3 Stable tag: 1.0.2 Requires PHP: 7.4 License: GPLv2 or later License U

null 2 Oct 7, 2022
All about docker projects either from dockerfile or compose. Anyway, here the project is in the form of a service, for the programming language I will make it later

Docker Project by ItsArul Hey, yo guys okay, this time I made some projects from Docker. Anyway, this project is open source, for example, if you want

Kiyo 10 Nov 4, 2022
Disclaimer: The documentation of this plugin is English at the moment, but I might go for Latin later down the line, just for the fun of it.

Quiritibus Plugin This repository is storing the custom plugin developed for the Quiritibus Latin Magazine website, currently being developed at: http

Alkor András 1 Jan 19, 2022
Save items in a bucket, retrieve them later.

Bucket Save items in a bucket, retrieve them later. use Laragear\Bucket\Facades\Buckets; use App\Models\Message; public function send(Message $messag

Laragear 2 Jun 3, 2022
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

Ernest Buffington 7 Aug 28, 2022
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
This project is very diverse and based upon many languages and libraries such as C++, Python, JavaScript, PHP and MQTT

ADMS-Real-time-project This project is very diverse and based upon many languages and libraries such as C++, Python, JavaScript, PHP and MQTT Advance_

Nitya parikh 1 Dec 1, 2021
Php-gamer - A repo with PHP 8.1, Swoole and Laminas. And of course, Docker in the front.

PHP-Gamer Instructions for run this app: First time $ git clone [email protected]:fatorx/php-gamer.git $ cd php-gamer $ chmod +x docker-build.sh $ chmod

Fabio de Souza 6 Oct 6, 2022
Private, self-hosted Composer/Satis repository with unlimited private and open-source packages and support for Git, Mercurial, and Subversion.

Private, self-hosted Composer/Satis repository with unlimited private and open-source packages and support for Git, Mercurial, and Subversion. HTTP API, HTTPs support, webhook handler, scheduled builds, Slack and HipChat integration.

Łukasz Lach 112 Nov 24, 2022
YesilCMS is based on BlizzCMS and specifically adapted for VMaNGOS Core and includes new features and many bug fixes.

YesilCMS · YesilCMS is based on BlizzCMS and specifically adapted for VMaNGOS Core and includes new features and many bug fixes. Features In addition

yesilmen 12 Jan 4, 2023
WPForms coding standards are based on the WordPress Coding Standards and the PHPCompatibility Coding Standards and help create strict and high-quality code.

WPForms coding standards are based on the WordPress Coding Standards and the PHPCompatibility Coding Standards and help create strict and high-quality code.

Awesome Motive, Inc. 7 Nov 29, 2022
A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel

Underscore.php The PHP manipulation toolbelt First off : Underscore.php is not a PHP port of Underscore.js (well ok I mean it was at first). It's does

Emma Fabre 1.1k Dec 11, 2022
:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

sabre.io 532 Dec 25, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
PeachPie - the PHP compiler and runtime for .NET and .NET Core

PeachPie Compiler The open-source PHP compiler to .NET If you run into any inconsistencies, bugs or incompatibilities, kindly let us know and we'll do

PeachPie Compiler Platform 2.1k Dec 22, 2022