From 5539b5bff81ed3b27ba096754a8226c2ad4d517e Mon Sep 17 00:00:00 2001 From: goodspb Date: Mon, 21 May 2018 01:23:19 +0800 Subject: [PATCH] first commit --- .gitignore | 38 +++++++++ CREDITS | 1 + EXPERIMENTAL | 0 config.m4 | 45 ++++++++++ config.w32 | 13 +++ pdlib.cc | 188 ++++++++++++++++++++++++++++++++++++++++++ pdlib.php | 21 +++++ php_pdlib.h | 74 +++++++++++++++++ src/face_detection.cc | 36 ++++++++ src/face_detection.h | 13 +++ tests/001.phpt | 21 +++++ 11 files changed, 450 insertions(+) create mode 100644 .gitignore create mode 100644 CREDITS create mode 100644 EXPERIMENTAL create mode 100644 config.m4 create mode 100644 config.w32 create mode 100644 pdlib.cc create mode 100644 pdlib.php create mode 100644 php_pdlib.h create mode 100644 src/face_detection.cc create mode 100644 src/face_detection.h create mode 100644 tests/001.phpt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1a9241 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +.deps +*.lo +*.la +.libs +acinclude.m4 +aclocal.m4 +autom4te.cache +build +config.guess +config.h +config.h.in +config.log +config.nice +config.status +config.sub +configure +configure.ac +include +install-sh +libtool +ltmain.sh +Makefile +Makefile.fragments +Makefile.global +Makefile.objects +missing +mkinstalldirs +modules +run-tests.php +tests/*/*.diff +tests/*/*.out +tests/*/*.php +tests/*/*.exp +tests/*/*.log +tests/*/*.sh + +.idea +cmake-build-debug \ No newline at end of file diff --git a/CREDITS b/CREDITS new file mode 100644 index 0000000..aa92ab8 --- /dev/null +++ b/CREDITS @@ -0,0 +1 @@ +pdlib diff --git a/EXPERIMENTAL b/EXPERIMENTAL new file mode 100644 index 0000000..e69de29 diff --git a/config.m4 b/config.m4 new file mode 100644 index 0000000..130541f --- /dev/null +++ b/config.m4 @@ -0,0 +1,45 @@ +dnl $Id$ +dnl config.m4 for extension pdlib + +dnl Comments in this file start with the string 'dnl'. +dnl Remove where necessary. This file will not work +dnl without editing. + +dnl If your extension references something external, use with: + +PHP_ARG_WITH(pdlib, for pdlib support, +dnl Make sure that the comment is aligned: +[ --with-pdlib Include pdlib support]) + +if test "$PHP_PDLIB" != "no"; then + dnl using C++11 + CXXFLAGS="-std=c++11" + PHP_REQUIRE_CXX() + PHP_ADD_LIBRARY(stdc++, 1, DLIB_SHARED_LIBADD) + PHP_SUBST(DLIB_SHARED_LIBADD) + + pdlib_src_files="pdlib.cc \ + src/face_detection.cc" + + AC_MSG_CHECKING(for pkg-config) + if test ! -f "$PKG_CONFIG"; then + PKG_CONFIG=`which pkg-config` + fi + + if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists dlib-1; then + if $PKG_CONFIG dlib-1 --atleast-version 19.00; then + LIBDLIB_CFLAGS=`$PKG_CONFIG dlib-1 --cflags` + LIBDLIB_LIBDIR=`$PKG_CONFIG dlib-1 --libs` + LIBDLIB_VERSON=`$PKG_CONFIG dlib-1 --modversion` + AC_MSG_RESULT(from pkgconfig: dlib version $LIBDLIB_VERSON) + else + AC_MSG_ERROR(system dlib is too old: version 19.00 required) + fi + else + AC_MSG_ERROR(pkg-config not found) + fi + PHP_EVAL_LIBLINE($LIBDLIB_LIBDIR, DLIB_SHARED_LIBADD) + PHP_EVAL_INCLINE($LIBDLIB_CFLAGS) + + PHP_NEW_EXTENSION(pdlib, $pdlib_src_files, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) +fi \ No newline at end of file diff --git a/config.w32 b/config.w32 new file mode 100644 index 0000000..2491362 --- /dev/null +++ b/config.w32 @@ -0,0 +1,13 @@ +// $Id$ +// vim:ft=javascript + +// If your extension references something external, use ARG_WITH +// ARG_WITH("pdlib", "for pdlib support", "no"); + +// Otherwise, use ARG_ENABLE +// ARG_ENABLE("pdlib", "enable pdlib support", "no"); + +if (PHP_PDLIB != "no") { + EXTENSION("pdlib", "pdlib.c", PHP_EXTNAME_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); +} + diff --git a/pdlib.cc b/pdlib.cc new file mode 100644 index 0000000..6166542 --- /dev/null +++ b/pdlib.cc @@ -0,0 +1,188 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2018 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +extern "C" { + #ifdef HAVE_CONFIG_H + #include "config.h" + #endif + + #include "php.h" + #include "php_ini.h" + #include "ext/standard/info.h" +} +#include "php_pdlib.h" +#include "src/face_detection.h" + +/* If you declare any globals in php_pdlib.h uncomment this: +ZEND_DECLARE_MODULE_GLOBALS(pdlib) +*/ + +/* True global resources - no need for thread safety here */ +static int le_pdlib; + +/* {{{ PHP_INI + */ +/* Remove comments and fill if you need to have entries in php.ini +PHP_INI_BEGIN() + STD_PHP_INI_ENTRY("pdlib.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_pdlib_globals, pdlib_globals) + STD_PHP_INI_ENTRY("pdlib.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_pdlib_globals, pdlib_globals) +PHP_INI_END() +*/ +/* }}} */ + +/* Remove the following function when you have successfully modified config.m4 + so that your module can be compiled into PHP, it exists only for testing + purposes. */ + +/* Every user-visible function in PHP should document itself in the source */ +/* {{{ proto string confirm_pdlib_compiled(string arg) + Return a string to confirm that the module is compiled in */ +PHP_FUNCTION(confirm_pdlib_compiled) +{ + char *arg = NULL; + size_t arg_len, len; + zend_string *strg; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { + return; + } + + strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "pdlib", arg); + + RETURN_STR(strg); +} +/* }}} */ +/* The previous line is meant for vim and emacs, so it can correctly fold and + unfold functions in source code. See the corresponding marks just before + function definition, where the functions purpose is also documented. Please + follow this convention for the convenience of others editing your code. +*/ + + +/* {{{ php_pdlib_init_globals + */ +/* Uncomment this function if you have INI entries +static void php_pdlib_init_globals(zend_pdlib_globals *pdlib_globals) +{ + pdlib_globals->global_value = 0; + pdlib_globals->global_string = NULL; +} +*/ +/* }}} */ + +/* {{{ PHP_MINIT_FUNCTION + */ +PHP_MINIT_FUNCTION(pdlib) +{ + /* If you have INI entries, uncomment these lines + REGISTER_INI_ENTRIES(); + */ + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION + */ +PHP_MSHUTDOWN_FUNCTION(pdlib) +{ + /* uncomment this line if you have INI entries + UNREGISTER_INI_ENTRIES(); + */ + return SUCCESS; +} +/* }}} */ + +/* Remove if there's nothing to do at request start */ +/* {{{ PHP_RINIT_FUNCTION + */ +PHP_RINIT_FUNCTION(pdlib) +{ +#if defined(COMPILE_DL_PDLIB) && defined(ZTS) + ZEND_TSRMLS_CACHE_UPDATE(); +#endif + return SUCCESS; +} +/* }}} */ + +/* Remove if there's nothing to do at request end */ +/* {{{ PHP_RSHUTDOWN_FUNCTION + */ +PHP_RSHUTDOWN_FUNCTION(pdlib) +{ + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(pdlib) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "pdlib support", "enabled"); + php_info_print_table_end(); + + /* Remove comments if you have entries in php.ini + DISPLAY_INI_ENTRIES(); + */ +} +/* }}} */ + +/* {{{ pdlib_functions[] + * + * Every user visible function must have an entry in pdlib_functions[]. + */ +const zend_function_entry pdlib_functions[] = { + PHP_FE(confirm_pdlib_compiled, NULL) + PHP_FE(dlib_face_detection, dlib_face_detection_arginfo) + PHP_FE_END /* Must be the last line in pdlib_functions[] */ +}; +/* }}} */ + +/* {{{ pdlib_module_entry + */ +zend_module_entry pdlib_module_entry = { + STANDARD_MODULE_HEADER, + "pdlib", + pdlib_functions, + PHP_MINIT(pdlib), + PHP_MSHUTDOWN(pdlib), + PHP_RINIT(pdlib), /* Replace with NULL if there's nothing to do at request start */ + PHP_RSHUTDOWN(pdlib), /* Replace with NULL if there's nothing to do at request end */ + PHP_MINFO(pdlib), + PHP_PDLIB_VERSION, + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_PDLIB +#ifdef ZTS +ZEND_TSRMLS_CACHE_DEFINE() +#endif +ZEND_GET_MODULE(pdlib) +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/pdlib.php b/pdlib.php new file mode 100644 index 0000000..366028b --- /dev/null +++ b/pdlib.php @@ -0,0 +1,21 @@ +"; + +if(!extension_loaded('pdlib')) { + dl('pdlib.' . PHP_SHLIB_SUFFIX); +} +$module = 'pdlib'; +$functions = get_extension_funcs($module); +echo "Functions available in the test extension:$br\n"; +foreach($functions as $func) { + echo $func."$br\n"; +} +echo "$br\n"; +$function = 'confirm_' . $module . '_compiled'; +if (extension_loaded($module)) { + $str = $function($module); +} else { + $str = "Module $module is not compiled into PHP"; +} +echo "$str\n"; +?> diff --git a/php_pdlib.h b/php_pdlib.h new file mode 100644 index 0000000..27f5223 --- /dev/null +++ b/php_pdlib.h @@ -0,0 +1,74 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2018 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_PDLIB_H +#define PHP_PDLIB_H + +extern "C" { + #include "php.h" + #ifdef ZTS + #include "TSRM.h" + #endif +}; + +extern zend_module_entry pdlib_module_entry; +#define phpext_pdlib_ptr &pdlib_module_entry + +#define PHP_PDLIB_VERSION "0.1.0" /* Replace with version number for your extension */ + +#ifdef PHP_WIN32 +# define PHP_PDLIB_API __declspec(dllexport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +# define PHP_PDLIB_API __attribute__ ((visibility("default"))) +#else +# define PHP_PDLIB_API +#endif + +/* + Declare any global variables you may need between the BEGIN + and END macros here: + +ZEND_BEGIN_MODULE_GLOBALS(pdlib) + zend_long global_value; + char *global_string; +ZEND_END_MODULE_GLOBALS(pdlib) +*/ + +/* Always refer to the globals in your function as PDLIB_G(variable). + You are encouraged to rename these macros something shorter, see + examples in any other php module directory. +*/ +#define PDLIB_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(pdlib, v) + +#if defined(ZTS) && defined(COMPILE_DL_PDLIB) +ZEND_TSRMLS_CACHE_EXTERN() +#endif + +#endif /* PHP_PDLIB_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/src/face_detection.cc b/src/face_detection.cc new file mode 100644 index 0000000..e172ecb --- /dev/null +++ b/src/face_detection.cc @@ -0,0 +1,36 @@ + +#include "../php_pdlib.h" +#include "face_detection.h" + +#include +#include +#include +#include + +using namespace dlib; +using namespace std; + +PHP_FUNCTION(dlib_face_detection) +{ + char *img_path; + size_t img_path_len; + + if(zend_parse_parameters(ZEND_NUM_ARGS(), "s", &img_path, &img_path_len) == FAILURE){ + RETURN_FALSE; + } + try { + frontal_face_detector detector = get_frontal_face_detector(); + + array2d img; + load_image(img, img_path); + + pyramid_up(img); + std::vector dets = detector(img); + RETURN_LONG(dets.size()); + } + catch (exception& e) + { + RETURN_FALSE; + } +} + diff --git a/src/face_detection.h b/src/face_detection.h new file mode 100644 index 0000000..aaeb2fd --- /dev/null +++ b/src/face_detection.h @@ -0,0 +1,13 @@ +// +// Created by goodspb on 2018/5/20. +// + +#ifndef PHP_DLIB_FACE_DETECTION_H +#define PHP_DLIB_FACE_DETECTION_H + +ZEND_BEGIN_ARG_INFO_EX(dlib_face_detection_arginfo, 0, 0, 1) + ZEND_ARG_INFO(0, img_path) +ZEND_END_ARG_INFO() +PHP_FUNCTION(dlib_face_detection); + +#endif //PHP_DLIB_FACE_DETECTION_H diff --git a/tests/001.phpt b/tests/001.phpt new file mode 100644 index 0000000..4a22d6f --- /dev/null +++ b/tests/001.phpt @@ -0,0 +1,21 @@ +--TEST-- +Check for pdlib presence +--SKIPIF-- + +--FILE-- + +--EXPECT-- +pdlib extension is available