forked from libvips/php-vips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceResource.php
More file actions
43 lines (37 loc) · 1.05 KB
/
Copy pathSourceResource.php
File metadata and controls
43 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Jcupitt\Vips;
class SourceResource extends SourceCustom
{
/**
* @var resource
*/
private $resource;
/**
* The resource passed in will become "owned" by this class.
* On destruction of this class, the resource will be closed.
*
* @param resource $resource
*/
public function __construct($resource)
{
$this->resource = $resource;
parent::__construct();
$this->onRead(static function (int $length) use (&$resource): ?string {
return (($read = fread($resource, $length)) !== '') ?
$read :
null;
});
if (stream_get_meta_data($resource)['seekable']) {
$this->onSeek(static function (int $offset, int $whence) use (&$resource): int {
return fseek($resource, $offset, $whence) === 0 ?
ftell($resource) :
-1;
});
}
}
public function __destruct()
{
fclose($this->resource);
parent::__destruct();
}
}