forked from char0n/ffmpeg-php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFFmpegFrame.php
193 lines (175 loc) · 5.9 KB
/
FFmpegFrame.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* FFmpegFrame represents one frame from the movie
*
* @author char0n (Vladimir Gorej)
* @package FFmpegPHP
* @license New BSD
* @version 1.5-b1
*/
class FFmpegFrame implements Serializable {
protected static $EX_CODE_NO_VALID_RESOURCE = 334563;
/**
* GdImage binary data
*
* @var string
*/
protected $gdImageData;
/**
* Presentation time stamp
*
* @var float
*/
protected $pts;
/**
* Frame width in pixels
*
* @var int
*/
protected $width;
/**
* Frame height in pixels
*
* @var int
*/
protected $height;
/**
* Create a FFmpegFrame object from a GD image.
*
* @param resource $gdImage image resource of type gd
* @param float $pts frame presentation timestamp; OPTIONAL parameter; DEFAULT value 0.0
* @throws Exception
* @return FFmpegFrame
*/
public function __construct($gdImage, $pts = 0.0) {
if (!(is_resource($gdImage) && get_resource_type($gdImage) == 'gd')) {
throw new Exception('Param given by constructor is not valid gd resource', self::$EX_CODE_NO_VALID_RESOURCE);
}
$this->gdImageData = $this->gdImageToBinaryData($gdImage);
$this->width = imagesx($gdImage);
$this->height = imagesy($gdImage);
$this->pts = $pts;
}
/**
* Return the width of the frame.
*
* @return int
*/
public function getWidth() {
return $this->width;
}
/**
* Return the height of the frame.
*
* @return int
*/
public function getHeight() {
return $this->height;
}
/**
* Return the presentation time stamp of the frame; alias $frame->getPresentationTimestamp()
*
* @return float
*/
public function getPTS() {
return $this->pts;
}
/**
* Return the presentation time stamp of the frame.
*
* @return float
*/
public function getPresentationTimestamp() {
return $this->getPTS();
}
/**
* Resize and optionally crop the frame. (Cropping is built into ffmpeg resizing so I'm providing it here for completeness.)
*
* * width - New width of the frame (must be an even number).
* * height - New height of the frame (must be an even number).
* * croptop - Remove [croptop] rows of pixels from the top of the frame.
* * cropbottom - Remove [cropbottom] rows of pixels from the bottom of the frame.
* * cropleft - Remove [cropleft] rows of pixels from the left of the frame.
* * cropright - Remove [cropright] rows of pixels from the right of the frame.
*
*
* NOTE: Cropping is always applied to the frame before it is resized. Crop values must be even numbers.
*
* @param int $width
* @param int $height
* @param int $cropTop OPTIONAL parameter; DEFAULT value - 0
* @param int $cropBottom OPTIONAL parameter; DEFAULT value - 0
* @param int $cropLeft OPTIONAL parameter; DEFAULT value - 0
* @param int $cropRight OPTIONAL parameter; DEFAULT value - 0
* @return void
*/
public function resize($width, $height, $cropTop = 0, $cropBottom = 0, $cropLeft = 0, $cropRight = 0) {
$widthCrop = ($cropLeft + $cropRight);
$heightCrop = ($cropTop + $cropBottom);
$width -= $widthCrop;
$height -= $heightCrop;
$resizedImage = imagecreatetruecolor($width, $height);
$gdImage = $this->toGDImage();
imagecopyresampled($resizedImage, $gdImage, 0, 0, $cropLeft, $cropTop, $width, $height, $this->getWidth() - $widthCrop, $this->getHeight() - $heightCrop);
imageconvolution($resizedImage, array(
array( -1, -1, -1 ),
array( -1, 24, -1 ),
array( -1, -1, -1 ),
), 16, 0);
$this->gdImageData = $this->gdImageToBinaryData($resizedImage);
$this->width = imagesx($resizedImage);
$this->height = imagesy($resizedImage);
imagedestroy($gdImage);
imagedestroy($resizedImage);
}
/**
* Crop the frame.
*
* * croptop - Remove [croptop] rows of pixels from the top of the frame.
* * cropbottom - Remove [cropbottom] rows of pixels from the bottom of the frame.
* * cropleft - Remove [cropleft] rows of pixels from the left of the frame.
* * cropright - Remove [cropright] rows of pixels from the right of the frame.
*
* NOTE: Crop values must be even numbers.
*
* @param int $cropTop
* @param int $cropBottom OPTIONAL parameter; DEFAULT value - 0
* @param int $cropLeft OPTIONAL parameter; DEFAULT value - 0
* @param int $cropRight OPTIONAL parameter; DEFAULT value - 0
* @return void
*/
public function crop($cropTop, $cropBottom = 0, $cropLeft = 0, $cropRight = 0) {
$this->resize($this->getWidth(), $this->getHeight(), $cropTop, $cropBottom, $cropLeft, $cropRight);
}
/**
* Returns a truecolor GD image of the frame.
*
* @return resource resource of type gd
*/
public function toGDImage() {
return imagecreatefromstring($this->gdImageData);
}
protected function gdImageToBinaryData($gdImage) {
ob_start();
imagegd2($gdImage);
return ob_get_clean();
}
public function serialize() {
$data = array(
$this->gdImageData,
$this->pts,
$this->width,
$this->height
);
return serialize($data);
}
public function unserialize($serialized) {
$data = unserialize($serialized);
list($this->gdImageData,
$this->pts,
$this->width,
$this->height
) = $data;
}
}
?>