Skip to content

Commit 9ad169a

Browse files
committed
Merge branch 'memory' of https://github.com/kleisauke/php-vips-ext into kleisauke-memory
2 parents 8248d07 + 1eeadec commit 9ad169a

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

tests/037.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
can make an image from memory
3+
--SKIPIF--
4+
<?php if (!extension_loaded("vips")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$byte_array = array_fill(0, 200, 0);
8+
$image = vips_image_new_from_memory($byte_array, 20, 10, 1, 'uchar')["out"];
9+
$width = vips_image_get($image, "width")["out"];
10+
$height = vips_image_get($image, "height")["out"];
11+
$format = vips_image_get($image, "format")["out"];
12+
$bands = vips_image_get($image, "bands")["out"];
13+
$avg = vips_call("avg", $image)["out"];
14+
15+
if ($width == 20 &&
16+
$height == 10 &&
17+
$format == 'uchar' &&
18+
$bands == 1 &&
19+
$avg == 0) {
20+
echo "pass";
21+
}
22+
?>
23+
--EXPECT--
24+
pass

tests/038.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
can write to memory
3+
--SKIPIF--
4+
<?php if (!extension_loaded("vips")) print "skip"; ?>
5+
--FILE--
6+
<?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);
10+
11+
if ($byte_array === $mem_arr) {
12+
echo "pass";
13+
}
14+
?>
15+
--EXPECT--
16+
pass

vips.c

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,8 +1375,7 @@ PHP_FUNCTION(vips_image_copy_memory)
13751375
RETURN_LONG(-1);
13761376
}
13771377

1378-
new_image = vips_image_copy_memory(image);
1379-
if (!new_image) {
1378+
if (!(new_image = vips_image_copy_memory(image))) {
13801379
RETURN_LONG(-1);
13811380
}
13821381

@@ -1389,6 +1388,93 @@ PHP_FUNCTION(vips_image_copy_memory)
13891388
}
13901389
/* }}} */
13911390

1391+
/* {{{ proto resource vips_image_write_to_memory(array data, integer width, integer height, integer bands, string format)
1392+
Wrap an image around a memory array. */
1393+
PHP_FUNCTION(vips_image_new_from_memory)
1394+
{
1395+
HashTable *ht;
1396+
long width;
1397+
long height;
1398+
long bands;
1399+
char *format;
1400+
size_t format_len;
1401+
int format_value;
1402+
VipsBandFormat band_format;
1403+
VipsImage *image;
1404+
zend_resource *resource;
1405+
zval zvalue;
1406+
1407+
VIPS_DEBUG_MSG("vips_image_new_from_memory:\n");
1408+
1409+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "hlllp",
1410+
&ht, &width, &height, &bands, &format, &format_len) == FAILURE) {
1411+
RETURN_LONG(-1);
1412+
}
1413+
1414+
if ((format_value = vips_enum_from_nick("enum", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
1415+
RETURN_LONG(-1);
1416+
}
1417+
band_format = format_value;
1418+
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,
1432+
band_format))) {
1433+
RETURN_LONG(-1);
1434+
}
1435+
1436+
/* Return as an array for all OK, -1 for error.
1437+
*/
1438+
array_init(return_value);
1439+
resource = zend_register_resource(image, le_gobject);
1440+
ZVAL_RES(&zvalue, resource);
1441+
add_assoc_zval(return_value, "out", &zvalue);
1442+
}
1443+
/* }}} */
1444+
1445+
/* {{{ proto array vips_image_write_to_memory(resource image)
1446+
Write an image to a memory array. */
1447+
PHP_FUNCTION(vips_image_write_to_memory)
1448+
{
1449+
zval *IM;
1450+
VipsImage *image;
1451+
size_t arr_len;
1452+
uint8_t *arr;
1453+
1454+
VIPS_DEBUG_MSG("vips_image_write_to_memory:\n");
1455+
1456+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
1457+
RETURN_LONG(-1);
1458+
}
1459+
1460+
if ((image = (VipsImage *)zend_fetch_resource(Z_RES_P(IM),
1461+
"GObject", le_gobject)) == NULL) {
1462+
RETURN_LONG(-1);
1463+
}
1464+
1465+
if (!(arr = vips_image_write_to_memory(image, &arr_len))) {
1466+
RETURN_LONG(-1);
1467+
}
1468+
1469+
array_init(return_value);
1470+
1471+
int i;
1472+
for (i = 0; i < arr_len; i++) {
1473+
add_next_index_long(return_value, arr[i]);
1474+
}
1475+
}
1476+
/* }}} */
1477+
13921478
/* {{{ proto string|long vips_foreign_find_load(string filename)
13931479
Find a loader for a file */
13941480
PHP_FUNCTION(vips_foreign_find_load)
@@ -1933,6 +2019,18 @@ ZEND_BEGIN_ARG_INFO(arginfo_vips_image_copy_memory, 0)
19332019
ZEND_ARG_INFO(0, image)
19342020
ZEND_END_ARG_INFO()
19352021

2022+
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_new_from_memory, 0)
2023+
ZEND_ARG_INFO(0, array)
2024+
ZEND_ARG_INFO(0, width)
2025+
ZEND_ARG_INFO(0, height)
2026+
ZEND_ARG_INFO(0, bands)
2027+
ZEND_ARG_INFO(0, format)
2028+
ZEND_END_ARG_INFO()
2029+
2030+
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_write_to_memory, 0)
2031+
ZEND_ARG_INFO(0, image)
2032+
ZEND_END_ARG_INFO()
2033+
19362034
ZEND_BEGIN_ARG_INFO(arginfo_vips_foreign_find_load, 0)
19372035
ZEND_ARG_INFO(0, filename)
19382036
ZEND_END_ARG_INFO()
@@ -2003,6 +2101,8 @@ const zend_function_entry vips_functions[] = {
20032101
PHP_FE(vips_image_write_to_file, arginfo_vips_image_write_to_file)
20042102
PHP_FE(vips_image_write_to_buffer, arginfo_vips_image_write_to_buffer)
20052103
PHP_FE(vips_image_copy_memory, arginfo_vips_image_copy_memory)
2104+
PHP_FE(vips_image_new_from_memory, arginfo_vips_image_new_from_memory)
2105+
PHP_FE(vips_image_write_to_memory, arginfo_vips_image_write_to_memory)
20062106
PHP_FE(vips_foreign_find_load, arginfo_vips_foreign_find_load)
20072107
PHP_FE(vips_foreign_find_load_buffer, arginfo_vips_foreign_find_load_buffer)
20082108
PHP_FE(vips_interpolate_new, arginfo_vips_interpolate_new)

0 commit comments

Comments
 (0)