Skip to content

Commit 4475da7

Browse files
committed
Merge branch 'memory' of https://github.com/kleisauke/php-vips-ext into kleisauke-memory
2 parents 9ac2a7a + c347899 commit 4475da7

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

tests/037.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ can make an image from memory
44
<?php if (!extension_loaded("vips")) print "skip"; ?>
55
--FILE--
66
<?php
7-
$byte_array = array_fill(0, 200, 0);
8-
$image = vips_image_new_from_memory($byte_array, 20, 10, 1, 'uchar')["out"];
7+
$binary_str = pack("C*", ...array_fill(0, 200, 0));
8+
$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"];
99
$width = vips_image_get($image, "width")["out"];
1010
$height = vips_image_get($image, "height")["out"];
1111
$format = vips_image_get($image, "format")["out"];

tests/038.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ can write to memory
44
<?php if (!extension_loaded("vips")) print "skip"; ?>
55
--FILE--
66
<?php
7-
$byte_array = array_fill(0, 200, 0);
8-
$image = vips_image_new_from_memory($byte_array, 20, 10, 1, 'uchar')["out"];
9-
$mem_arr = vips_image_write_to_memory($image);
7+
$binary_str = pack("C*", ...array_fill(0, 200, 0));
8+
$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"];
9+
$mem_str = vips_image_write_to_memory($image);
1010

11-
if ($byte_array === $mem_arr) {
11+
if ($binary_str === $mem_str) {
1212
echo "pass";
1313
}
1414
?>

vips.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,11 +1388,12 @@ PHP_FUNCTION(vips_image_copy_memory)
13881388
}
13891389
/* }}} */
13901390

1391-
/* {{{ proto resource vips_image_new_from_memory(array data, integer width, integer height, integer bands, string format)
1391+
/* {{{ proto resource vips_image_new_from_memory(string data, integer width, integer height, integer bands, string format)
13921392
Wrap an image around a memory array. */
13931393
PHP_FUNCTION(vips_image_new_from_memory)
13941394
{
1395-
HashTable *ht;
1395+
char *bstr;
1396+
size_t bstr_len;
13961397
long width;
13971398
long height;
13981399
long bands;
@@ -1406,29 +1407,17 @@ PHP_FUNCTION(vips_image_new_from_memory)
14061407

14071408
VIPS_DEBUG_MSG("vips_image_new_from_memory:\n");
14081409

1409-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "hlllp",
1410-
&ht, &width, &height, &bands, &format, &format_len) == FAILURE) {
1410+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slllp",
1411+
&bstr, &bstr_len, &width, &height, &bands, &format, &format_len) == FAILURE) {
14111412
RETURN_LONG(-1);
14121413
}
14131414

1414-
if ((format_value = vips_enum_from_nick("enum", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
1415+
if ((format_value = vips_enum_from_nick("php-vips", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
14151416
RETURN_LONG(-1);
14161417
}
14171418
band_format = format_value;
14181419

1419-
const int size = zend_hash_num_elements(ht);
1420-
int arr[size];
1421-
int i;
1422-
1423-
for (i = 0; i < size; i++) {
1424-
zval *ele;
1425-
1426-
if ((ele = zend_hash_index_find(ht, i)) != NULL) {
1427-
arr[i] = zval_get_long(ele);
1428-
}
1429-
}
1430-
1431-
if (!(image = vips_image_new_from_memory_copy(arr, size, width, height, bands,
1420+
if (!(image = vips_image_new_from_memory_copy(bstr, bstr_len, width, height, bands,
14321421
band_format))) {
14331422
RETURN_LONG(-1);
14341423
}
@@ -1442,7 +1431,7 @@ PHP_FUNCTION(vips_image_new_from_memory)
14421431
}
14431432
/* }}} */
14441433

1445-
/* {{{ proto array vips_image_write_to_memory(resource image)
1434+
/* {{{ proto string vips_image_write_to_memory(resource image)
14461435
Write an image to a memory array. */
14471436
PHP_FUNCTION(vips_image_write_to_memory)
14481437
{
@@ -1466,12 +1455,9 @@ PHP_FUNCTION(vips_image_write_to_memory)
14661455
RETURN_LONG(-1);
14671456
}
14681457

1469-
array_init(return_value);
1458+
RETVAL_STRINGL((char *)arr, arr_len);
14701459

1471-
int i;
1472-
for (i = 0; i < arr_len; i++) {
1473-
add_next_index_long(return_value, arr[i]);
1474-
}
1460+
g_free(arr);
14751461
}
14761462
/* }}} */
14771463

0 commit comments

Comments
 (0)