Skip to content

Add newFromMemory / writeToMemory #60

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 3 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 57 additions & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
* $im = $im->conv($mask);
* ```
*
* `Image::new_from_array` creates an image from an array constant. The 8 at
* `Image::newFromArray` creates an image from an array constant. The 8 at
* the end sets the scale: the amount to divide the image by after
* integer convolution. See the libvips API docs for `vips_conv()` (the operation
* invoked by `Image::conv`) for details on the convolution operator. See
Expand Down Expand Up @@ -930,6 +930,40 @@ public static function newFromArray(
return $result;
}

/**
* Wraps an Image around an area of memory containing a C-style array.
*
* @param string $data C-style array.
* @param int $width Image width in pixels.
* @param int $height Image height in pixels.
* @param int $bands Number of bands.
* @param string $format Band format. (@see BandFormat)
*
* @return Image A new Image.
*/
public static function newFromMemory(
string $data,
int $width,
int $height,
int $bands,
string $format
): Image {
Utils::debugLog('newFromMemory', [
'instance' => null,
'arguments' => [$data, $width, $height, $bands, $format]
]);

$result = vips_image_new_from_memory($data, $width, $height, $bands, $format);
if ($result === -1) {
self::errorVips();
}
$result = self::wrapResult($result);

Utils::debugLog('newFromMemory', ['result' => $result]);

return $result;
}

/**
* Make an interpolator from a name.
*
Expand Down Expand Up @@ -1048,6 +1082,28 @@ public function writeToBuffer(string $suffix, array $options = []): string
return $result;
}

/**
* Write an image to a large memory array.
*
* @return string The memory array.
*/
public function writeToMemory(): string
{
Utils::debugLog('writeToMemory', [
'instance' => $this,
'arguments' => []
]);

$result = vips_image_write_to_memory($this->image);
if ($result === -1) {
self::errorVips();
}

Utils::debugLog('writeToMemory', ['result' => $result]);

return $result;
}

/**
* Copy to memory.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/NewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public function testVipsNewInterpolator()
$this->assertNotNull($interp);
$this->assertEquals($widthInput * 2, $widthOutput);
}

public function testVipsNewFromMemory()
{
/*$binaryStr = pack('C*', ...array_fill(0, 200, 0));
$image = Vips\Image::newFromMemory($binaryStr, 20, 10, 1, Vips\BandFormat::UCHAR);

$this->assertEquals($image->width, 20);
$this->assertEquals($image->height, 10);
$this->assertEquals($image->format, Vips\BandFormat::UCHAR);
$this->assertEquals($image->bands, 1);
$this->assertEquals($image->avg(), 0);*/
}
}

/*
Expand Down
9 changes: 9 additions & 0 deletions tests/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public function testVipsWriteToBuffer()

$this->assertEquals($buffer1, $buffer2);
}

public function testVipsWriteToMemory()
{
/*$binaryStr = pack('C*', ...array_fill(0, 200, 0));
$image = Vips\Image::newFromMemory($binaryStr, 20, 10, 1, Vips\BandFormat::UCHAR);
$memStr = $image->writeToMemory();

$this->assertEquals($binaryStr, $memStr);*/
}
}

/*
Expand Down