1
1
<?php
2
-
3
- /* This file tries to make a nice API over the raw `vips_call` thing defined
4
- * in C.
2
+ /**
3
+ * php-vips is a php binding for the vips image processing library.
5
4
*/
6
5
7
6
if (!extension_loaded ("vips " )) {
8
7
dl ('vips. ' . PHP_SHLIB_SUFFIX );
9
8
}
10
9
10
+ /**
11
+ * The VImage class represents an image.
12
+ */
11
13
class VImage implements ArrayAccess
12
14
{
13
15
/* The resource for the underlying VipsImage.
14
16
*/
15
17
private $ image ;
16
18
17
- function __construct ($ image )
19
+ /**
20
+ * Wrap a VImage around an underlying vips resource.
21
+ *
22
+ * Don't call this yourself, users should stick to (for example)
23
+ * VImage::new_from_file().
24
+ *
25
+ * @param resource $image The underlying vips image resource that this
26
+ * class should wrap.
27
+ *
28
+ * @return a new VImage.
29
+ */
30
+ public function __construct ($ image )
18
31
{
19
32
$ this ->image = $ image ;
20
33
}
@@ -130,13 +143,31 @@ private static function wrap($result)
130
143
return $ result ;
131
144
}
132
145
146
+ /**
147
+ * Create a new VImage from a file on disc.
148
+ *
149
+ * @param string $filename The file to open.
150
+ * @param array $options Any options to pass on to the load operation.
151
+ *
152
+ * @return A new VImage.
153
+ */
133
154
public static function new_from_file ($ filename , $ options = [])
134
155
{
135
156
$ options = self ::unwrap ($ options );
136
157
$ result = vips_image_new_from_file ($ filename , $ options );
137
158
return self ::wrap ($ result );
138
159
}
139
160
161
+ /**
162
+ * Create a new VImage from a compressed image held as a string.
163
+ *
164
+ * @param string $buffer The formatted image to open.
165
+ * @param string $option_string Any text-style options to pass to the
166
+ * selected loader.
167
+ * @param array $options Any options to pass on to the load operation.
168
+ *
169
+ * @return A new VImage.
170
+ */
140
171
public static function new_from_buffer ($ buffer ,
141
172
$ option_string = "" , $ options = [])
142
173
{
@@ -145,37 +176,93 @@ public static function new_from_buffer($buffer,
145
176
return self ::wrap ($ result );
146
177
}
147
178
179
+ /**
180
+ * Create a new VImage from a php array.
181
+ *
182
+ * 2D arrays become 2D images. 1D arrays become 2D images with height 1.
183
+ *
184
+ * @param array $array The array to make the image from.
185
+ * @param double $scale The "scale" metadata item. Useful for integer
186
+ * convolution masks.
187
+ * @param double $offset The "offset" metadata item. Useful for integer
188
+ * convolution masks.
189
+ *
190
+ * @return A new VImage.
191
+ */
148
192
public static function new_from_array ($ array , $ scale = 1 , $ offset = 0 )
149
193
{
150
194
$ result = vips_image_new_from_array ($ array , $ scale , $ offset );
151
195
return self ::wrap ($ result );
152
196
}
153
197
198
+ /**
199
+ * Write an image to a file.
200
+ *
201
+ * @param string $filename The file to write the image to.
202
+ * @param array $options Any options to pass on to the selected save
203
+ * operation.
204
+ *
205
+ * @return bool TRUE for success and FALSE for failure.
206
+ */
154
207
public function write_to_file ($ filename , $ options = [])
155
208
{
156
209
$ options = self ::unwrap ($ options );
157
210
$ result = vips_image_write_to_file ($ this ->image , $ filename , $ options );
158
211
return self ::wrap ($ result );
159
212
}
160
213
214
+ /**
215
+ * Write an image to a formatted string.
216
+ *
217
+ * @param string $suffix The file type suffix, eg. ".jpg".
218
+ * @param array $options Any options to pass on to the selected save
219
+ * operation.
220
+ *
221
+ * @return string The formatted image.
222
+ */
161
223
public function write_to_buffer ($ suffix , $ options = [])
162
224
{
163
225
$ options = self ::unwrap ($ options );
164
226
$ result = vips_image_write_to_buffer ($ this ->image , $ suffix , $ options );
165
227
return self ::wrap ($ result );
166
228
}
167
229
230
+ /**
231
+ * Get any property from the underlying image.
232
+ *
233
+ * @param string $name The property name.
234
+ *
235
+ * @return mixed
236
+ */
168
237
public function __get ($ name )
169
238
{
170
239
$ result = vips_image_get ($ this ->image , $ name );
171
240
return self ::wrap ($ result );
172
241
}
173
242
243
+ /**
244
+ * Set any property on the underlying image.
245
+ *
246
+ * @param string $name The property name.
247
+ * @param mixed $value The value to set for this property.
248
+ *
249
+ * @return void
250
+ */
174
251
public function __set ($ name , $ value )
175
252
{
176
253
vips_image_set ($ this ->image , $ name , $ value );
177
254
}
178
255
256
+ /**
257
+ * Call any vips operation.
258
+ *
259
+ * @param string $name The operation name.
260
+ * @param VImage $instance The instance this operation is being invoked
261
+ * from.
262
+ * @param mixed[] $arguments An array of arguments to pass to the operation.
263
+ *
264
+ * @return mixed The result(s) of the operation.
265
+ */
179
266
public static function call ($ name , $ instance , $ arguments )
180
267
{
181
268
/*
@@ -208,11 +295,17 @@ private function call_enum($other, $base, $op, $options = [])
208
295
}
209
296
}
210
297
298
+ /**
299
+ * Call any vips operation as an instance method.
300
+ */
211
301
public function __call ($ name , $ arguments )
212
302
{
213
303
return self ::call ($ name , $ this , $ arguments );
214
304
}
215
305
306
+ /**
307
+ * Call any vips operation as a class method.
308
+ */
216
309
public static function __callStatic ($ name , $ arguments )
217
310
{
218
311
return self ::call ($ name , NULL , $ arguments );
@@ -245,6 +338,15 @@ public function offsetUnset($offset)
245
338
* speedup.
246
339
*/
247
340
341
+ /**
342
+ * Add to this image.
343
+ *
344
+ * @param int|double|int[]|double[]|VImage $other The thing to add to this
345
+ * image.
346
+ * @param array $options Any options to pass on to the add operation.
347
+ *
348
+ * @return VImage A new image.
349
+ */
248
350
public function add ($ other , $ options = [])
249
351
{
250
352
if ($ other instanceof VImage) {
@@ -263,7 +365,7 @@ public function subtract($other, $options = [])
263
365
}
264
366
else {
265
367
$ other = map_numeric ($ other , function ($ value ) {
266
- return -1 * $ value ;
368
+ return -1 * $ value ;
267
369
});
268
370
return self ::linear ($ this , 1 , $ other , $ options );
269
371
}
@@ -287,7 +389,7 @@ public function divide($other, $options = [])
287
389
}
288
390
else {
289
391
$ other = map_numeric ($ other , function ($ value ) {
290
- return $ value ** -1 ;
392
+ return $ value ** -1 ;
291
393
});
292
394
return self ::linear ($ this , $ other , 0 , $ options );
293
395
}
0 commit comments