Completed
Pull Request — master (#103)
by
unknown
02:36
created

Image::get_landscape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
class Image {
4
5
	/**
6
	 * Wiki class
7
	 *
8
	 * @var Wiki
9
	 * @access protected
10
	 */
11
	protected $wiki;
12
13
	/**
14
	 * Page class
15
	 *
16
	 * @var Page
17
	 * @access protected
18
	 */
19
	protected $page;
20
21
	/**
22
	 * MIME type of image
23
	 *
24
	 * @var string
25
	 * @access protected
26
	 */
27
	protected $mime;
28
29
	/**
30
	 * Bitdepth of image
31
	 *
32
	 * @var int
33
	 * @access protected
34
	 */
35
	protected $bitdepth;
36
37
	/**
38
	 * SHA1 hash of image
39
	 *
40
	 * @var string
41
	 * @access protected
42
	 */
43
	protected $hash;
44
45
	/**
46
	 * Size of image
47
	 *
48
	 * @var int
49
	 * @access protected
50
	 */
51
	protected $size;
52
53
	/**
54
	 * Metadata stored in the image
55
	 *
56
	 * @var array
57
	 * @access protected
58
	 */
59
	protected $metadata = array();
60
61
	/**
62
	 * URL to direct image
63
	 *
64
	 * @var string
65
	 * @access protected
66
	 */
67
	protected $url;
68
69
	/**
70
	 * Timestamp that of the most recent upload
71
	 *
72
	 * @var string
73
	 * @access protected
74
	 */
75
	protected $timestamp;
76
77
	/**
78
	 * Username of the most recent uploader
79
	 *
80
	 * @var string
81
	 * @access protected
82
	 */
83
	protected $user;
84
85
	/**
86
	 * Width of image
87
	 *
88
	 * @var int
89
	 * @access protected
90
	 */
91
	protected $width;
92
93
	/**
94
	 * Height of image
95
	 *
96
	 * @var int
97
	 * @access protected
98
	 */
99
	protected $height;
100
101
	/**
102
	 * Sanitized name for local storage (namespace, colons, etc all removed)
103
	 *
104
	 * @var string
105
	 * @access protected
106
	 */
107
	protected $localname;
108
109
	/**
110
	 * Image name, with namespace
111
	 *
112
	 * @var string
113
	 * @access protected
114
	 */
115
	protected $title;
116
117
	/**
118
	 * Image name, without namespace
119
	 *
120
	 * @var string
121
	 * @access protected
122
	 */
123
	protected $rawtitle;
124
125
	/**
126
	 * List of pages where the image is used
127
	 *
128
	 * @var array
129
	 * @access protected
130
	 */
131
	protected $usage = array();
132
133
	/**
134
	 * List of previous uploads
135
	 *
136
	 * @var array
137
	 * @access protected
138
	 */
139
	protected $history = array();
140
141
	/**
142
	 * Other images with identical SHA1 hashes
143
	 *
144
	 * @var array
145
	 * @access protected
146
	 */
147
	protected $duplicates = array();
148
149
	/**
150
	 * Whether image itself exists or not
151
	 *
152
	 * @var bool
153
	 * @access protected
154
	 */
155
	protected $exists = true;
156
157
	/**
158
	 * Construction method for the Image class
159
	 *
160
	 * @access public
161
	 * @param Wiki &$wikiClass The Wiki class object
162
	 * @param string $title The title of the image
163
	 * @param int $pageid The ID of the image page (optional)
164
	 * @return Image
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
165
	 */
166
	public function __construct( Wiki &$wikiClass, $title = null, $pageid = null ) {
167
168
		$this->wiki = & $wikiClass;
169
		$this->title = $title;
170
171
		if( $this->wiki->removeNamespace( $title ) == $title ) {
172
			$namespaces = $this->wiki->get_namespaces();
173
			$this->title = $namespaces[6] . ':' . $title;
174
		}
175
176
		$ii = $this->imageinfo();
177
178
		if( is_array( $ii ) ) {
179
180
                        $ii = $ii[0];
181
			
182
			$this->title = $ii['canonicaltitle'];
183
			$this->rawtitle = $this->wiki->removeNamespace( $this->title );
184
			$this->localname = str_replace( array( ' ', '+' ), array( '_', '_' ), urlencode( $this->rawtitle ) );
185
			$this->page = & $this->wiki->initPage( $this->title, $pageid );
186
			$this->mime = $ii['mime'];
187
			$this->bitdepth = $ii['bitdepth'];
188
			$this->hash = $ii['sha1'];
189
			$this->size = $ii['size'];
190
			$this->width = $ii['width'];
191
			$this->height = $ii['height'];
192
			$this->url = $ii['url'];
193
			$this->timestamp = $ii['timestamp'];
194
			$this->user = $ii['user'];
195
196
			if( is_array( $ii['metadata'] ) ) {
197
				foreach( $ii['metadata'] as $metadata ){
198
					$this->metadata[$metadata['name']] = $metadata['value'];
199
				}
200
201
        		}
202
                } else {
203
                        $this->exists = false;
204
                }
205
206
	}
207
208
	/**
209
	 *
210
	 * @access public
211
	 * @link https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii
212
	 * @param int $limit Number of results to limit to. Default 50.
213
	 * @param array $prop What image information to get.  Default all values.
214
	 * @return array|bool False if not found, otherwise array of info indexed by revision
215
	 */
216
	public function get_stashinfo( $limit = 50, $prop = array(
217
        'timestamp', 'user', 'comment', 'url', 'size', 'dimensions', 'sha1', 'mime', 'metadata', 'archivename',
218
        'bitdepth'
219
    ) ) {
220
221
		$stasharray = array(
222
			'prop'     => 'stashimageinfo',
223
			'siiprop'  => implode( '|', $prop ),
224
			'titles'   => $this->title,
225
			'_code'    => 'sii',
226
			'_limit'   => $limit,
227
			'_lhtitle' => 'stashimageinfo'
228
		);
229
230
		$stashinfo = $this->wiki->listHandler( $stasharray );
231
		if( is_array( $stashinfo ) ) {
232
			return $stashinfo[0];
233
		} else {
234
			return false;
235
		}
236
	}
237
238
	/**
239
	 * Returns various information about the image
240
	 *
241
	 * @access public
242
	 * @param int $limit Number of revisions to get info about. Default 1
243
	 * @param int $width Width of image. Default -1 (no width)
244
	 * @param int $height Height of image. Default -1 (no height)
245
	 * @param string $start Timestamp to start at. Default null
246
	 * @param string $end Timestamp to end at. Default null
247
	 * @param string[] $prop Properties to retrieve. Default array( 'timestamp', 'user', 'comment', 'url', 'size', 'sha1', 'mime', 'metadata', 'archivename', 'bitdepth' )
248
	 * @param string $version Version of metadata to use. Default 'latest'
249
	 * @param string $urlparam A handler specific parameter string. Default null
250
	 * @param bool $localonly Look only for files in the local repository. Default false
251
	 * @return array|bool False if file does not exist, otherwise array of info indexed by revision
252
	 */
253
	public function imageinfo( $limit = 1, $width = -1, $height = -1, $start = null, $end = null, $prop = array(
254
		'canonicaltitle', 'timestamp', 'userid', 'user', 'comment', 'parsedcomment', 'url', 'size', 'dimensions', 'sha1', 'mime',
255
		'thumbmime', 'mediatype', 'metadata', 'archivename', 'bitdepth'
256
	), $version = 'latest', $urlparam = null, $localonly = false ) {
257
258
		$imageInfoArray = array(
259
			'prop'              => 'imageinfo',
260
			'_code'             => 'ii',
261
			'_limit'            => $limit,
262
			'iiprop'            => implode( '|', $prop ),
263
			'iiurlwidth'        => $width,
264
			'iiurlheight'       => $height,
265
			'titles'            => $this->title,
266
			'iimetadataversion' => $version,
267
			'_lhtitle'          => 'imageinfo'
268
		);
269
270
		if( !is_null( $start ) ) $imageInfoArray['iistart'] = $start;
271
		if( !is_null( $end ) ) $imageInfoArray['iiend'] = $end;
272
		if( !is_null( $urlparam ) ) $imageInfoArray['iiurlparam'] = $urlparam;
273
		if( $localonly ) $imageInfoArray['iilocalonly'] = 'yes';
274
275
		pecho( "Getting image info for {$this->title}...\n\n", PECHO_NORMAL );
276
277
		$imageInfo = $this->wiki->listHandler( $imageInfoArray );
278
		if( count( $imageInfo ) > 0 ) {
279
			return $imageInfo;
280
		} else {
281
			// Does not exist
282
			return false;
283
		}
284
	}
285
286
	/**
287
	 * Returns the upload history of the image. If function was already called earlier in the script, it will return the local cache unless $force is set to true.
288
	 *
289
	 * @access public
290
	 * @param string $dir Which direction to go. Default 'older'
291
	 * @param int $limit Number of revisions to get. Default null (all revisions)
292
	 * @param bool $force Force generation of the cache. Default false (use cache).
293
	 * @return array Upload history.
294
	 */
295
	public function get_history( $dir = 'older', $limit = null, $force = false ) {
296
		if( !count( $this->history ) || $force ) {
297
			$this->history = $this->page->history( $limit, $dir );
298
		}
299
		return $this->history;
300
	}
301
302
	/**
303
	 * Returns all pages where the image is used. If function was already called earlier in the script, it will return the local cache unless $force is set to true.
304
	 *
305
	 * @access public
306
	 * @param string|array $namespace Namespaces to look in. If set as a string, must be set in the syntax "0|1|2|...". If an array, simply the namespace IDs to look in. Default null.
307
	 * @param string $redirects How to filter for redirects. Options are "all", "redirects", or "nonredirects". Default "all".
308
	 * @param bool $followRedir If linking page is a redirect, find all pages that link to that redirect as well. Default false.
309
	 * @param int|null $limit
310
	 * @param bool $force Force regeneration of the cache. Default false (use cache).
311
	 * @return array
312
	 */
313
	public function get_usage( $namespace = null, $redirects = "all", $followRedir = false, $limit = null, $force = false ) {
314
315
		if( $force || !count( $this->usage ) ) {
316
317
			$iuArray = array(
318
				'list'          => 'imageusage',
319
				'_code'         => 'iu',
320
				'_lhtitle'      => 'title',
321
				'iutitle'       => $this->title,
322
				'iufilterredir' => $redirects,
323
			);
324
325
			if( !is_null( $namespace ) ) {
326
327
				if( is_array( $namespace ) ) {
328
					$namespace = implode( '|', $namespace );
329
				}
330
				$iuArray['iunamespace'] = $namespace;
331
			}
332
333
			if( !is_null( $limit ) ) $iuArray['iulimit'] = $limit;
334
335
			if( $followRedir ) $iuArray['iuredirect'] = 'yes';
336
337
			pecho( "Getting image usage for {$this->title}..\n\n", PECHO_NORMAL );
338
339
			$this->usage = $this->wiki->listHandler( $iuArray );
340
341
		}
342
343
		return $this->usage;
344
	}
345
346
	/**
347
	 * Returns an array of all files with identical sha1 hashes
348
	 *
349
	 * @param int $limit Number of duplicates to get. Default null (all)
350
	 * @param bool $force Force regeneration of the cache. Default false (use cache).
351
	 * @return array Duplicate files
352
	 */
353
	public function get_duplicates( $limit = null, $force = false ) {
354
355
		if( $force || !count( $this->duplicates ) ) {
356
357
			if( !$this->get_exists() ) {
358
				return $this->duplicates;
359
			}
360
361
			$dArray = array(
362
				'action'  => 'query',
363
				'prop'    => 'duplicatefiles',
364
				'dflimit' => ( ( is_null( $limit ) ? $this->wiki->get_api_limit() + 1 : $limit ) ),
365
				'titles'  => $this->title
366
			);
367
368
			$continue = null;
369
370
			pecho( "Getting duplicate images of {$this->title}..\n\n", PECHO_NORMAL );
371
372
			while( 1 ){
373
				if( !is_null( $continue ) ) $dArray['dfcontinue'] = $continue;
374
375
				$dRes = $this->wiki->apiQuery( $dArray );
376
377
				foreach( $dRes['query']['pages'] as $x ){
378
					if( isset( $x['duplicatefiles'] ) ) {
379
						foreach( $x['duplicatefiles'] as $y ){
380
							$this->duplicates[] = $y['name'];
381
						}
382
					}
383
				}
384
385
				if( isset( $dRes['query-continue'] ) ) {
386
					foreach( $dRes['query-continue'] as $z ){
387
						$continue = $z['dfcontinue'];
388
					}
389
				} else {
390
					break;
391
				}
392
393
394
			}
395
396
		}
397
398
		return $this->duplicates;
399
	}
400
401
	/**
402
	 * Revert a file to an old version
403
	 *
404
	 * @access public
405
	 * @param string $comment Comment for inthe upload in logs (default: '')
406
	 * @param string $revertto Archive name of the revision to revert to.  Default null.
407
	 * @return boolean
408
	 */
409
	public function revert( $comment = '', $revertto = null ) {
410
		global $pgNotag, $pgTag;
411
		$tokens = $this->wiki->get_tokens();
412
413
		if( !$pgNotag ) $comment .= $pgTag;
414
		$apiArray = array(
415
			'action'   => 'filerevert',
416
			'token'    => $tokens['edit'],
417
			'comment'  => $comment,
418
			'filename' => $this->rawtitle
419
		);
420
421
		if( !is_null( $revertto ) ) {
422
			$apiArray['archivename'] = $revertto;
423
			pecho( "Reverting to $revertto" . "...\n\n", PECHO_NOTICE );
424
		} else {
425
			$ii = $this->imageinfo( 2, -1, -1, null, null, array( 'archivename' ) );
426
			if( is_array( $ii ) ) $apiArray['archivename'] = $ii[1]['archivename'];
427
			pecho( "Reverting to prior upload...\n\n", PECHO_NOTICE );
428
		}
429
430
		try{
431
			$this->preEditChecks( "Revert" );
432
		} catch( EditError $e ){
433
			pecho( "Error: $e\n\n", PECHO_FATAL );
434
			return false;
435
		}
436
437
		$result = $this->wiki->apiQuery( $apiArray, true );
438
439
		if( isset( $result['filerevert'] ) ) {
440
			if( isset( $result['filerevert']['result'] ) && $result['filerevert']['result'] == "Success" ) {
441
				$this->__construct( $this->wiki, $this->title );
442
				return true;
443
			} else {
444
				pecho( "Revert error...\n\n" . print_r( $result['filerevert'], true ) . "\n\n", PECHO_FATAL );
445
				return false;
446
			}
447
		} else {
448
			pecho( "Revert error...\n\n" . print_r( $result, true ), PECHO_FATAL );
449
			return false;
450
		}
451
	}
452
453
	/**
454
	 * Rotate the image clockwise a certain degree.
455
	 *
456
	 * @param integer $degree Degrees to rotate image clockwise
457
	 * @return boolean
458
	 */
459
	public function rotate( $degree = 90 ) {
460
		$tokens = $this->wiki->get_tokens();
461
462
		$apiArray = array(
463
			'action' => 'imagerotate',
464
			'token'  => $tokens['edit'],
465
			'titles' => $this->title
466
		);
467
		pecho( "Rotating image(s) $degree degrees...\n\n", PECHO_NOTICE );
468
469
		try{
470
			$this->preEditChecks( "Rotate" );
471
		} catch( EditError $e ){
472
			pecho( "Error: $e\n\n", PECHO_FATAL );
473
			return false;
474
		}
475
476
		$result = $this->wiki->apiQuery( $apiArray, true );
477
478
		if( isset( $result['imagerotate'] ) ) {
479
			if( isset( $result['imagerotate']['result'] ) && $result['imagerotate']['result'] == "Success" ) {
480
				$this->__construct( $this->wiki, $this->title );
481
				return true;
482
			} else {
483
				pecho( "Rotate error...\n\n" . print_r( $result['imagerotate'], true ) . "\n\n", PECHO_FATAL );
484
				return false;
485
			}
486
		} else {
487
			pecho( "Rotate error...\n\n" . print_r( $result, true ), PECHO_FATAL );
488
			return false;
489
		}
490
	}
491
492
	/**
493
	 * Upload an image to the wiki
494
	 *
495
	 * @access public
496
	 * @param string $file Identifier of a file. Flexible format (local path, URL)
497
	 * @param string $text Text on the image file page (default: '')
498
	 * @param string $comment Comment for inthe upload in logs (default: '')
499
	 * @param bool $watch Should the upload be added to the watchlist (default: false)
500
	 * @param bool $ignorewarnings Ignore warnings about the upload (default: true)
501
	 * @param bool $async Make potentially large file operations asynchronous when possible.  Default false.
502
	 * @throws BadEntryError
503
	 * @return boolean
504
	 */
505
	public function upload( $file, $text = '', $comment = '', $watch = null, $ignorewarnings = true, $async = false ) {
506
		global $pgIP, $pgNotag, $pgTag;
507
508
		if( !$pgNotag ) $comment .= $pgTag;
509
		if( !is_array( $file ) ) {
510
			if( !preg_match( '@((http(s)?:\/\/)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', $file ) ) {
511
				if( !is_file( $file ) ) {
512
513
					if( is_file( $pgIP . 'Images/' . $file ) ) {
514
						$file = $pgIP . 'Images/' . $file;
515
					}
516
517
					if( !is_file( $file ) ) {
518
						throw new BadEntryError( "FileNotFound", "The given file, or file chunks, was not found or an invalid URL was specified." );
519
					}
520
				}
521
			}
522
			pecho( "Uploading $file to {$this->title}..\n\n", PECHO_NOTICE );
523
		} else {
524
			$i = 0;
525
			foreach( $file as $chunk ){
526
				if( !is_file( $chunk ) ) {
527
528
					if( is_file( $pgIP . 'Images/' . $chunk ) ) {
529
						$file[$i] = $pgIP . 'Images/' . $chunk;
530
					}
531
532
					if( !is_file( $file[$i] ) ) {
533
						throw new BadEntryError( "FileNotFound", "The given chunk file was not found." );
534
					}
535
				}
536
				$i++;
537
			}
538
			pecho( "Uploading chunk files to {$this->title}..\n\n", PECHO_NOTICE );
539
		}
540
541
		try{
542
			$this->preEditChecks( "Uploads" );
543
		} catch( EditError $e ){
544
			pecho( "Error: $e\n\n", PECHO_FATAL );
545
			return false;
546
		}
547
548
		return $this->api_upload( $file, $text, $comment, $watch, $ignorewarnings, $async );
549
550
	}
551
552
	/**
553
	 * Upload an image to the wiki using api.php
554
	 *
555
	 * @access public
556
	 * @param mixed $file Absolute path to the image, a URL, or an array containing file chunks for a chunk upload.
557
	 * @param string $text Text on the image file page (default: '')
558
	 * @param string $comment Comment for inthe upload in logs (default: '')
559
	 * @param bool $watch Should the upload be added to the watchlist (default: false)
560
	 * @param bool $ignorewarnings Ignore warnings about the upload (default: true)
561
	 * @param bool $async Make potentially large file operations asynchronous when possible.  Default false.
562
	 * @param string $filekey Key that identifies a previous upload that was stashed temporarily. Default null.
563
	 * @notice This feature is not yet fully developed.  Manual stashing is not allowed at this time.  This will be corrected during the final release of Peachy 2.
564
	 * @return bool
565
	 */
566
	public function api_upload( $file, $text = '', $comment = '', $watch = null, $ignorewarnings = true, $async = false, $filekey = null ) {
567
568
		$tokens = $this->wiki->get_tokens();
569
570
		$apiArr = array(
571
			'action'         => 'upload',
572
			'filename'       => $this->rawtitle,
573
			'comment'        => $comment,
574
			'text'           => $text,
575
			'token'          => $tokens['edit'],
576
			'ignorewarnings' => intval( $ignorewarnings )
577
		);
578
579
		if( !is_null( $filekey ) ) $apiArr['filekey'] = $filekey;
580
581
		if( !is_null( $watch ) ) {
582
			if( $watch ) {
583
				$apiArr['watchlist'] = 'watch';
584
			} elseif( !$watch ) $apiArr['watchlist'] = 'nochange';
585
			elseif( in_array(
586
				$watch, array(
587
					'watch', 'unwatch', 'preferences', 'nochange'
588
				)
589
			) ) {
590
				$apiArr['watchlist'] = $watch;
591
			} else pecho( "Watch parameter set incorrectly.  Omitting...\n\n", PECHO_WARN );
592
		}
593
594
		if( !is_array( $file ) ) {
595
			if( is_file( $file ) ) {
596
				if( $async ) $apiArr['async'] = 'yes';
597
				$localfile = $file;
598
				$apiArr['file'] = "@$localfile";
599
			} else {
600
				$apiArr['url'] = $file;
601
				if( $async ) {
602
					$apiArr['asyncdownload'] = 'yes';
603
					$apiArr['leavemessage'] = 'yes';
604
				}
605
			}
606
		} else {
607
			$apiArr['stash'] = 'yes';
608
			$apiArr['offset'] = 0;
609
			$apiArr['filesize'] = 0;
610
			foreach( $file as $chunk ){
611
				$apiArr['filesize'] = $apiArr['filesize'] + filesize( $chunk );
612
			}
613
			foreach( $file as $chunk ){
614
				$apiArr['chunk'] = "@$chunk";
615
				pecho( "Uploading $chunk\n\n", PECHO_NOTICE );
616
				$result = $this->wiki->apiQuery( $apiArr, true );
617
				if( isset( $result['upload']['result'] ) && $result['upload']['result'] == "Continue" ) {
618
					$apiArr['filekey'] = $result['upload']['filekey'];
619
					$apiArr['offset'] = $result['upload']['offset'];
620
				} elseif( isset( $result['upload']['result'] ) && $result['upload']['result'] == "Success" ) {
621
					$apiArr['filekey'] = $result['upload']['filekey'];
622
					unset( $apiArr['offset'] );
623
					unset( $apiArr['chunk'] );
624
					unset( $apiArr['stash'] );
625
					unset( $apiArr['filesize'] );
626
					pecho( "Chunks uploaded successfully!\n\n", PECHO_NORMAL );
627
					break;
628
				} else {
629
					pecho( "Upload error...\n\n" . print_r( $result, true ) . "\n\n", PECHO_FATAL );
630
					return false;
631
				}
632
			}
633
		}
634
635
		Hooks::runHook( 'APIUpload', array( &$apiArr ) );
636
637
		$result = $this->wiki->apiQuery( $apiArr, true );
638
639
		if( isset( $result['upload'] ) ) {
640
			if( isset( $result['upload']['result'] ) && $result['upload']['result'] == "Success" ) {
641
				$this->__construct( $this->wiki, $this->title );
642
				return true;
643
			} else {
644
				pecho( "Upload error...\n\n" . print_r( $result['upload'], true ) . "\n\n", PECHO_FATAL );
645
				return false;
646
			}
647
		} else {
648
			pecho( "Upload error...\n\n" . print_r( $result, true ), PECHO_FATAL );
649
			return false;
650
		}
651
652
	}
653
654
	/**
655
	 * Downloads an image to the local disk
656
	 *
657
	 * @param string $localname Filename to store image as. Default null.
658
	 * @param int $width Width of image to download. Default -1.
659
	 * @param int $height Height of image to download. Default -1.
660
	 * @return void
661
	 */
662
	public function download( $localname = null, $width = -1, $height = -1 ) {
663
		global $pgIP;
664
665
		if( !$this->get_exists() ) {
666
			pecho( "Attempted to download a non-existant file.", PECHO_NOTICE );
667
		}
668
669
		$ii = $this->imageinfo( 1, $width, $height );
670
671
		if( is_array( $ii ) ) {
672
			$ii = $ii[0];
673
674
			if( $width != -1 ) {
675
				$url = $ii['thumburl'];
676
			} else {
677
				$url = $ii['url'];
678
			}
679
680
			if( is_null( $localname ) ) {
681
				$localname = $pgIP . 'Images/' . $this->localname;
682
			}
683
684
			Hooks::runHook( 'DownloadImage', array( &$url, &$localname ) );
685
686
			pecho( "Downloading {$this->title} to $localname..\n\n", PECHO_NOTICE );
687
688
			$this->wiki->get_http()->download( $url, $localname );
689
		} else {
690
			pecho( "Error in getting image URL.\n\n" . print_r( $ii ) . "\n\n", PECHO_FATAL );
691
		}
692
	}
693
694
	/**
695
	 * Resize an image
696
	 *
697
	 * @access public
698
	 * @param int $width Width of resized image. Default null
699
	 * @param int $height Height of resized image. Default null.
700
	 * @param bool $reupload Whether or not to automatically upload the image again. Default false
701
	 * @param string $text Text to use for the image name
702
	 * @param string $comment Upload comment.
703
	 * @param bool $watch Whether or not to watch the image on uploading
704
	 * @param bool $ignorewarnings Whether or not to ignore upload warnings
705
	 * @throws DependencyError Relies on GD PHP plugin
706
	 * @throws BadEntryError
707
	 * @return boolean|void False on failure
708
	 */
709
	public function resize( $width = null, $height = null, $reupload = false, $text = '', $comment = '', $watch = null, $ignorewarnings = true ) {
710
		global $pgIP, $pgNotag, $pgTag;
711
712
		try{
713
			$this->preEditChecks( "Resize" );
714
		} catch( EditError $e ){
715
			pecho( "Error: $e\n\n", PECHO_FATAL );
716
			return false;
717
		}
718
719
		if( !$pgNotag ) $comment .= $pgTag;
720
		if( !function_exists( 'ImageCreateTrueColor' ) ) {
721
			throw new DependencyError( "GD", "http://us2.php.net/manual/en/book.image.php" );
0 ignored issues
show
Documentation introduced by
'http://us2.php.net/manual/en/book.image.php' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
722
		}
723
		echo "1\n";
724
		if( !is_null( $width ) && !is_null( $height ) ) {
725
			$this->download();
726
727
			$type = substr( strrchr( $this->mime, '/' ), 1 );
728
729
			switch( $type ){
730
				case 'jpeg':
731
					$image_create_func = 'ImageCreateFromJPEG';
732
					$image_save_func = 'ImageJPEG';
733
					break;
734
735
				case 'png':
736
					$image_create_func = 'ImageCreateFromPNG';
737
					$image_save_func = 'ImagePNG';
738
					break;
739
740
				case 'bmp':
741
					$image_create_func = 'ImageCreateFromBMP';
742
					$image_save_func = 'ImageBMP';
743
					break;
744
745
				case 'gif':
746
					$image_create_func = 'ImageCreateFromGIF';
747
					$image_save_func = 'ImageGIF';
748
					break;
749
750
				case 'vnd.wap.wbmp':
751
					$image_create_func = 'ImageCreateFromWBMP';
752
					$image_save_func = 'ImageWBMP';
753
					break;
754
755
				case 'xbm':
756
					$image_create_func = 'ImageCreateFromXBM';
757
					$image_save_func = 'ImageXBM';
758
					break;
759
760
				default:
761
					$image_create_func = 'ImageCreateFromJPEG';
762
					$image_save_func = 'ImageJPEG';
763
			}
764
			echo "2\n";
765
			$image = imagecreatetruecolor( $width, $height );
766
			echo "3\n";
767
			$new_image = $image_create_func( $pgIP . 'Images/' . $this->localname );
768
769
			$info = getimagesize( $pgIP . 'Images/' . $this->localname );
770
771
			imagecopyresampled( $image, $new_image, 0, 0, 0, 0, $width, $height, $info[0], $info[1] );
772
773
			$image_save_func( $image, $pgIP . 'Images/' . $this->localname );
774
775
		} elseif( !is_null( $width ) ) {
776
			$this->download( null, $width );
777
		} elseif( !is_null( $height ) ) {
778
			$this->download( null, $height + 100000, $height );
779
		} else {
780
			throw new BadEntryError( "NoParams", "No parameters given" );
781
		}
782
783
		if( $reupload ) {
784
			return $this->upload( null, $text, $comment, $watch, $ignorewarnings );
785
		}
786
	}
787
788
	/**
789
	 * Returns the normalized image name
790
	 *
791
	 * @param bool $namespace Whether or not to include the File: part of the name. Default true.
792
	 * @return string
793
	 */
794
	public function get_title( $namespace = true ) {
795
		if( $namespace ) {
796
			return $this->title;
797
		} else {
798
			return $this->rawtitle;
799
		}
800
	}
801
802
	/**
803
	 * Deletes the image and page.
804
	 *
805
	 * @param string $reason A reason for the deletion. Defaults to null (blank).
806
	 * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default preferences.
807
	 * @param string $oldimage The name of the old image to delete as provided by iiprop=archivename
808
	 * @return bool True on success
809
	 */
810
	public function delete( $reason = null, $watch = null, $oldimage = null ) {
811
		return $this->page->delete( $reason, $watch, $oldimage );
812
	}
813
814
	/*
815
	 * Performs new message checking, etc
816
	 *
817
	 * @access public
818
	 * @return void
819
	 */
820
	protected function preEditChecks( $action = "Edit" ) {
821
		$this->wiki->preEditChecks( $action );
822
	}
823
824
	/**
825
	 * Returns the sanitized local disk name
826
	 *
827
	 * @return string
828
	 */
829
	public function get_localname() {
830
		return $this->localname;
831
	}
832
833
	/**
834
	 * Whether or not the image exists
835
	 *
836
	 * @return bool
837
	 */
838
	public function get_exists() {
839
		return $this->exists;
840
	}
841
842
	/**
843
	 * Returns a page class for the image
844
	 *
845
	 * @return Page
846
	 */
847
	public function &get_page() {
848
		return $this->page;
849
	}
850
851
	/**
852
	 * Returns the MIME type of the image
853
	 *
854
	 * @return string
855
	 */
856
	public function get_mime() {
857
		return $this->mime;
858
	}
859
860
	/**
861
	 * Returns the bitdepth of the image
862
	 *
863
	 * @return int
864
	 */
865
	public function get_bitdepth() {
866
		return $this->bitdepth;
867
	}
868
869
	/**
870
	 * Returns the SHA1 hash of the image
871
	 *
872
	 * @return string
873
	 */
874
	public function get_hash() {
875
		return $this->hash;
876
	}
877
878
	/**
879
	 * Returns the size of the image, in bytes
880
	 *
881
	 * @return int
882
	 */
883
	public function get_size() {
884
		return $this->size;
885
	}
886
887
	/**
888
	 * Returns the metadata of the image
889
	 *
890
	 * @return array
891
	 */
892
	public function get_metadata() {
893
		return $this->metadata;
894
	}
895
896
	/**
897
	 * Returns the direct URL of the image
898
	 *
899
	 * @return string
900
	 */
901
	public function get_url() {
902
		return $this->url;
903
	}
904
905
	/**
906
	 * Returns the timestamp that of the most recent upload
907
	 *
908
	 * @return string
909
	 */
910
	public function get_timestamp() {
911
		return $this->timestamp;
912
	}
913
914
	/**
915
	 * Returns the username of the most recent uploader
916
	 *
917
	 * @return string
918
	 */
919
	public function get_user() {
920
		return $this->user;
921
	}
922
923
	/**
924
	 * Returns the width of the image
925
	 *
926
	 * @return int
927
	 */
928
	public function get_width() {
929
		return $this->width;
930
	}
931
932
	/**
933
	 * Returns the height of the image
934
	 *
935
	 * @return int
936
	 */
937
	public function get_height() {
938
		return $this->height;
939
	}
940
941
	/**
942
	 * Whether or not the image is in landscape format (width greater than height)
943
	 *
944
	 * @return bool
945
	 */
946
	public function get_landscape() {
947
		return ($this->width > $this->height);
948
	}
949
950
}
951