Skip to content

Commit

Permalink
bump to 0.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanilla Hsu committed Dec 9, 2011
1 parent 3946c20 commit c41d37f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 33 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Version 0.4 2011/12/09
- Coding style fix.
- Add unittest.
- Change qr_save api to make it flexible.
22 changes: 12 additions & 10 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ php qrencode is the wrapper of libqrencode to generate qr code.
only 2 function to generate qr code.

<code>
$qr = qr_encode ('test for qrcode');
if (is_resource ($qr))
qr_save ($qr, '1.png');
$qr = qr_encode('test for qrcode');
if (is_resource($qr))
qr_save($qr, '1.png');
</code>

or you can output direct to stdout.

<code>
$qr = qr_encode ('test for qrcode');
if (is_resource ($qr))
{
header ("Content-type: image/PNG");
qr_save ($qr);
$qr = qr_encode('test for qrcode');
if (is_resource($qr)) {
header("Content-type: image/PNG");
qr_save($qr);
}
</code>

resource = qr_encode (string $text, [ int $version, int $mode, int $casesensitive]);
resource = qr_encode(string $text, [int $version, int $mode, int $casesensitive]);

bool = qr_save (resource $qr, [ string $filename] );
bool = qr_save(resource $qr, string $filename);
bool = qr_save(resource $qr, string $filename, int size, int margin);
bool = qr_save(resource $qr, int size, int margin, string $filename);
bool = qr_save(resource $qr, int size, int margin);

110 changes: 87 additions & 23 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,23 @@ PHP_FUNCTION(qr_encode) {
* @return
*/
PHP_FUNCTION(qr_save) {
zval *link = NULL;
zval *link = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL;
long size = 3, margin = 4;
const char *fn = NULL;
int fn_len, argc;
int argc;
FILE *fp = NULL;
png_structp png_ptr;
png_infop info_ptr;
unsigned char *row, *p, *q;
int x, y, xx, yy, bit;
int realwidth;
int realwidth, temp_file = 0;
char *path;
int b;
char buf[4096];

argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|sll",
&link, &fn, &fn_len, &size, &margin) == FAILURE)
if (zend_parse_parameters(argc TSRMLS_CC, "r|zzz",
&link, &arg2, &arg3, &arg4) == FAILURE)
RETURN_FALSE;

if (link) {
Expand All @@ -169,20 +168,85 @@ PHP_FUNCTION(qr_save) {
ZEND_FETCH_RESOURCE2(qr, php_qrcode *, &link, -1,
"qr handle", le_qr, NULL);

if ((argc == 2) || (argc > 2 && fn != NULL)) {
fp = VCWD_FOPEN(fn, "wb");
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open '%s' for writing.", fn);
RETURN_FALSE;
}
} else {
fp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open temporary file for writing.");
switch (argc) {
case 4:
if (Z_TYPE_P(arg2) == IS_STRING) {
// qr_save(resource $link, string $fn, int $size, int $margin);
fp = VCWD_FOPEN(Z_STRVAL_P(arg2), "wb");
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open '%s' for writing.",
Z_STRVAL_P(arg2));
RETURN_FALSE;
}
size = Z_LVAL_P(arg3);
margin = Z_LVAL_P(arg4);
} else if (Z_TYPE_P(arg2) == IS_LONG) {
// qr_save(resource $link, int $size, int $margin, string $fn);
size = Z_LVAL_P(arg2);
margin = Z_LVAL_P(arg3);

fp = VCWD_FOPEN(Z_STRVAL_P(arg4), "wb");
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open '%s' for writing.",
Z_STRVAL_P(arg4));
RETURN_FALSE;
}
}
break;

case 3:
// qr_save(resource $link, int $size, int $margin);
if (Z_TYPE_P(arg2) != IS_LONG) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"should use type long as argument 2 when pass 3 arguments");
RETURN_FALSE;
}

fp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open temporary file for writing.");
RETURN_FALSE;
}

size = Z_LVAL_P(arg2);
margin = Z_LVAL_P(arg3);
temp_file = 1;

break;
case 2:
if (Z_TYPE_P(arg2) == IS_STRING) {
fp = VCWD_FOPEN(Z_STRVAL_P(arg2), "wb");
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open '%s' for writing.",
Z_STRVAL_P(arg2));
RETURN_FALSE;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"should use type string as argument 2");
RETURN_FALSE;
}
break;

case 1:
fp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);

if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to open temporary file for writing.");
RETURN_FALSE;
}
temp_file = 1;

break;

case 0:
default:
RETURN_FALSE;
}
}

realwidth = (qr->c->width + margin * 2) * size;
Expand Down Expand Up @@ -257,10 +321,7 @@ PHP_FUNCTION(qr_save) {

efree(row);

if ((argc == 2) || (argc > 2 && fn != NULL)) {
fflush(fp);
fclose(fp);
} else {
if (temp_file) {
fseek(fp, 0, SEEK_SET);
#if APACHE && defined(CHARSET_EBCDIC)
ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
Expand All @@ -271,6 +332,9 @@ PHP_FUNCTION(qr_save) {
fclose(fp);
VCWD_UNLINK((const char *)path);
efree(path);
} else {
fflush(fp);
fclose(fp);
}

RETURN_TRUE;
Expand Down
15 changes: 15 additions & 0 deletions tests/qr_encode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
qrencode: qr_encode test
--SKIPIF--
<?php if (!extension_loaded("qrencode")) print "skip"; ?>
--FILE--
<?php
$q = qr_encode("test");
var_dump($q);
?>
--CLEAN--
<?php
unset($q);
?>
--EXPECTF--
resource(%d) of type (qr)
56 changes: 56 additions & 0 deletions tests/qr_save.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
qrencode: qr_encode test
--SKIPIF--
<?php if (!extension_loaded("qrencode")) print "skip"; ?>
--FILE--
<?php
$q = qr_encode('test');

qr_save($q, 2);

qr_save($q, '/tmp/test.png', 3);

ob_start();
qr_save($q);
$content = ob_get_contents();
ob_end_clean();
echo base64_encode($content) . "\n";

ob_start();
qr_save($q, 4, 5);
$content = ob_get_contents();
ob_end_clean();
echo base64_encode($content) . "\n";

qr_save($q, '/tmp/test.png');
$size = filesize('/tmp/test.png');
if ($size == 237) {
echo "PASS\n";
}

qr_save($q, '/tmp/test2.png', 4, 5);
$size = filesize('/tmp/test2.png');
if ($size == 237) {
echo "PASS\n";
}

qr_save($q, 4, 5, '/tmp/test3.png');
$size = filesize('/tmp/test3.png');
if ($size == 237) {
echo "PASS\n";
}
?>
--CLEAN--
<?php
unlink('/tmp/test.png');
unlink('/tmp/test2.png');
unlink('/tmp/test3.png');
unset($q);
?>
--EXPECTF--
Warning: qr_save(): should use type string as argument 2 in %s on line %d

Warning: qr_save(): should use type long as argument 2 when pass 3 arguments in %s on line %d
iVBORw0KGgoAAAANSUhEUgAAAFcAAABXAQAAAABZs+TBAAAAtElEQVQ4jc3TsQ3CMBAF0EMU6WABS7fGdVnJWSCGCVjJndeIxAJOl+KU40JBKKyfEq56xS++v2Sy/egvXIlSCUQMPJsmC8mgcxiKxu7Anj/0KEc2HUk/HZr2/sPXW5r2m8u+SdO1o1PxKZBXYcvPx8LAVjQt04WQ53K+yfm+GPAqOnheGNivdjr2yO8duAqy7xl7r2fIOcQ+RGHstO1g2FGma2Fk0yH7FAbs/bd8ZuBf/bW2XweNtloIoiLrAAAAAElFTkSuQmCC
iVBORw0KGgoAAAANSUhEUgAAAHwAAAB8AQAAAACDZekTAAAAmklEQVRIie3UMQ7FIAwDUN/A97+lb8CPw1/KhtOxCAnxhog4arGOhQ9eAAFYUh8x7L2PFIQq2ccIVr9sCMQMvMGj20uodHWmfglejvgx/UuoplwV/a4QKpxSkgs5yOUIIofKxrcqzRgWKU+bRA5+m+OhYvgPuzJCDu6PHVIO/gjZMY1gD5sjqDso5bD/BJ5UDg65iyKG5/pgDD+c+VFPQmXXuAAAAABJRU5ErkJggg==
PASS

0 comments on commit c41d37f

Please sign in to comment.