Skip to content

Commit fc56cff

Browse files
authored
Merge pull request #13 from kleisauke/memory
Add new_from_memory / write_to_memory
2 parents cb3718f + 2880405 commit fc56cff

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-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+
$binary_str = pack("C*", ...array_fill(0, 200, 0));
8+
$image = vips_image_new_from_memory($binary_str, 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+
$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);
10+
11+
if ($binary_str === $mem_str) {
12+
echo "pass";
13+
}
14+
?>
15+
--EXPECT--
16+
pass

vips.c

Lines changed: 87 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,78 @@ PHP_FUNCTION(vips_image_copy_memory)
13891388
}
13901389
/* }}} */
13911390

1391+
/* {{{ proto resource vips_image_new_from_memory(string 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+
char *bstr;
1396+
size_t bstr_len;
1397+
long width;
1398+
long height;
1399+
long bands;
1400+
char *format;
1401+
size_t format_len;
1402+
int format_value;
1403+
VipsBandFormat band_format;
1404+
VipsImage *image;
1405+
zend_resource *resource;
1406+
zval zvalue;
1407+
1408+
VIPS_DEBUG_MSG("vips_image_new_from_memory:\n");
1409+
1410+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slllp",
1411+
&bstr, &bstr_len, &width, &height, &bands, &format, &format_len) == FAILURE) {
1412+
RETURN_LONG(-1);
1413+
}
1414+
1415+
if ((format_value = vips_enum_from_nick("php-vips", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
1416+
RETURN_LONG(-1);
1417+
}
1418+
band_format = format_value;
1419+
1420+
if (!(image = vips_image_new_from_memory_copy(bstr, bstr_len, width, height, bands,
1421+
band_format))) {
1422+
RETURN_LONG(-1);
1423+
}
1424+
1425+
/* Return as an array for all OK, -1 for error.
1426+
*/
1427+
array_init(return_value);
1428+
resource = zend_register_resource(image, le_gobject);
1429+
ZVAL_RES(&zvalue, resource);
1430+
add_assoc_zval(return_value, "out", &zvalue);
1431+
}
1432+
/* }}} */
1433+
1434+
/* {{{ proto string vips_image_write_to_memory(resource image)
1435+
Write an image to a memory array. */
1436+
PHP_FUNCTION(vips_image_write_to_memory)
1437+
{
1438+
zval *IM;
1439+
VipsImage *image;
1440+
size_t arr_len;
1441+
uint8_t *arr;
1442+
1443+
VIPS_DEBUG_MSG("vips_image_write_to_memory:\n");
1444+
1445+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
1446+
RETURN_LONG(-1);
1447+
}
1448+
1449+
if ((image = (VipsImage *)zend_fetch_resource(Z_RES_P(IM),
1450+
"GObject", le_gobject)) == NULL) {
1451+
RETURN_LONG(-1);
1452+
}
1453+
1454+
if (!(arr = vips_image_write_to_memory(image, &arr_len))) {
1455+
RETURN_LONG(-1);
1456+
}
1457+
1458+
RETVAL_STRINGL((char *)arr, arr_len);
1459+
g_free(arr);
1460+
}
1461+
/* }}} */
1462+
13921463
/* {{{ proto string|long vips_foreign_find_load(string filename)
13931464
Find a loader for a file */
13941465
PHP_FUNCTION(vips_foreign_find_load)
@@ -1933,6 +2004,18 @@ ZEND_BEGIN_ARG_INFO(arginfo_vips_image_copy_memory, 0)
19332004
ZEND_ARG_INFO(0, image)
19342005
ZEND_END_ARG_INFO()
19352006

2007+
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_new_from_memory, 0)
2008+
ZEND_ARG_INFO(0, array)
2009+
ZEND_ARG_INFO(0, width)
2010+
ZEND_ARG_INFO(0, height)
2011+
ZEND_ARG_INFO(0, bands)
2012+
ZEND_ARG_INFO(0, format)
2013+
ZEND_END_ARG_INFO()
2014+
2015+
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_write_to_memory, 0)
2016+
ZEND_ARG_INFO(0, image)
2017+
ZEND_END_ARG_INFO()
2018+
19362019
ZEND_BEGIN_ARG_INFO(arginfo_vips_foreign_find_load, 0)
19372020
ZEND_ARG_INFO(0, filename)
19382021
ZEND_END_ARG_INFO()
@@ -2003,6 +2086,8 @@ const zend_function_entry vips_functions[] = {
20032086
PHP_FE(vips_image_write_to_file, arginfo_vips_image_write_to_file)
20042087
PHP_FE(vips_image_write_to_buffer, arginfo_vips_image_write_to_buffer)
20052088
PHP_FE(vips_image_copy_memory, arginfo_vips_image_copy_memory)
2089+
PHP_FE(vips_image_new_from_memory, arginfo_vips_image_new_from_memory)
2090+
PHP_FE(vips_image_write_to_memory, arginfo_vips_image_write_to_memory)
20062091
PHP_FE(vips_foreign_find_load, arginfo_vips_foreign_find_load)
20072092
PHP_FE(vips_foreign_find_load_buffer, arginfo_vips_foreign_find_load_buffer)
20082093
PHP_FE(vips_interpolate_new, arginfo_vips_interpolate_new)

0 commit comments

Comments
 (0)