Search by

flyingmana / php-openvc

Flyingmana

PHP extension bindings for OpenCV

Package info

github.com/Flyingmana/php-openvc

Language:C++

Type:php-ext

Ext name:ext-opencv

pkg:composer/flyingmana/php-openvc

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

4.0.0-alpha+1 2026-11-02 20:08 UTC

This package is auto-updated.

Last update: 2026-11-02 21:03:02 UTC


README

Note

This repository is a fork of hihozhou/php-opencv. The fork aims to make the extension compatible with PHP 8.5; that work is in progress and compatibility is not yet guaranteed. The information and installation instructions below are inherited from upstream and may be outdated.

Warning

This fork is developed primarily using AI tooling. Review changes carefully and independently test the extension before relying on it, especially in production.

Build Status Minimum PHP Version

Upstream notice: ⚠️⚠️ NO LONGER IN ACTIVE DEVELOPMENT | 项目不再维护 ⚠️⚠️ Due to personal scheduling reasons, the project has been suspended for maintenance and it is hoped that the project code will provide useful value.

Document

Requirements

  • OpenCV 4.0.0+
  • PHP7.0+

Installation

Install with PIE (Linux/macOS)

Install the OpenCV 4 development libraries and pkg-config first. PIE builds this extension from source, so a C++ compiler and the opencv4 pkg-config metadata must also be available. After this package is published on Packagist, install it with:

pie install flyingmana/php-openvc --with-opencv

Pushing a tag runs the Build PIE binaries workflow, which builds Linux (glibc, x86_64 and arm64, NTS and ZTS) binaries for PHP 8.5 and attaches them to a draft GitHub release; review and publish it manually. PIE uses a matching binary when available and otherwise builds from source. The binaries link dynamically against the OpenCV libraries of the build image (Ubuntu 24.04), so the target system needs compatible OpenCV runtime libraries; if not, use pie build to compile from source.

Windows PIE installations require prebuilt extension binaries; this project does not currently publish them.

PHP 8.5 development build (Linux/WSL)

The PHP 8.5 port is experimental. Use matching PHP 8.5 CLI, phpize, and php-config installations, a C++ compiler, pkg-config, and OpenCV 4 development libraries including the contrib modules. From this checkout:

php -v
phpize --version
php-config --version
pkg-config --modversion opencv4
phpize
./configure --with-opencv --with-php-config="$(command -v php-config)"
make -j2
php -n -d extension="$PWD/modules/opencv.so" --ri opencv
NO_INTERACTION=1 REPORT_EXIT_STATUS=1 make test

The extension can be tested from modules/opencv.so without installing it system-wide. Check the test results and skips before relying on the build. The PHPT suite has been run in WSL with PHP 8.5.4 and OpenCV 4.7.0. It does not cover GUI interaction, camera access, or Windows builds. The Unix build currently does not enable the optional CV\Face bindings.

Use OpenCV docker(commendatory)

If you don't know how to install OpenCV, you can use my OpenCV docker image(only 300M, including opencv_contrib).

docker pull hihozhou/php-opencv

Compile and install php-opencv extension

git clone https://github.com/hihozhou/php-opencv.git
cd php-opencv
phpize
./configure --with-php-config=your php-config path
make
make install

Configure

php.ini

extension="your opencv.so path"

Example

LBPH face recognition

use CV\Face\LBPHFaceRecognizer;
//use ...;

$src = imread('facePic.jpg');
$gray = cvtColor($src, COLOR_BGR2GRAY);
equalizeHist($gray, $gray);
$faceRecognizer = LBPHFaceRecognizer::create();
/* ... */ //get $images and $labels for train
$faceRecognizer->train($images, $labels);//How to get $image and $labels, see the document
/* ... */ //Face detection using CascadeClassifier
$faceLabel = $faceRecognizer->predict($gray);
/* ... */ //draw face and name

result:

predict

Image Processing

//Obama.php
use function CV\{ imread, imshow, waitkey, namedWindow};

$im = imread('Obama.png');//load image
namedWindow('This is Obama id card',WINDOW_FULLSCREEN);//create window
imshow('This is Obama id card',$im);//show image on window

waitkey(0);

result:

Obama

Load image by gray

$gray = imread('Obama.png',IMREAD_GRAYSCALE);
//or
use  function CV\{ cvtColor};
$gray = cvtColor($im, COLOR_BGR2GRAY);

Obama_gray

You can draw something.
e.g:

use CV\{Mat,Scalar, Point, Size};
use function CV\{ellipse, imwrite, waitKey};
use const CV\{CV_8UC3};

$windowWidth = 600;
$thickness = 2;
$lineType = 8;
$matScalar = new Scalar(0,0,0);
$mat = new Mat($windowWidth, $windowWidth, CV_8UC3, $matScalar);
$point=new Point($windowWidth/2, $windowWidth/2);
$size=new Size($windowWidth/4, $windowWidth/16);
$scalar=new Scalar(255, 129, 0);
for($i = 0; $i <= 360; $i += 45){
    ellipse($mat,$point,$size,$i,0,360,$scalar,$thickness,$lineType);
}
imwrite('./tests/ellipse.png',$mat);

result:

ellipse

Features

  • 1.core
  • 2.imgproc
  • 3.highgui
  • 4.contrib
  • 5.features2d
  • 6.flann
  • 7.gpu
  • 8.calib3d
  • 9.legacy
  • 10.ml
  • 11.nonfree
  • 12.objdetect
  • 13.ocl
  • 14.photo
  • 15.stitching
  • 16.superres
  • 17.ts
  • 18.video
  • 19.Videostab

Contributors

This project exists thanks to all the people who contribute. [Contribute].

感谢

感谢韩天峰老大的指导,
感谢韩天峰老大的指导, 感谢盘古大叔的php7-internal项目以及平常的指导, 感谢木桶技术升级交流群和黑夜路人技术群、以及PHP内核交流的群友对技术的帮助。