forked from garrettrayj/php7-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cc
137 lines (111 loc) · 3.9 KB
/
image.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "php.h"
#include "exception.h"
#include "image.h"
#include <mapnik/image.hpp>
#include <mapnik/image_util.hpp>
zend_class_entry *php_mapnik_image_ce;
zend_object_handlers php_mapnik_image_object_handlers;
// PHP object handling
void php_mapnik_image_free_storage(zend_object *object TSRMLS_DC)
{
php_mapnik_image_object *obj;
obj = php_mapnik_image_fetch_object(object);
zend_object_std_dtor(object TSRMLS_DC);
}
zend_object * php_mapnik_image_new(zend_class_entry *ce TSRMLS_DC) {
// Allocate sizeof(custom) + sizeof(properties table requirements)
php_mapnik_image_object *intern;
intern = (php_mapnik_image_object*) ecalloc(1, sizeof(php_mapnik_image_object) + zend_object_properties_size(ce));
zend_object_std_init(&intern->std, ce TSRMLS_CC);
object_properties_init(&intern->std, ce);
intern->std.handlers = &php_mapnik_image_object_handlers;
return &intern->std;
}
// Class Methods
PHP_METHOD(Image, __construct)
{
php_mapnik_image_object *obj;
mapnik::image_rgba8 *image;
long width, height;
if (::zend_parse_parameters_ex(
ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC,
"ll",
&width,
&height) == SUCCESS
) {
image = new mapnik::image_rgba8(width, height);
} else {
php_mapnik_throw_exception("Wrong arguments passed to \\Mapnik\\Image::__construct");
RETURN_FALSE;
}
obj = Z_PHP_MAPNIK_IMAGE_P(getThis());
obj->image = image;
}
PHP_METHOD(Image, saveToFile)
{
php_mapnik_image_object *obj;
zend_string *file;
if (::zend_parse_parameters_ex(
ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC,
"S",
&file) == FAILURE
) {
php_mapnik_throw_exception("Wrong arguments passed to \\Mapnik\\Image::saveToFile");
RETURN_FALSE;
}
obj = Z_PHP_MAPNIK_IMAGE_P(getThis());
try {
std::string file_str(file->val, file->len);
mapnik::save_to_file(*obj->image, file_str, "png");
} catch (const mapnik::image_writer_exception & ex) {
php_mapnik_throw_exception(ex.what());
RETURN_FALSE;
} catch (const std::exception & ex) {
php_mapnik_throw_exception(ex.what());
RETURN_FALSE;
} catch (...) {
php_mapnik_throw_exception("Unknown exception thrown while saving image to file.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_METHOD(Image, saveToString)
{
php_mapnik_image_object *obj;
std::string image_str;
obj = Z_PHP_MAPNIK_IMAGE_P(getThis());
try {
image_str = mapnik::save_to_string(*obj->image, "png");
} catch (const mapnik::image_writer_exception & ex) {
php_mapnik_throw_exception(ex.what());
RETURN_FALSE;
} catch (const std::exception & ex) {
php_mapnik_throw_exception(ex.what());
RETURN_FALSE;
} catch (...) {
php_mapnik_throw_exception("Unknown exception thrown while saving image to string.");
RETURN_FALSE;
}
RETURN_STRINGL(image_str.c_str(), image_str.size());
}
// Register methods
zend_function_entry php_mapnik_image_methods[] = {
PHP_ME(Image, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Image, saveToFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Image, saveToString, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
// Extension class startup
void php_mapnik_image_startup(INIT_FUNC_ARGS)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "Mapnik", "Image", php_mapnik_image_methods);
php_mapnik_image_ce = zend_register_internal_class(&ce TSRMLS_CC);
php_mapnik_image_ce->create_object = php_mapnik_image_new;
memcpy(&php_mapnik_image_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_mapnik_image_object_handlers.offset = XtOffsetOf(struct php_mapnik_image_object, std);
php_mapnik_image_object_handlers.free_obj = &php_mapnik_image_free_storage;
php_mapnik_image_object_handlers.clone_obj = NULL;
}