| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use Exception; |
| 15 |
use Imagick; |
| 16 |
use ImagickPixel; |
| 17 |
use MediaWiki\Shell\Shell; |
| 18 |
use SimpleXMLElement; |
| 19 |
use UnexpectedValueException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class RandomImageGenerator { |
| 26 |
|
| 27 |
private $minWidth = 16; |
| 28 |
|
| 29 |
private $maxWidth = 16; |
| 30 |
|
| 31 |
private $minHeight = 16; |
| 32 |
|
| 33 |
private $maxHeight = 16; |
| 34 |
|
| 35 |
public function __construct( $options = [] ) { |
| 36 |
foreach ( [ 'minWidth', 'minHeight', |
| 37 |
'maxWidth', 'maxHeight' ] as $property |
| 38 |
) { |
| 39 |
if ( isset( $options[$property] ) ) { |
| 40 |
$this->$property = $options[$property]; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function writeImages( int $number, string $format = 'svg', ?string $dir = null ): array { |
| 55 |
$filenames = $this->getRandomFilenames( $number, $format, $dir ?? getcwd() ); |
| 56 |
$imageWriteMethod = $this->getImageWriteMethod( $format ); |
| 57 |
foreach ( $filenames as $filename ) { |
| 58 |
$imageWriteMethod( $this->getImageSpec(), $format, $filename ); |
| 59 |
} |
| 60 |
|
| 61 |
return $filenames; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private function getImageWriteMethod( string $format ): callable { |
| 72 |
global $wgUseImageMagick, $wgImageMagickConvertCommand; |
| 73 |
if ( $format === 'svg' ) { |
| 74 |
return [ $this, 'writeSvg' ]; |
| 75 |
} else { |
| 76 |
|
| 77 |
global $wgExiv2Command; |
| 78 |
if ( class_exists( Imagick::class ) && $wgExiv2Command && is_executable( $wgExiv2Command ) ) { |
| 79 |
return [ $this, 'writeImageWithApi' ]; |
| 80 |
} elseif ( $wgUseImageMagick |
| 81 |
&& $wgImageMagickConvertCommand |
| 82 |
&& is_executable( $wgImageMagickConvertCommand ) |
| 83 |
) { |
| 84 |
return [ $this, 'writeImageWithCommandLine' ]; |
| 85 |
} |
| 86 |
} |
| 87 |
throw new Exception( "RandomImageGenerator: could not find a suitable " |
| 88 |
. "method to write images in '$format' format" ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private function getRandomFilenames( int $number, string $extension, string $dir ): array { |
| 99 |
$filenames = []; |
| 100 |
$prefix = wfRandomString( 3 ) . '_' . gmdate( 'YmdHis' ) . '_'; |
| 101 |
foreach ( range( 1, $number ) as $offset ) { |
| 102 |
$filename = $prefix . $offset . '.' . $extension; |
| 103 |
$filenames[] = "$dir/$filename"; |
| 104 |
} |
| 105 |
|
| 106 |
return $filenames; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function getImageSpec(): array { |
| 116 |
return [ |
| 117 |
'width' => mt_rand( $this->minWidth, $this->maxWidth ), |
| 118 |
'height' => mt_rand( $this->minHeight, $this->maxHeight ), |
| 119 |
'fill' => '#f0f', |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private function writeSvg( array $spec, string $format, string $filename ): void { |
| 130 |
$svg = new SimpleXmlElement( '<svg/>' ); |
| 131 |
$svg->addAttribute( 'xmlns', 'http://www.w3.org/2000/svg' ); |
| 132 |
$svg->addAttribute( 'width', $spec['width'] ); |
| 133 |
$svg->addAttribute( 'height', $spec['height'] ); |
| 134 |
|
| 135 |
$fh = fopen( $filename, 'w' ); |
| 136 |
if ( !$fh ) { |
| 137 |
throw new UnexpectedValueException( "couldn't open $filename for writing" ); |
| 138 |
} |
| 139 |
fwrite( $fh, $svg->asXML() ); |
| 140 |
if ( !fclose( $fh ) ) { |
| 141 |
throw new UnexpectedValueException( "couldn't close $filename" ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private function writeImageWithApi( array $spec, string $format, string $filename ): void { |
| 149 |
$image = new Imagick(); |
| 150 |
$image->newImage( $spec['width'], $spec['height'], new ImagickPixel( $spec['fill'] ) ); |
| 151 |
$image->setImageFormat( $format ); |
| 152 |
$image->writeImage( $filename ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private function writeImageWithCommandLine( array $spec, string $format, string $filename ): void { |
| 166 |
global $wgImageMagickConvertCommand; |
| 167 |
|
| 168 |
$args = [ |
| 169 |
$wgImageMagickConvertCommand, |
| 170 |
'-size', |
| 171 |
$spec['width'] . 'x' . $spec['height'], |
| 172 |
"xc:{$spec['fill']}", |
| 173 |
$filename, |
| 174 |
]; |
| 175 |
Shell::command( $args )->execute(); |
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
class_alias( RandomImageGenerator::class, 'RandomImageGenerator' ); |