http://www.mediawiki.org/wiki/Special:Code/MediaWiki/74111
Revision: 74111
Author: neilk
Date: 2010-10-01 21:39:46 +0000 (Fri, 01 Oct 2010)
Log Message:
-----------
split random image generation in library and script
Modified Paths:
--------------
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/RandomImageGenerator.php
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/generateRandomImages.php
Modified:
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/RandomImageGenerator.php
===================================================================
---
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/RandomImageGenerator.php
2010-10-01 21:27:21 UTC (rev 74110)
+++
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/RandomImageGenerator.php
2010-10-01 21:39:46 UTC (rev 74111)
@@ -1,7 +1,7 @@
<?php
/*
- * generateRandomImages -- does what it says on the tin.
+ * RandomImageGenerator -- does what it says on the tin.
*
* Because MediaWiki tests the uniqueness of media upload content, and
filenames, it is sometimes useful to generate
* files that are guaranteed (or at least very likely) to be unique in both
those ways.
@@ -13,169 +13,191 @@
* @author Neil Kandalgaonkar <[email protected]>
*/
-$defaults = array(
- 'dict' => "/usr/share/dict/words",
- 'number' => 10,
- 'minWidth' => 400,
- 'maxWidth' => 800,
- 'minHeight' => 400,
- 'maxHeight' => 800,
- 'format' => 'jpg'
-);
-writeRandomImages( getOptions( $defaults ) );
+/**
+ * Does what it says on the tin.
+ * Can fetch a random image, or also write a number of them to disk with
random filenames.
+ */
+class RandomImageGenerator {
-
-
-
-/**
- * Override defaults with command-line options
- *
- * @param {Array} key-value default values
- * @return {Array} defaults with CLI overrides
- */
-function getOptions( $defaults ) {
+ private $dictionaryFile;
+ private $minWidth = 400;
+ private $maxWidth = 800;
+ private $minHeight = 400;
+ private $maxHeight = 800;
+ private $circlesToDraw = 5;
- // all options are optional, so append '::' to spec
- $getoptSpec = array_map( function($s) { return $s . "::"; },
array_keys( $defaults ) );
- $cliOptions = getopt( null, $getoptSpec );
+ public function __construct( $options ) {
+ foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight',
'maxHeight', 'circlesToDraw' ) as $property ) {
+ if ( isset( $options[$property] ) ) {
+ $this->$property = $options[$property];
+ }
+ }
+
+ if ( !isset( $this->dictionaryFile ) ) {
+ foreach ( array( '/usr/share/dict/words',
'/usr/dict/words' ) as $dictionaryFile ) {
+ if ( is_file( $dictionaryFile ) and
is_readable( $dictionaryFile ) ) {
+ $this->dictionaryFile = $dictionaryFile;
+ break;
+ }
+ }
+ }
+ if ( !isset( $this->dictionaryFile ) ) {
+ die( "dictionary file not found or not specified
properly" );
+ }
+ if ( !is_file( $this->dictionaryFile ) or !file_exists(
$this->dictionaryFile ) ) {
+ die( "can't read dictionary file, or it doesn't exist."
);
+ }
+ }
- $options = array();
- foreach ( $defaults as $key => $value ) {
- $options[$key] = array_key_exists( $key, $cliOptions ) ?
$cliOptions[$key] : $defaults[$key];
+ /**
+ * Writes random images with random filenames to disk in current
working directory
+ *
+ * @param {Integer} number of filenames to write
+ * @param {String} format understood by ImageMagick, such as 'jpg' or
'gif'
+ * @return {Array} filenames we just wrote
+ */
+ function writeImages( $number, $format ) {
+ $filenames = $this->getRandomFilenames( $number, $format );
+ foreach( $filenames as $filename ) {
+ $image = $this->getImage();
+ $image->setImageFormat( $format );
+ $image->writeImage( $filename );
+ }
+ return $filenames;
}
- return $options;
-}
+ /**
+ * Return a number of randomly-generated filenames
+ * Each filename uses two words randomly drawn from the dictionary,
like foo_bar.jpg
+ *
+ * @param {Integer} number of filenames to generate
+ * @param {String} extension, if desired
+ * @return {Array} of filenames
+ */
+ private function getRandomFilenames( $number, $extension=null ) {
+ $filenames = array();
-
-/**
- * writes random images with random files to disk in current working directory
- *
- * @param {Array} key-value options
- */
-function writeRandomImages( $options ) {
- global $dictionary;
-
- // each filename uses two words from the dictionary
- $wordsDesired = $options['number'] * 2;
-
- foreach( getPairs( getRandomLines( $wordsDesired, $options['dict'] ) )
as $pair ) {
- $filename = $pair[0] . '_' . $pair[1] . '.' .
$options['format'];
+ foreach( $this->getRandomWordPairs( $number ) as $pair ) {
+ $filename = $pair[0] . '_' . $pair[1];
+ if ( !is_null( $extension ) ) {
+ $filename .= '.' . $extension;
+ }
+ $filename = preg_replace( '/\s+/', '', $filename );
+ $filenames[] = $filename;
+ }
+
+ return $filenames;
- // strip all whitespace (in case we somehow have inner
whitespace)
- $filename = preg_replace( '/\s+/', '', $filename );
-
- $image = getRandomImage( $options['minWidth'],
$options['maxWidth'], $options['minHeight'], $options['maxHeight'] );
- $image->setImageFormat( $options['format'] );
- $image->writeImage( $filename );
}
-}
-/**
- * Generate an image consisting of randomly colored and sized circles
- * @return {Image}
- */
-function getRandomImage($minWidth, $maxWidth, $minHeight, $maxHeight) {
- global $options;
+ /**
+ * Generate an image consisting of randomly colored and sized circles
+ * @return {Image}
+ */
+ public function getImage() {
- $imageWidth = mt_rand( $minWidth, $maxWidth );
- $imageHeight = mt_rand( $minHeight, $maxHeight );
+ $imageWidth = mt_rand( $this->minWidth, $this->maxWidth );
+ $imageHeight = mt_rand( $this->minHeight, $this->maxHeight );
- $image = new Imagick();
- $image->newImage( $imageWidth, $imageHeight, new ImagickPixel(
getRandomColor() ) );
+ $image = new Imagick();
+ $image->newImage( $imageWidth, $imageHeight, new ImagickPixel(
$this->getRandomColor() ) );
+ $diagonalLength = sqrt( pow( $imageWidth, 2 ) + pow(
$imageHeight, 2 ) );
- $diagonalLength = sqrt( pow( $imageWidth, 2 ) + pow( $imageHeight, 2 )
);
+ for ( $i = 0; $i <= $this->circlesToDraw; $i++ ) {
+ $radius = mt_rand( 0, $diagonalLength / 4 );
+ $originX = mt_rand( -1 * $radius, $imageWidth + $radius
);
+ $originY = mt_rand( -1 * $radius, $imageHeight +
$radius );
+ $perimeterX = $originX + $radius;
+ $perimeterY = $originY + $radius;
- for ( $i = 0; $i <= 5; $i++ ) {
- $radius = mt_rand( 0, $diagonalLength / 4 );
- $originX = mt_rand( -1 * $radius, $imageWidth + $radius );
- $originY = mt_rand( -1 * $radius, $imageHeight + $radius );
- $perimeterX = $originX + $radius;
- $perimeterY = $originY + $radius;
+ $draw = new ImagickDraw();
+ $draw->setFillColor( $this->getRandomColor() );
+ $draw->circle( $originX, $originY, $perimeterX,
$perimeterY );
+ $image->drawImage( $draw );
+
+ }
- $draw = new ImagickDraw();
- $draw->setFillColor( getRandomColor() );
- $draw->circle( $originX, $originY, $perimeterX, $perimeterY );
- $image->drawImage( $draw );
-
+ return $image;
}
- return $image;
-}
+
-
-
-/**
- * Generate a string of random colors for ImageMagick, like "rgb(12, 37, 98)"
- *
- * @return {String}
- */
-function getRandomColor() {
- $components = array();
- for ($i = 0; $i <= 2; $i++ ) {
- $components[] = mt_rand( 0, 255 );
+ /**
+ * Generate a string of random colors for ImageMagick, like "rgb(12,
37, 98)"
+ *
+ * @return {String}
+ */
+ public function getRandomColor() {
+ $components = array();
+ for ($i = 0; $i <= 2; $i++ ) {
+ $components[] = mt_rand( 0, 255 );
+ }
+ return 'rgb(' . join(', ', $components) . ')';
}
- return 'rgb(' . join(', ', $components) . ')';
-}
-/**
- * Turn an array into an array of pairs.
- *
- * @param {Array} an array
- * @return {Array} of two-element arrays
- */
-function getPairs( $arr ) {
- // construct pairs of words
- $pairs = array();
- $count = count( $arr );
- for( $i = 0; $i < $count; $i += 2 ) {
- $pairs[] = array( $arr[$i], $arr[$i+1] );
+ /**
+ * Get an array of random pairs of random words, like array( array(
'foo', 'bar' ), array( 'quux', 'baz' ) );
+ *
+ * @param {Integer} number of pairs
+ * @return {Array} of two-element arrays
+ */
+ private function getRandomWordPairs( $number ) {
+ $lines = $this->getRandomLines( $number * 2 );
+ // construct pairs of words
+ $pairs = array();
+ $count = count( $lines );
+ for( $i = 0; $i < $count; $i += 2 ) {
+ $pairs[] = array( $lines[$i], $lines[$i+1] );
+ }
+ return $pairs;
}
- return $pairs;
-}
-/**
- * Return N random lines from a file
- *
- * Will die if the file could not be read or if it had fewer lines than
requested.
- *
- * @param {Integer} number of lines desired
- * @string {String} path to file
- * @return {Array} of exactly n elements, drawn randomly from lines the file
- */
-function getRandomLines( $number_desired, $filepath ) {
- $lines = array();
- for ( $i = 0; $i < $number_desired; $i++ ) {
- $lines[] = null;
- }
+
+ /**
+ * Return N random lines from a file
+ *
+ * Will die if the file could not be read or if it had fewer lines than
requested.
+ *
+ * @param {Integer} number of lines desired
+ * @string {String} path to file
+ * @return {Array} of exactly n elements, drawn randomly from lines the
file
+ */
+ private function getRandomLines( $number_desired ) {
+ $filepath = $this->dictionaryFile;
- /*
- * This algorithm obtains N random lines from a file in one single
pass. It does this by replacing elements of
- * a fixed-size array of lines, less and less frequently as it reads
the file.
- */
- $fh = fopen( $filepath, "r" ) or die( "couldn't open $filepath" ) ;
- $line_number = 0;
- $max_index = $number_desired - 1;
- while( !feof( $fh ) ) {
- $line = fgets( $fh );
- if ( $line !== false ) {
- $line_number++;
- $line = trim( $line );
- if ( mt_rand( 0, $line_number ) <= $max_index ) {
- $lines[ mt_rand( 0, $max_index ) ] = $line;
+ // initialize array of lines
+ $lines = array();
+ for ( $i = 0; $i < $number_desired; $i++ ) {
+ $lines[] = null;
+ }
+
+ /*
+ * This algorithm obtains N random lines from a file in one
single pass. It does this by replacing elements of
+ * a fixed-size array of lines, less and less frequently as it
reads the file.
+ */
+ $fh = fopen( $filepath, "r" ) or die( "couldn't open $filepath"
) ;
+ $line_number = 0;
+ $max_index = $number_desired - 1;
+ while( !feof( $fh ) ) {
+ $line = fgets( $fh );
+ if ( $line !== false ) {
+ $line_number++;
+ $line = trim( $line );
+ if ( mt_rand( 0, $line_number ) <= $max_index )
{
+ $lines[ mt_rand( 0, $max_index ) ] =
$line;
+ }
}
}
+ fclose( $fh );
+ if ( $line_number < $number_desired ) {
+ die( "not enough lines in $filepath" );
+ }
+
+ return $lines;
}
- fclose( $fh );
- if ( $line_number < $number_desired ) {
- die( "not enough lines in $filepath" );
- }
-
- return $lines;
+
}
-
-?>
Modified:
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/generateRandomImages.php
===================================================================
---
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/generateRandomImages.php
2010-10-01 21:27:21 UTC (rev 74110)
+++
branches/uploadwizard/extensions/UploadWizard/test/php/scripts/generateRandomImages.php
2010-10-01 21:39:46 UTC (rev 74111)
@@ -1,181 +1,25 @@
<?php
-/*
- * generateRandomImages -- does what it says on the tin.
- *
- * Because MediaWiki tests the uniqueness of media upload content, and
filenames, it is sometimes useful to generate
- * files that are guaranteed (or at least very likely) to be unique in both
those ways.
- * This generates a number of filenames with random names and random content
(colored circles)
- *
- * Requires Imagick, the ImageMagick library for PHP.
- *
- * @file
- * @author Neil Kandalgaonkar <[email protected]>
- */
+require("RandomImageGenerator.php");
-$defaults = array(
- 'dict' => "/usr/share/dict/words",
- 'number' => 10,
- 'minWidth' => 400,
- 'maxWidth' => 800,
- 'minHeight' => 400,
- 'maxHeight' => 800,
- 'format' => 'jpg'
+$getOptSpec = array(
+ 'dictionaryFile::',
+ 'minWidth::',
+ 'maxWidth::',
+ 'minHeight::',
+ 'maxHeight::',
+ 'circlesToDraw::',
+
+ 'number::',
+ 'format::'
);
+$options = getopt( null, $getOptSpec );
-writeRandomImages( getOptions( $defaults ) );
+$format = isset( $options['format'] ) ? $options['format'] : 'jpg';
+unset( $options['format'] );
+$number = isset( $options['number'] ) ? int( $options['number'] ) : 10;
+unset( $options['number'] );
-
-
-/**
- * Override defaults with command-line options
- *
- * @param {Array} key-value default values
- * @return {Array} defaults with CLI overrides
- */
-function getOptions( $defaults ) {
-
- // all options are optional, so append '::' to spec
- $getoptSpec = array_map( function($s) { return $s . "::"; },
array_keys( $defaults ) );
- $cliOptions = getopt( null, $getoptSpec );
-
- $options = array();
- foreach ( $defaults as $key => $value ) {
- $options[$key] = array_key_exists( $key, $cliOptions ) ?
$cliOptions[$key] : $defaults[$key];
- }
-
- return $options;
-}
-
-
-
-/**
- * writes random images with random files to disk in current working directory
- *
- * @param {Array} key-value options
- */
-function writeRandomImages( $options ) {
- global $dictionary;
-
- // each filename uses two words from the dictionary
- $wordsDesired = $options['number'] * 2;
-
- foreach( getPairs( getRandomLines( $wordsDesired, $options['dict'] ) )
as $pair ) {
- $filename = $pair[0] . '_' . $pair[1] . '.' .
$options['format'];
-
- // strip all whitespace (in case we somehow have inner
whitespace)
- $filename = preg_replace( '/\s+/', '', $filename );
-
- $image = getRandomImage( $options['minWidth'],
$options['maxWidth'], $options['minHeight'], $options['maxHeight'] );
- $image->setImageFormat( $options['format'] );
- $image->writeImage( $filename );
- }
-}
-
-
-/**
- * Generate an image consisting of randomly colored and sized circles
- * @return {Image}
- */
-function getRandomImage($minWidth, $maxWidth, $minHeight, $maxHeight) {
- global $options;
-
- $imageWidth = mt_rand( $minWidth, $maxWidth );
- $imageHeight = mt_rand( $minHeight, $maxHeight );
-
- $image = new Imagick();
- $image->newImage( $imageWidth, $imageHeight, new ImagickPixel(
getRandomColor() ) );
-
-
- $diagonalLength = sqrt( pow( $imageWidth, 2 ) + pow( $imageHeight, 2 )
);
-
- for ( $i = 0; $i <= 5; $i++ ) {
- $radius = mt_rand( 0, $diagonalLength / 4 );
- $originX = mt_rand( -1 * $radius, $imageWidth + $radius );
- $originY = mt_rand( -1 * $radius, $imageHeight + $radius );
- $perimeterX = $originX + $radius;
- $perimeterY = $originY + $radius;
-
- $draw = new ImagickDraw();
- $draw->setFillColor( getRandomColor() );
- $draw->circle( $originX, $originY, $perimeterX, $perimeterY );
- $image->drawImage( $draw );
-
- }
-
- return $image;
-}
-
-
-
-/**
- * Generate a string of random colors for ImageMagick, like "rgb(12, 37, 98)"
- *
- * @return {String}
- */
-function getRandomColor() {
- $components = array();
- for ($i = 0; $i <= 2; $i++ ) {
- $components[] = mt_rand( 0, 255 );
- }
- return 'rgb(' . join(', ', $components) . ')';
-}
-
-/**
- * Turn an array into an array of pairs.
- *
- * @param {Array} an array
- * @return {Array} of two-element arrays
- */
-function getPairs( $arr ) {
- // construct pairs of words
- $pairs = array();
- $count = count( $arr );
- for( $i = 0; $i < $count; $i += 2 ) {
- $pairs[] = array( $arr[$i], $arr[$i+1] );
- }
- return $pairs;
-}
-
-/**
- * Return N random lines from a file
- *
- * Will die if the file could not be read or if it had fewer lines than
requested.
- *
- * @param {Integer} number of lines desired
- * @string {String} path to file
- * @return {Array} of exactly n elements, drawn randomly from lines the file
- */
-function getRandomLines( $number_desired, $filepath ) {
- $lines = array();
- for ( $i = 0; $i < $number_desired; $i++ ) {
- $lines[] = null;
- }
-
- /*
- * This algorithm obtains N random lines from a file in one single
pass. It does this by replacing elements of
- * a fixed-size array of lines, less and less frequently as it reads
the file.
- */
- $fh = fopen( $filepath, "r" ) or die( "couldn't open $filepath" ) ;
- $line_number = 0;
- $max_index = $number_desired - 1;
- while( !feof( $fh ) ) {
- $line = fgets( $fh );
- if ( $line !== false ) {
- $line_number++;
- $line = trim( $line );
- if ( mt_rand( 0, $line_number ) <= $max_index ) {
- $lines[ mt_rand( 0, $max_index ) ] = $line;
- }
- }
- }
- fclose( $fh );
- if ( $line_number < $number_desired ) {
- die( "not enough lines in $filepath" );
- }
-
- return $lines;
-}
-
-?>
+$randomImageGenerator = new RandomImageGenerator( $options );
+$randomImageGenerator->writeImages( $number, $format );
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs