Index: branches/uploadwizard/phase3/maintenance/tests/phpunit/includes/api/generateRandomImages.php |
— | — | @@ -0,0 +1,25 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +require("RandomImageGenerator.php"); |
| 5 | + |
| 6 | +$getOptSpec = array( |
| 7 | + 'dictionaryFile::', |
| 8 | + 'minWidth::', |
| 9 | + 'maxWidth::', |
| 10 | + 'minHeight::', |
| 11 | + 'maxHeight::', |
| 12 | + 'circlesToDraw::', |
| 13 | + |
| 14 | + 'number::', |
| 15 | + 'format::' |
| 16 | +); |
| 17 | +$options = getopt( null, $getOptSpec ); |
| 18 | + |
| 19 | +$format = isset( $options['format'] ) ? $options['format'] : 'jpg'; |
| 20 | +unset( $options['format'] ); |
| 21 | + |
| 22 | +$number = isset( $options['number'] ) ? int( $options['number'] ) : 10; |
| 23 | +unset( $options['number'] ); |
| 24 | + |
| 25 | +$randomImageGenerator = new RandomImageGenerator( $options ); |
| 26 | +$randomImageGenerator->writeImages( $number, $format ); |
Property changes on: branches/uploadwizard/phase3/maintenance/tests/phpunit/includes/api/generateRandomImages.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 27 | + native |
Index: branches/uploadwizard/extensions/UploadWizard/test/php/scripts/RandomImageGenerator.php |
— | — | @@ -1,203 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/* |
5 | | - * RandomImageGenerator -- does what it says on the tin. |
6 | | - * |
7 | | - * Because MediaWiki tests the uniqueness of media upload content, and filenames, it is sometimes useful to generate |
8 | | - * files that are guaranteed (or at least very likely) to be unique in both those ways. |
9 | | - * This generates a number of filenames with random names and random content (colored circles) |
10 | | - * |
11 | | - * Requires Imagick, the ImageMagick library for PHP. |
12 | | - * |
13 | | - * @file |
14 | | - * @author Neil Kandalgaonkar <neilk@wikimedia.org> |
15 | | - */ |
16 | | - |
17 | | - |
18 | | -/** |
19 | | - * Does what it says on the tin. |
20 | | - * Can fetch a random image, or also write a number of them to disk with random filenames. |
21 | | - */ |
22 | | -class RandomImageGenerator { |
23 | | - |
24 | | - private $dictionaryFile; |
25 | | - private $minWidth = 400; |
26 | | - private $maxWidth = 800; |
27 | | - private $minHeight = 400; |
28 | | - private $maxHeight = 800; |
29 | | - private $circlesToDraw = 5; |
30 | | - |
31 | | - public function __construct( $options ) { |
32 | | - foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight', 'maxHeight', 'circlesToDraw' ) as $property ) { |
33 | | - if ( isset( $options[$property] ) ) { |
34 | | - $this->$property = $options[$property]; |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - if ( !isset( $this->dictionaryFile ) ) { |
39 | | - foreach ( array( '/usr/share/dict/words', '/usr/dict/words' ) as $dictionaryFile ) { |
40 | | - if ( is_file( $dictionaryFile ) and is_readable( $dictionaryFile ) ) { |
41 | | - $this->dictionaryFile = $dictionaryFile; |
42 | | - break; |
43 | | - } |
44 | | - } |
45 | | - } |
46 | | - if ( !isset( $this->dictionaryFile ) ) { |
47 | | - die( "dictionary file not found or not specified properly" ); |
48 | | - } |
49 | | - if ( !is_file( $this->dictionaryFile ) or !file_exists( $this->dictionaryFile ) ) { |
50 | | - die( "can't read dictionary file, or it doesn't exist." ); |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Writes random images with random filenames to disk in current working directory |
56 | | - * |
57 | | - * @param {Integer} number of filenames to write |
58 | | - * @param {String} format understood by ImageMagick, such as 'jpg' or 'gif' |
59 | | - * @return {Array} filenames we just wrote |
60 | | - */ |
61 | | - function writeImages( $number, $format ) { |
62 | | - $filenames = $this->getRandomFilenames( $number, $format ); |
63 | | - foreach( $filenames as $filename ) { |
64 | | - $image = $this->getImage(); |
65 | | - $image->setImageFormat( $format ); |
66 | | - $image->writeImage( $filename ); |
67 | | - } |
68 | | - return $filenames; |
69 | | - } |
70 | | - |
71 | | - |
72 | | - /** |
73 | | - * Return a number of randomly-generated filenames |
74 | | - * Each filename uses two words randomly drawn from the dictionary, like foo_bar.jpg |
75 | | - * |
76 | | - * @param {Integer} number of filenames to generate |
77 | | - * @param {String} extension, if desired |
78 | | - * @return {Array} of filenames |
79 | | - */ |
80 | | - private function getRandomFilenames( $number, $extension=null ) { |
81 | | - $filenames = array(); |
82 | | - |
83 | | - foreach( $this->getRandomWordPairs( $number ) as $pair ) { |
84 | | - $filename = $pair[0] . '_' . $pair[1]; |
85 | | - if ( !is_null( $extension ) ) { |
86 | | - $filename .= '.' . $extension; |
87 | | - } |
88 | | - $filename = preg_replace( '/\s+/', '', $filename ); |
89 | | - $filenames[] = $filename; |
90 | | - } |
91 | | - |
92 | | - return $filenames; |
93 | | - |
94 | | - } |
95 | | - |
96 | | - |
97 | | - /** |
98 | | - * Generate an image consisting of randomly colored and sized circles |
99 | | - * @return {Image} |
100 | | - */ |
101 | | - public function getImage() { |
102 | | - |
103 | | - $imageWidth = mt_rand( $this->minWidth, $this->maxWidth ); |
104 | | - $imageHeight = mt_rand( $this->minHeight, $this->maxHeight ); |
105 | | - |
106 | | - $image = new Imagick(); |
107 | | - $image->newImage( $imageWidth, $imageHeight, new ImagickPixel( $this->getRandomColor() ) ); |
108 | | - |
109 | | - $diagonalLength = sqrt( pow( $imageWidth, 2 ) + pow( $imageHeight, 2 ) ); |
110 | | - |
111 | | - for ( $i = 0; $i <= $this->circlesToDraw; $i++ ) { |
112 | | - $radius = mt_rand( 0, $diagonalLength / 4 ); |
113 | | - $originX = mt_rand( -1 * $radius, $imageWidth + $radius ); |
114 | | - $originY = mt_rand( -1 * $radius, $imageHeight + $radius ); |
115 | | - $perimeterX = $originX + $radius; |
116 | | - $perimeterY = $originY + $radius; |
117 | | - |
118 | | - $draw = new ImagickDraw(); |
119 | | - $draw->setFillColor( $this->getRandomColor() ); |
120 | | - $draw->circle( $originX, $originY, $perimeterX, $perimeterY ); |
121 | | - $image->drawImage( $draw ); |
122 | | - |
123 | | - } |
124 | | - |
125 | | - return $image; |
126 | | - } |
127 | | - |
128 | | - |
129 | | - |
130 | | - /** |
131 | | - * Generate a string of random colors for ImageMagick, like "rgb(12, 37, 98)" |
132 | | - * |
133 | | - * @return {String} |
134 | | - */ |
135 | | - public function getRandomColor() { |
136 | | - $components = array(); |
137 | | - for ($i = 0; $i <= 2; $i++ ) { |
138 | | - $components[] = mt_rand( 0, 255 ); |
139 | | - } |
140 | | - return 'rgb(' . join(', ', $components) . ')'; |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * Get an array of random pairs of random words, like array( array( 'foo', 'bar' ), array( 'quux', 'baz' ) ); |
145 | | - * |
146 | | - * @param {Integer} number of pairs |
147 | | - * @return {Array} of two-element arrays |
148 | | - */ |
149 | | - private function getRandomWordPairs( $number ) { |
150 | | - $lines = $this->getRandomLines( $number * 2 ); |
151 | | - // construct pairs of words |
152 | | - $pairs = array(); |
153 | | - $count = count( $lines ); |
154 | | - for( $i = 0; $i < $count; $i += 2 ) { |
155 | | - $pairs[] = array( $lines[$i], $lines[$i+1] ); |
156 | | - } |
157 | | - return $pairs; |
158 | | - } |
159 | | - |
160 | | - |
161 | | - /** |
162 | | - * Return N random lines from a file |
163 | | - * |
164 | | - * Will die if the file could not be read or if it had fewer lines than requested. |
165 | | - * |
166 | | - * @param {Integer} number of lines desired |
167 | | - * @string {String} path to file |
168 | | - * @return {Array} of exactly n elements, drawn randomly from lines the file |
169 | | - */ |
170 | | - private function getRandomLines( $number_desired ) { |
171 | | - $filepath = $this->dictionaryFile; |
172 | | - |
173 | | - // initialize array of lines |
174 | | - $lines = array(); |
175 | | - for ( $i = 0; $i < $number_desired; $i++ ) { |
176 | | - $lines[] = null; |
177 | | - } |
178 | | - |
179 | | - /* |
180 | | - * This algorithm obtains N random lines from a file in one single pass. It does this by replacing elements of |
181 | | - * a fixed-size array of lines, less and less frequently as it reads the file. |
182 | | - */ |
183 | | - $fh = fopen( $filepath, "r" ) or die( "couldn't open $filepath" ) ; |
184 | | - $line_number = 0; |
185 | | - $max_index = $number_desired - 1; |
186 | | - while( !feof( $fh ) ) { |
187 | | - $line = fgets( $fh ); |
188 | | - if ( $line !== false ) { |
189 | | - $line_number++; |
190 | | - $line = trim( $line ); |
191 | | - if ( mt_rand( 0, $line_number ) <= $max_index ) { |
192 | | - $lines[ mt_rand( 0, $max_index ) ] = $line; |
193 | | - } |
194 | | - } |
195 | | - } |
196 | | - fclose( $fh ); |
197 | | - if ( $line_number < $number_desired ) { |
198 | | - die( "not enough lines in $filepath" ); |
199 | | - } |
200 | | - |
201 | | - return $lines; |
202 | | - } |
203 | | - |
204 | | -} |
Index: branches/uploadwizard/extensions/UploadWizard/test/php/scripts/generateRandomImages.php |
— | — | @@ -1,25 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -require("RandomImageGenerator.php"); |
5 | | - |
6 | | -$getOptSpec = array( |
7 | | - 'dictionaryFile::', |
8 | | - 'minWidth::', |
9 | | - 'maxWidth::', |
10 | | - 'minHeight::', |
11 | | - 'maxHeight::', |
12 | | - 'circlesToDraw::', |
13 | | - |
14 | | - 'number::', |
15 | | - 'format::' |
16 | | -); |
17 | | -$options = getopt( null, $getOptSpec ); |
18 | | - |
19 | | -$format = isset( $options['format'] ) ? $options['format'] : 'jpg'; |
20 | | -unset( $options['format'] ); |
21 | | - |
22 | | -$number = isset( $options['number'] ) ? int( $options['number'] ) : 10; |
23 | | -unset( $options['number'] ); |
24 | | - |
25 | | -$randomImageGenerator = new RandomImageGenerator( $options ); |
26 | | -$randomImageGenerator->writeImages( $number, $format ); |