GifByteStream.php
2.1 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Grafika\Gd\Helper;
/**
* Class GifByteStream
* Normalize string operations.
* Treat string as byte stream where 2 string characters are treated as 1 hex string (byte).
* Eg. String ffff with length 4 is 0xff 0xff in bytes with length of 2.
*/
final class GifByteStream
{
/**
* @var int
*/
private $position;
/**
* @var string
*/
private $bytes;
/**
* GifByteStream constructor.
*
* @param string $bytes Accepts only the string created by unpack('H*')
*/
public function __construct($bytes)
{
$this->position = 0;
$this->bytes = $bytes;
}
/**
* Take a bite from the byte stream.
*
* @param int $size Byte size in integer.
*
* @return string
*/
public function bite($size)
{
$str = substr($this->bytes, $this->position * 2, $size * 2);
$this->position += $size;
return $str;
}
/**
* @param $byteString
* @param $offset
*
* @return bool|float
*/
public function find($byteString, $offset)
{
$pos = strpos($this->bytes, $byteString, $offset * 2);
if ($pos !== false) {
return $pos / 2;
}
return false;
}
/**
* @param int $step
*/
public function back($step = 1)
{
$this->position -= $step;
}
/**
* @param int $step
*/
public function next($step = 1)
{
$this->position += $step;
}
/**
* @return float
*/
public function length()
{
return strlen($this->bytes) / 2;
}
/**
* @param $position
*/
public function setPosition($position)
{
$this->position = $position;
}
/**
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* @return mixed
*/
public function getBytes()
{
return $this->bytes;
}
/**
* @return bool
*/
public function isEnd()
{
if ($this->position > $this->length() - 1) {
return true;
}
return false;
}
}