Skip to content

Add new_from_memory / write_to_memory #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/037.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ can make an image from memory
<?php if (!extension_loaded("vips")) print "skip"; ?>
--FILE--
<?php
$byte_array = array_fill(0, 200, 0);
$image = vips_image_new_from_memory($byte_array, 20, 10, 1, 'uchar')["out"];
$binary_str = pack("c*", ...array_fill(0, 200, 0));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use "C*" (unsigned char) here instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Probably, yes.

$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"];
$width = vips_image_get($image, "width")["out"];
$height = vips_image_get($image, "height")["out"];
$format = vips_image_get($image, "format")["out"];
Expand Down
8 changes: 4 additions & 4 deletions tests/038.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ can write to memory
<?php if (!extension_loaded("vips")) print "skip"; ?>
--FILE--
<?php
$byte_array = array_fill(0, 200, 0);
$image = vips_image_new_from_memory($byte_array, 20, 10, 1, 'uchar')["out"];
$mem_arr = vips_image_write_to_memory($image);
$binary_str = pack("c*", ...array_fill(0, 200, 0));
$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"];
$mem_str = vips_image_write_to_memory($image);

if ($byte_array === $mem_arr) {
if ($binary_str === $mem_str) {
echo "pass";
}
?>
Expand Down
34 changes: 9 additions & 25 deletions vips.c
Original file line number Diff line number Diff line change
Expand Up @@ -1388,11 +1388,12 @@ PHP_FUNCTION(vips_image_copy_memory)
}
/* }}} */

/* {{{ proto resource vips_image_write_to_memory(array data, integer width, integer height, integer bands, string format)
/* {{{ proto resource vips_image_new_from_memory(string data, integer width, integer height, integer bands, string format)
Wrap an image around a memory array. */
PHP_FUNCTION(vips_image_new_from_memory)
{
HashTable *ht;
char *bstr;
size_t bstr_len;
long width;
long height;
long bands;
Expand All @@ -1406,29 +1407,17 @@ PHP_FUNCTION(vips_image_new_from_memory)

VIPS_DEBUG_MSG("vips_image_new_from_memory:\n");

if (zend_parse_parameters(ZEND_NUM_ARGS(), "hlllp",
&ht, &width, &height, &bands, &format, &format_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slllp",
&bstr, &bstr_len, &width, &height, &bands, &format, &format_len) == FAILURE) {
RETURN_LONG(-1);
}

if ((format_value = vips_enum_from_nick("enum", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
if ((format_value = vips_enum_from_nick("php-vips", VIPS_TYPE_BAND_FORMAT, format)) < 0) {
RETURN_LONG(-1);
}
band_format = format_value;

const int size = zend_hash_num_elements(ht);
int arr[size];
int i;

for (i = 0; i < size; i++) {
zval *ele;

if ((ele = zend_hash_index_find(ht, i)) != NULL) {
arr[i] = zval_get_long(ele);
}
}

if (!(image = vips_image_new_from_memory_copy(arr, size, width, height, bands,
if (!(image = vips_image_new_from_memory_copy(bstr, bstr_len, width, height, bands,
band_format))) {
RETURN_LONG(-1);
}
Expand All @@ -1442,7 +1431,7 @@ PHP_FUNCTION(vips_image_new_from_memory)
}
/* }}} */

/* {{{ proto array vips_image_write_to_memory(resource image)
/* {{{ proto string vips_image_write_to_memory(resource image)
Write an image to a memory array. */
PHP_FUNCTION(vips_image_write_to_memory)
{
Expand All @@ -1466,12 +1455,7 @@ PHP_FUNCTION(vips_image_write_to_memory)
RETURN_LONG(-1);
}

array_init(return_value);

int i;
for (i = 0; i < arr_len; i++) {
add_next_index_long(return_value, arr[i]);
}
RETVAL_STRINGL((char *)arr, arr_len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RETVAL_STRINGL will take a copy of the string, so we need a g_free(arr); afterwards to avoid a leak (I think).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2880405.

}
/* }}} */

Expand Down