定制 jcupitt/vips 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jcupitt/vips

Composer 安装命令:

composer require jcupitt/vips

包简介

A high-level interface to the libvips image processing library.

README 文档

README

CI

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 --no-install-recommends libvips42

(--no-install-recommends stops Debian installing a lot of extra packages)

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.4.0"
}

php-vips does not yet support preloading, so you need to enable FFI globally. This has some security implications, since anyone who can run php on your server can use it to call any native library they have access to.

Of course if attackers are running their own PHP code on your webserver you are probably already toast, unfortunately.

Finally, on php 8.3 and later you need to disable stack overflow tests. php-vips executes FFI callbacks off the main thread and this confuses those checks, at least in php 8.3.0.

Add:

zend.max_allowed_stack_size=-1

To your php.ini.

Example

#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

// handy for Windows
Vips\FFI::addLibraryPath("C:/vips-dev-8.16/bin");

// check libvips version
echo 'libvips version: ' . Vips\Config::version() . PHP_EOL;

// 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 can 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 3 x 2 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:

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

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

$ vendor/bin/phpdoc

And look in docs/.

Unfortunatly, due to php-doc limitations, these do not list every option to every operation. For a full API description you need to see the main libvips documentation:

https://libvips.org/API/current

Test and install

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

Regenerate auto docs

$ cd src
$ ../examples/generate_phpdoc.py

jcupitt/vips 适用场景与选型建议

jcupitt/vips 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.9M 次下载、GitHub Stars 达 705, 最近一次更新时间为 2016 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「processing」 「libvips」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jcupitt/vips 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jcupitt/vips 我们能提供哪些服务?
定制开发 / 二次开发

基于 jcupitt/vips 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.9M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 712
  • 点击次数: 29
  • 依赖项目数: 33
  • 推荐数: 8

GitHub 信息

  • Stars: 705
  • Watchers: 19
  • Forks: 31
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-21