PHP tclip extension calls opencv to read and parse image data from memory

Source: Internet
Author: User
Tags gtk centos

Two days ago, I said there was a PHP extension, tclip Image cropping program.
Tclip is committed to face recognition and recognition of prominent areas of images. The biggest difference between Tclip and other image cropping programs is that the face and other prominent areas of the cropped image are retained, however, the image is still cropped.
Tclip depends on the Open Source opencv Library. Encyclopedia: OpenCV Full name: Open Source Computer Vision Library. OpenCV is a (open-source)-based cross-platform computer vision library that runs on Linux, Windows, and Mac OS. It is lightweight and efficient-it consists of a series of C functions and a small number of C ++ classes, and provides interfaces for Python, Ruby, MATLAB, and other languages, it implements many common algorithms for image processing and computer vision.
Back to the topic, tclip only uses a small part of opencv, that is, recognition of the significance area of the image.
The specific cropping algorithm does not do much research.
The problem is that you want to read the binary data of the image from the memory using PHP (this part of the data is prepared first) and pass it to opencv. You can view the API documentation of opencv, you can see the imdecode method to load images from the memory. This problem can be solved.
About installing tclip extension: https://github.com/exinnet/tclip
But it should be noted that the opencv-2.4.9 should have bugs, compilation is not successful, and the ubuntu-14.0.4 after the compilation and installation call tclip function error, I tested the version is:

CentOS-6.4
PHP-5.4.31
Opencv-2.4.4

Example

Installation steps:

The code is as follows: Copy code
Download source code:
Download opencv Source: http://www.bo56.com/download/opencv2.tar.bz2
Download Tclip source code:
Http://code.taobao.org/p/tclip/src/
Https://github.com/exinnet/tclip
Install opencv2:
This extension depends on opencv2.0. Therefore, install opencv before installation. Follow these steps to install opencv:
If you have any questions during installation, please contact qq 179815944.
1. Install the following dependency package: gtk +-devel pkgconfig libpng zlib libjpeg libtiff cmake
For centos, run the following command to install the dependency package:
Yum install gtk +-devel pkgconfig libpng zlib libjpeg libtiff cmake
2. Install opencv2 as follows:
Decompress the installation package
Cd to enter the installation package folder.
Cmake CMakeLists.txt
Make & make install
Vim/etc/profile
Note: When compiling opencv, you must use the gcc version 4.4 or later. Otherwise, an error is reported.
3. Set related environment variables
Add before unset I
Export PKG_CONFIG_PATH =/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig: $ PKG_CONFIG_PATH
After you exit, run the following command:
Source/etc/profile
Echo "/usr/local/lib/">/etc/ld. so. conf. d/opencv. conf
Ldconfig
Install tclip extension for php image cropping
Cd to the php_ext folder in the source code directory
Phpize
./Configure
Make
Cp modules/tclip. so to the extension directory
Modify php. ini. Add extension = tclip. so
Restart fpm
Install command line
If you want to use the command line method, you can install
Cd to enter the soft folder of the installation package
Chmod + x./tclip. sh
./Tclip. sh
Usage instructions
First: Image cropping format in php:
Tclip (original file path, save path of the cropped image, width of the cropped image, and height of the cropped image)
Example:
$ Source_file = "/tmp/a.jpg ";
$ Dest_file = "/www/a_dest.jpg ";
$ Width = 400;
$ Height = 200;
$ Ret = tclip ($ source_file, $ dest_file, $ width, $ height );
Execution successful $ ret is true; otherwise, it is false.
Type 2: command line
Parameter description:
-S source image path
-D save path of the cropped image
-W: width of the cropped image
-H: specifies the image height after cropping.
./Tclip-s a.jpg-d a_dest.jpg-w 400-h 200

Finally, although PHP can directly transmit the binary data of the image, it is found that the binary data in the opencv source code is saved to a temporary file and then read. And temporary files won't be deleted in opencv-2.4.2 versions, which is a bit pitfall. However, PHP logic can still be reduced, so it is necessary to support binary image data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.