70 foreach (
array(
'dictionaryFile',
'minWidth',
'minHeight',
'maxWidth',
'maxHeight',
'shapesToDraw' )
as $property ) {
77 if ( !isset( $this->dictionaryFile ) ) {
80 '/usr/share/dict/words',
82 __DIR__ .
'/words.txt'
91 if ( !isset( $this->dictionaryFile ) ) {
92 throw new Exception(
"RandomImageGenerator: dictionary file not found or not specified properly" );
107 foreach ( $filenames
as $filename ) {
108 $this->{$imageWriteMethod}( $this->
getImageSpec(), $format, $filename );
123 global $wgUseImageMagick, $wgImageMagickConvertCommand;
124 if ( $format ===
'svg' ) {
129 if ( class_exists(
'Imagick' ) && $wgExiv2Command && is_executable( $wgExiv2Command ) ) {
130 return 'writeImageWithApi';
131 } elseif ( $wgUseImageMagick && $wgImageMagickConvertCommand && is_executable( $wgImageMagickConvertCommand ) ) {
132 return 'writeImageWithCommandLine';
135 throw new Exception(
"RandomImageGenerator: could not find a suitable method to write images in '$format' format" );
148 if ( is_null(
$dir ) ) {
151 $filenames =
array();
153 $basename = $pair[0] .
'_' . $pair[1];
154 if ( !is_null( $extension ) ) {
155 $basename .=
'.' . $extension;
157 $basename = preg_replace(
'/\s+/',
'', $basename );
158 $filenames[] =
"$dir/$basename";
173 $spec[
'width'] = mt_rand( $this->minWidth, $this->maxWidth );
174 $spec[
'height'] = mt_rand( $this->minHeight, $this->maxHeight );
177 $diagonalLength = sqrt( pow( $spec[
'width'], 2 ) + pow( $spec[
'height'], 2 ) );
181 $radius = mt_rand( 0, $diagonalLength / 4 );
182 if ( $radius == 0 ) {
185 $originX = mt_rand( -1 * $radius, $spec[
'width'] + $radius );
186 $originY = mt_rand( -1 * $radius, $spec[
'height'] + $radius );
187 $angle = mt_rand( 0, ( 3.141592 / 2 ) * $radius ) / $radius;
188 $legDeltaX = round( $radius * sin( $angle ) );
189 $legDeltaY = round( $radius * cos( $angle ) );
193 $draw[
'shape'] =
array(
194 array(
'x' => $originX,
'y' => $originY - $radius ),
195 array(
'x' => $originX + $legDeltaX,
'y' => $originY + $legDeltaY ),
196 array(
'x' => $originX - $legDeltaX,
'y' => $originY + $legDeltaY ),
197 array(
'x' => $originX,
'y' => $originY - $radius )
202 $spec[
'draws'] = $draws;
216 foreach ( $shape
as $point ) {
217 $points[] = $point[
'x'] .
',' . $point[
'y'];
233 public function writeSvg( $spec, $format, $filename ) {
234 $svg =
new SimpleXmlElement(
'<svg/>' );
235 $svg->addAttribute(
'xmlns',
'http://www.w3.org/2000/svg' );
236 $svg->addAttribute(
'version',
'1.1' );
237 $svg->addAttribute(
'width', $spec[
'width'] );
238 $svg->addAttribute(
'height', $spec[
'height'] );
239 $g = $svg->addChild(
'g' );
240 foreach ( $spec[
'draws']
as $drawSpec ) {
241 $shape = $g->addChild(
'polygon' );
242 $shape->addAttribute(
'fill', $drawSpec[
'fill'] );
243 $shape->addAttribute(
'points', self::shapePointsToString( $drawSpec[
'shape'] ) );
246 if ( !$fh = fopen( $filename,
'w' ) ) {
247 throw new Exception(
"couldn't open $filename for writing" );
249 fwrite( $fh, $svg->asXML() );
250 if ( !fclose( $fh ) ) {
251 throw new Exception(
"couldn't close $filename" );
265 $image =
new Imagick();
270 $orientation = self::$orientations[0];
271 if ( $format ==
'jpg' ) {
272 $orientation = self::$orientations[array_rand( self::$orientations )];
276 $image->newImage( $spec[
'width'], $spec[
'height'],
new ImagickPixel( $spec[
'fill'] ) );
278 foreach ( $spec[
'draws']
as $drawSpec ) {
279 $draw =
new ImagickDraw();
280 $draw->setFillColor( $drawSpec[
'fill'] );
281 $draw->polygon( $drawSpec[
'shape'] );
282 $image->drawImage( $draw );
285 $image->setImageFormat( $format );
290 $image->writeImage( $filename );
294 if ( $wgExiv2Command ) {
297 .
wfEscapeShellArg(
"set Exif.Image.Orientation " . $orientation[
'exifCode'] )
304 print
"Error with $cmd: $retval, $err\n";
321 if ( $dims[
'x'] < 0 ) {
322 $correctionX = abs( $dims[
'x'] );
324 if ( $dims[
'y'] < 0 ) {
325 $correctionY = abs( $dims[
'y'] );
327 $tSpec[
'width'] = abs( $dims[
'x'] );
328 $tSpec[
'height'] = abs( $dims[
'y'] );
329 $tSpec[
'fill'] = $spec[
'fill'];
330 $tSpec[
'draws'] =
array();
331 foreach ( $spec[
'draws']
as $draw ) {
333 'fill' => $draw[
'fill'],
336 foreach ( $draw[
'shape']
as $point ) {
338 $tPoint[
'x'] += $correctionX;
339 $tPoint[
'y'] += $correctionY;
340 $tDraw[
'shape'][] = $tPoint;
342 $tSpec[
'draws'][] = $tDraw;
357 'x' => $x * $matrix[0][0] + $y * $matrix[0][1],
358 'y' => $x * $matrix[1][0] + $y * $matrix[1][1]
379 global $wgImageMagickConvertCommand;
383 foreach ( $spec[
'draws']
as $draw ) {
384 $fill = $draw[
'fill'];
386 $drawCommand =
"fill $fill polygon $polygon";
404 $components =
array();
405 for ( $i = 0; $i <= 2; $i++ ) {
406 $components[] = mt_rand( 0, 255 );
409 return 'rgb(' . join(
', ', $components ) .
')';
423 for ( $i = 0; $i <
$count; $i += 2 ) {
445 for ( $i = 0; $i < $number_desired; $i++ ) {
453 $fh = fopen( $filepath,
"r" );
455 throw new Exception(
"couldn't open $filepath" );
458 $max_index = $number_desired - 1;
459 while ( !feof( $fh ) ) {
460 $line = fgets( $fh );
461 if (
$line !==
false ) {
464 if ( mt_rand( 0, $line_number ) <= $max_index ) {
470 if ( $line_number < $number_desired ) {
471 throw new Exception(
"not enough lines in $filepath" );