-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmosaic.c
381 lines (324 loc) · 11.2 KB
/
mosaic.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
+----------------------------------------------------------------------+
| Mosaic |
+----------------------------------------------------------------------+
| Copyright (c) 2012 Chris Yue |
+----------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met (BSD, revised). |
+----------------------------------------------------------------------+
| Author: Chris Yue |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mosaic.h"
#include "libqrencode/qrencode.h"
#include "gd/php_gd.h"
#include "gd/libgd/gd.h"
/* If you declare any globals in php_mosaic.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(mosaic)
*/
/* True global resources - no need for thread safety here */
static int le_mosaic;
static zend_class_entry* qr_ce;
/* {{{ mosaic_functions[]
*
* Every user visible function must have an entry in mosaic_functions[].
*/
static const zend_function_entry mosaic_functions[] = {
PHP_FE(qr_encode, NULL)
PHP_FE_END /* Must be the last line in mosaic_functions[] */
};
/* }}} */
static const zend_function_entry mosaic_methods[] = {
PHP_ME(qr, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, encode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, setText, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, getText, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, setModuleSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, getModuleSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, setMargin, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, getMargin, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, setVersion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, getVersion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, setErrorCorrectionLevel, NULL, ZEND_ACC_PUBLIC)
PHP_ME(qr, getErrorCorrectionLevel, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
#if ZEND_MODULE_API_NO >= 220050617
const zend_module_dep mosaic_deps[] = { /* {{{ */
zend_module_required("gd")
{ NULL, NULL, NULL }
};
/* }}} */
#endif
/* {{{ mosaic_module_entry
*/
zend_module_entry mosaic_module_entry = {
#if ZEND_MODULE_API_NO >= 220050617
STANDARD_MODULE_HEADER_EX, NULL,
mosaic_deps,
#elif ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"mosaic",
mosaic_functions,
PHP_MINIT(mosaic),
PHP_MSHUTDOWN(mosaic),
PHP_RINIT(mosaic), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(mosaic), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(mosaic),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MOSAIC
ZEND_GET_MODULE(mosaic)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("mosaic.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_mosaic_globals, mosaic_globals)
STD_PHP_INI_ENTRY("mosaic.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_mosaic_globals, mosaic_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_mosaic_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_mosaic_init_globals(zend_mosaic_globals *mosaic_globals)
{
mosaic_globals->global_value = 0;
mosaic_globals->global_string = NULL;
}
*/
/* }}} */
#define prop_text "text"
static const int prop_text_len = sizeof(prop_text) - 1;
#define prop_msize "moduleSize"
static const int prop_msize_len = sizeof(prop_msize) - 1;
#define prop_margin "margin"
static const int prop_margin_len = sizeof(prop_margin) - 1;
#define prop_version "version"
static const int prop_version_len = sizeof(prop_version) - 1;
#define prop_eclevel "errorCorrectionLevel"
static const int prop_eclevel_len = sizeof(prop_eclevel) - 1;
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(mosaic)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
REGISTER_LONG_CONSTANT("QR_ECLEVEL_L", 0, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QR_ECLEVEL_M", 1, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QR_ECLEVEL_Q", 2, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QR_ECLEVEL_H", 3, CONST_CS|CONST_PERSISTENT);
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Qr", mosaic_methods);
qr_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_null(qr_ce, prop_text, prop_text_len, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(qr_ce, prop_msize, prop_msize_len, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(qr_ce, prop_margin, prop_margin_len, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(qr_ce, prop_version, prop_version_len, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(qr_ce, prop_eclevel, prop_eclevel_len, ZEND_ACC_PROTECTED TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(mosaic)
{
/* 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(mosaic)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mosaic)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mosaic)
{
php_info_print_table_start();
php_info_print_table_header(2, "mosaic support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
gdImagePtr qr_encode(char* text, int s, int margin, int v, int l)
{
QRecLevel level;
switch (l) {
case 1:
level = QR_ECLEVEL_M;
break;
case 2:
level = QR_ECLEVEL_Q;
break;
case 3:
level = QR_ECLEVEL_H;
break;
default:
level = QR_ECLEVEL_L;
}
QRcode* qrcode = QRcode_encodeString8bit(text, v, level);
int size = qrcode->width * s + 2 * margin;
gdImagePtr im = gdImageCreate(size, size);
int black = gdImageColorAllocate(im, 0, 0, 0);
int white = gdImageColorAllocate(im, 255, 255, 255);
gdImageFill(im, 0, 0, white);
int x, y, posx, posy;
for (y = 0; y < qrcode->width; y++) {
for (x = 0; x < qrcode->width; x++) {
if (*(qrcode->data + y * qrcode->width + x) & 1) {
posx = x * s + margin;
posy = y * s + margin;
gdImageFilledRectangle(im, posx, posy, posx + s, posy + s, black);
}
}
}
return im;
}
PHP_FUNCTION(qr_encode) /* {{{ */
{
char* text;
int text_len;
int s = 5; /* module size in pixel */
int margin = 10; /* width (pixel) */
int v = 3; /* version */
int l = 2; /* error correction level */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|llll", &text, &text_len, &s, &margin, &v, &l) == FAILURE) {
return ;
}
gdImagePtr im = qr_encode(text, s, margin, v, l);
ZEND_REGISTER_RESOURCE(return_value, im, zend_fetch_list_dtor_id("gd"));
}
/* }}} */
PHP_METHOD(qr, __construct)
{
char* text;
int text_len;
int s = 5; /* module size in pixel */
int margin = 10; /* width (pixel) */
int v = 3; /* version */
int l = 2; /* error correction level */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|llll", &text, &text_len, &s, &margin, &v, &l) == FAILURE) {
return ;
}
zend_update_property_stringl(qr_ce, getThis(), prop_text, prop_text_len, text, text_len TSRMLS_CC);
zend_update_property_long(qr_ce, getThis(), prop_msize, prop_msize_len, s TSRMLS_CC);
zend_update_property_long(qr_ce, getThis(), prop_margin, prop_margin_len, margin TSRMLS_CC);
zend_update_property_long(qr_ce, getThis(), prop_version, prop_version_len, v TSRMLS_CC);
zend_update_property_long(qr_ce, getThis(), prop_eclevel, prop_eclevel_len, l TSRMLS_CC);
}
PHP_METHOD(qr, setText)
{
char* text;
int text_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_len) == FAILURE) {
return ;
}
zend_update_property_stringl(qr_ce, getThis(), prop_text, prop_text_len, text, text_len TSRMLS_CC);
RETURN_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(qr, getText)
{
RETURN_STRING(Z_STRVAL_P(zend_read_property(qr_ce, getThis(), prop_text, prop_text_len, 0 TSRMLS_CC)), 1);
}
PHP_METHOD(qr, setModuleSize)
{
int size;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &size) == FAILURE) {
return ;
}
zend_update_property_long(qr_ce, getThis(), prop_msize, prop_msize_len, size TSRMLS_CC);
RETURN_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(qr, getModuleSize)
{
RETURN_LONG(Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_msize, prop_msize_len, 0 TSRMLS_CC)));
}
PHP_METHOD(qr, setMargin)
{
int margin;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &margin) == FAILURE) {
return ;
}
zend_update_property_long(qr_ce, getThis(), prop_margin, prop_margin_len, margin TSRMLS_CC);
RETURN_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(qr, getMargin)
{
RETURN_LONG(Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_margin, prop_margin_len, 0 TSRMLS_CC)));
}
PHP_METHOD(qr, setVersion)
{
int version;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &version) == FAILURE) {
return ;
}
zend_update_property_long(qr_ce, getThis(), prop_version, prop_version_len, version TSRMLS_CC);
RETURN_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(qr, getVersion)
{
RETURN_LONG(Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_version, prop_version_len, 0 TSRMLS_CC)));
}
PHP_METHOD(qr, setErrorCorrectionLevel)
{
int eclevel;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eclevel) == FAILURE) {
return ;
}
zend_update_property_long(qr_ce, getThis(), prop_eclevel, prop_eclevel_len, eclevel TSRMLS_CC);
RETURN_ZVAL(getThis(), 1, 0);
}
PHP_METHOD(qr, getErrorCorrectionLevel)
{
RETURN_LONG(Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_eclevel, prop_eclevel_len, 0 TSRMLS_CC)));
}
PHP_METHOD(qr, encode)
{
gdImagePtr im = qr_encode(Z_STRVAL_P(zend_read_property(qr_ce, getThis(), prop_text, prop_text_len, 0 TSRMLS_CC)),
Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_msize, prop_msize_len, 0 TSRMLS_CC)),
Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_margin, prop_margin_len, 0 TSRMLS_CC)),
Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_version, prop_version_len, 0 TSRMLS_CC)),
Z_LVAL_P(zend_read_property(qr_ce, getThis(), prop_eclevel, prop_eclevel_len, 0 TSRMLS_CC)));
ZEND_REGISTER_RESOURCE(return_value, im, zend_fetch_list_dtor_id("gd"));
}
/*
* 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
*/