Skip to content

Commit 5de1a40

Browse files
committed
Add newFromMemory / writeToMemory
1 parent 6590295 commit 5de1a40

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/Image.php

+57-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
* $im = $im->conv($mask);
124124
* ```
125125
*
126-
* `Image::new_from_array` creates an image from an array constant. The 8 at
126+
* `Image::newFromArray` creates an image from an array constant. The 8 at
127127
* the end sets the scale: the amount to divide the image by after
128128
* integer convolution. See the libvips API docs for `vips_conv()` (the operation
129129
* invoked by `Image::conv`) for details on the convolution operator. See
@@ -930,6 +930,40 @@ public static function newFromArray(
930930
return $result;
931931
}
932932

933+
/**
934+
* Wraps an Image around an area of memory containing a C-style array.
935+
*
936+
* @param array $data C-style array.
937+
* @param int $width Image width in pixels.
938+
* @param int $height Image height in pixels.
939+
* @param int $bands Number of bands.
940+
* @param string $format Band format. (@see BandFormat)
941+
*
942+
* @return Image A new Image.
943+
*/
944+
public static function newFromMemory(
945+
array $data,
946+
int $width,
947+
int $height,
948+
int $bands,
949+
string $format
950+
): Image {
951+
Utils::debugLog('newFromMemory', [
952+
'instance' => null,
953+
'arguments' => [$data, $width, $height, $bands, $format]
954+
]);
955+
956+
$result = vips_image_new_from_memory($data, $width, $height, $bands, $format);
957+
if ($result === -1) {
958+
self::errorVips();
959+
}
960+
$result = self::wrapResult($result);
961+
962+
Utils::debugLog('newFromMemory', ['result' => $result]);
963+
964+
return $result;
965+
}
966+
933967
/**
934968
* Make an interpolator from a name.
935969
*
@@ -1048,6 +1082,28 @@ public function writeToBuffer(string $suffix, array $options = []): string
10481082
return $result;
10491083
}
10501084

1085+
/**
1086+
* Write an image to a large memory array.
1087+
*
1088+
* @return array The memory array.
1089+
*/
1090+
public function writeToMemory(): array
1091+
{
1092+
Utils::debugLog('writeToMemory', [
1093+
'instance' => $this,
1094+
'arguments' => []
1095+
]);
1096+
1097+
$result = vips_image_write_to_memory($this->image);
1098+
if ($result === -1) {
1099+
self::errorVips();
1100+
}
1101+
1102+
Utils::debugLog('writeToMemory', ['result' => $result]);
1103+
1104+
return $result;
1105+
}
1106+
10511107
/**
10521108
* Copy to memory.
10531109
*

tests/NewTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ public function testVipsNewInterpolator()
107107
$this->assertNotNull($interp);
108108
$this->assertEquals($widthInput * 2, $widthOutput);
109109
}
110+
111+
public function testVipsNewFromMemory()
112+
{
113+
$byte_array = array_fill(0, 200, 0);
114+
$image = Vips\Image::newFromMemory($byte_array, 20, 10, 1, Vips\BandFormat::UCHAR);
115+
116+
$this->assertEquals($image->width, 20);
117+
$this->assertEquals($image->height, 10);
118+
$this->assertEquals($image->format, Vips\BandFormat::UCHAR);
119+
$this->assertEquals($image->bands, 1);
120+
$this->assertEquals($image->avg(), 0);
121+
}
110122
}
111123

112124
/*

tests/WriteTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public function testVipsWriteToBuffer()
5959

6060
$this->assertEquals($buffer1, $buffer2);
6161
}
62+
63+
public function testVipsWriteToMemory()
64+
{
65+
$byte_array = array_fill(0, 200, 0);
66+
$image = Vips\Image::newFromMemory($byte_array, 20, 10, 1, Vips\BandFormat::UCHAR);
67+
$mem_arr = $image->writeToMemory();
68+
69+
$this->assertEquals($byte_array, $mem_arr);
70+
}
6271
}
6372

6473
/*

0 commit comments

Comments
 (0)