2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2011-10-11 18:30:50 +00:00
|
|
|
/**
|
2010-12-14 16:26:35 +00:00
|
|
|
* RandomImageGenerator -- does what it says on the tin.
|
|
|
|
|
* Requires Imagick, the ImageMagick library for PHP, or the command line equivalent (usually 'convert').
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
2011-06-21 03:40:53 +00:00
|
|
|
* This generates a number of filenames with random names and random content (colored triangles)
|
2010-12-14 16:26:35 +00:00
|
|
|
*
|
|
|
|
|
* It is also useful to have fresh content because our tests currently run in a "destructive" mode, and don't create a fresh new wiki for each
|
|
|
|
|
* test run.
|
|
|
|
|
* Consequently, if we just had a few static files we kept re-uploading, we'd get lots of warnings about matching content or filenames,
|
|
|
|
|
* and even if we deleted those files, we'd get warnings about archived files.
|
|
|
|
|
*
|
|
|
|
|
* This can also be used with a cronjob to generate random files all the time -- I use it to have a constant, never ending supply when I'm
|
|
|
|
|
* testing interactively.
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Neil Kandalgaonkar <neilk@wikimedia.org>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RandomImageGenerator: 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 {
|
|
|
|
|
|
|
|
|
|
private $dictionaryFile;
|
2013-02-14 13:10:38 +00:00
|
|
|
private $minWidth = 400;
|
|
|
|
|
private $maxWidth = 800;
|
|
|
|
|
private $minHeight = 400;
|
|
|
|
|
private $maxHeight = 800;
|
|
|
|
|
private $shapesToDraw = 5;
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-06-21 03:40:53 +00:00
|
|
|
/**
|
2013-05-10 11:51:06 +00:00
|
|
|
* Orientations: 0th row, 0th column, Exif orientation code, rotation 2x2 matrix that is opposite of orientation
|
2011-06-21 03:40:53 +00:00
|
|
|
* n.b. we do not handle the 'flipped' orientations, which is why there is no entry for 2, 4, 5, or 7. Those
|
|
|
|
|
* seem to be rare in real images anyway
|
|
|
|
|
* (we also would need a non-symmetric shape for the images to test those, like a letter F)
|
|
|
|
|
*/
|
|
|
|
|
private static $orientations = array(
|
2011-12-08 13:02:34 +00:00
|
|
|
array(
|
|
|
|
|
'0thRow' => 'top',
|
|
|
|
|
'0thCol' => 'left',
|
|
|
|
|
'exifCode' => 1,
|
|
|
|
|
'counterRotation' => array( array( 1, 0 ), array( 0, 1 ) )
|
2011-06-21 03:40:53 +00:00
|
|
|
),
|
2011-12-08 13:02:34 +00:00
|
|
|
array(
|
2011-06-21 03:40:53 +00:00
|
|
|
'0thRow' => 'bottom',
|
2011-12-08 13:02:34 +00:00
|
|
|
'0thCol' => 'right',
|
|
|
|
|
'exifCode' => 3,
|
|
|
|
|
'counterRotation' => array( array( -1, 0 ), array( 0, -1 ) )
|
2011-06-21 03:40:53 +00:00
|
|
|
),
|
2011-12-08 13:02:34 +00:00
|
|
|
array(
|
|
|
|
|
'0thRow' => 'right',
|
|
|
|
|
'0thCol' => 'top',
|
|
|
|
|
'exifCode' => 6,
|
|
|
|
|
'counterRotation' => array( array( 0, 1 ), array( 1, 0 ) )
|
2011-06-21 03:40:53 +00:00
|
|
|
),
|
2011-12-08 13:02:34 +00:00
|
|
|
array(
|
|
|
|
|
'0thRow' => 'left',
|
|
|
|
|
'0thCol' => 'bottom',
|
|
|
|
|
'exifCode' => 8,
|
|
|
|
|
'counterRotation' => array( array( 0, -1 ), array( -1, 0 ) )
|
2011-06-21 03:40:53 +00:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2011-04-07 14:54:38 +00:00
|
|
|
public function __construct( $options = array() ) {
|
2011-08-02 00:36:38 +00:00
|
|
|
foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight', 'shapesToDraw' ) as $property ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
if ( isset( $options[$property] ) ) {
|
|
|
|
|
$this->$property = $options[$property];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the dictionary file, to generate random names
|
|
|
|
|
if ( !isset( $this->dictionaryFile ) ) {
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach (
|
|
|
|
|
array(
|
2011-12-08 13:02:34 +00:00
|
|
|
'/usr/share/dict/words',
|
|
|
|
|
'/usr/dict/words',
|
2013-02-14 13:10:38 +00:00
|
|
|
__DIR__ . '/words.txt'
|
|
|
|
|
) as $dictionaryFile
|
|
|
|
|
) {
|
2010-12-14 16:26:35 +00:00
|
|
|
if ( is_file( $dictionaryFile ) and is_readable( $dictionaryFile ) ) {
|
|
|
|
|
$this->dictionaryFile = $dictionaryFile;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $this->dictionaryFile ) ) {
|
|
|
|
|
throw new Exception( "RandomImageGenerator: dictionary file not found or not specified properly" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes random images with random filenames to disk in the directory you specify, or current working directory
|
|
|
|
|
*
|
|
|
|
|
* @param $number Integer: number of filenames to write
|
|
|
|
|
* @param $format String: optional, must be understood by ImageMagick, such as 'jpg' or 'gif'
|
|
|
|
|
* @param $dir String: directory, optional (will default to current working directory)
|
|
|
|
|
* @return Array: filenames we just wrote
|
|
|
|
|
*/
|
|
|
|
|
function writeImages( $number, $format = 'jpg', $dir = null ) {
|
|
|
|
|
$filenames = $this->getRandomFilenames( $number, $format, $dir );
|
2011-04-07 23:32:36 +00:00
|
|
|
$imageWriteMethod = $this->getImageWriteMethod( $format );
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $filenames as $filename ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
$this->{$imageWriteMethod}( $this->getImageSpec(), $format, $filename );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
return $filenames;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-07 23:32:36 +00:00
|
|
|
/**
|
|
|
|
|
* Figure out how we write images. This is a factor of both format and the local system
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param string $format (a typical extension like 'svg', 'jpg', etc.)
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @return string
|
2011-04-07 23:32:36 +00:00
|
|
|
*/
|
|
|
|
|
function getImageWriteMethod( $format ) {
|
2011-04-10 22:27:52 +00:00
|
|
|
global $wgUseImageMagick, $wgImageMagickConvertCommand;
|
2011-12-08 13:02:34 +00:00
|
|
|
if ( $format === 'svg' ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
return 'writeSvg';
|
|
|
|
|
} else {
|
|
|
|
|
// figure out how to write images
|
2011-08-04 22:33:04 +00:00
|
|
|
global $wgExiv2Command;
|
|
|
|
|
if ( class_exists( 'Imagick' ) && $wgExiv2Command && is_executable( $wgExiv2Command ) ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
return 'writeImageWithApi';
|
|
|
|
|
} elseif ( $wgUseImageMagick && $wgImageMagickConvertCommand && is_executable( $wgImageMagickConvertCommand ) ) {
|
|
|
|
|
return 'writeImageWithCommandLine';
|
2011-12-08 13:02:34 +00:00
|
|
|
}
|
2011-04-07 23:32:36 +00:00
|
|
|
}
|
|
|
|
|
throw new Exception( "RandomImageGenerator: could not find a suitable method to write images in '$format' format" );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* Return a number of randomly-generated filenames
|
|
|
|
|
* Each filename uses two words randomly drawn from the dictionary, like elephantine_spatula.jpg
|
|
|
|
|
*
|
|
|
|
|
* @param $number Integer: of filenames to generate
|
|
|
|
|
* @param $extension String: optional, defaults to 'jpg'
|
|
|
|
|
* @param $dir String: optional, defaults to current working directory
|
|
|
|
|
* @return Array: of filenames
|
|
|
|
|
*/
|
|
|
|
|
private function getRandomFilenames( $number, $extension = 'jpg', $dir = null ) {
|
|
|
|
|
if ( is_null( $dir ) ) {
|
|
|
|
|
$dir = getcwd();
|
|
|
|
|
}
|
|
|
|
|
$filenames = array();
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $this->getRandomWordPairs( $number ) as $pair ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$basename = $pair[0] . '_' . $pair[1];
|
|
|
|
|
if ( !is_null( $extension ) ) {
|
|
|
|
|
$basename .= '.' . $extension;
|
|
|
|
|
}
|
|
|
|
|
$basename = preg_replace( '/\s+/', '', $basename );
|
|
|
|
|
$filenames[] = "$dir/$basename";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $filenames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate data representing an image of random size (within limits),
|
2011-06-17 23:26:45 +00:00
|
|
|
* consisting of randomly colored and sized upward pointing triangles against a random background color
|
2010-12-14 16:26:35 +00:00
|
|
|
* (This data is used in the writeImage* methods).
|
|
|
|
|
* @return {Mixed}
|
|
|
|
|
*/
|
|
|
|
|
public function getImageSpec() {
|
|
|
|
|
$spec = array();
|
|
|
|
|
|
|
|
|
|
$spec['width'] = mt_rand( $this->minWidth, $this->maxWidth );
|
|
|
|
|
$spec['height'] = mt_rand( $this->minHeight, $this->maxHeight );
|
|
|
|
|
$spec['fill'] = $this->getRandomColor();
|
|
|
|
|
|
|
|
|
|
$diagonalLength = sqrt( pow( $spec['width'], 2 ) + pow( $spec['height'], 2 ) );
|
|
|
|
|
|
|
|
|
|
$draws = array();
|
2011-06-21 03:40:53 +00:00
|
|
|
for ( $i = 0; $i <= $this->shapesToDraw; $i++ ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$radius = mt_rand( 0, $diagonalLength / 4 );
|
2011-06-21 03:40:53 +00:00
|
|
|
if ( $radius == 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
$originX = mt_rand( -1 * $radius, $spec['width'] + $radius );
|
|
|
|
|
$originY = mt_rand( -1 * $radius, $spec['height'] + $radius );
|
2013-02-14 13:10:38 +00:00
|
|
|
$angle = mt_rand( 0, ( 3.141592 / 2 ) * $radius ) / $radius;
|
2011-06-17 23:26:45 +00:00
|
|
|
$legDeltaX = round( $radius * sin( $angle ) );
|
|
|
|
|
$legDeltaY = round( $radius * cos( $angle ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$draw = array();
|
|
|
|
|
$draw['fill'] = $this->getRandomColor();
|
2011-06-17 23:26:45 +00:00
|
|
|
$draw['shape'] = array(
|
2013-02-14 13:10:38 +00:00
|
|
|
array( 'x' => $originX, 'y' => $originY - $radius ),
|
|
|
|
|
array( 'x' => $originX + $legDeltaX, 'y' => $originY + $legDeltaY ),
|
|
|
|
|
array( 'x' => $originX - $legDeltaX, 'y' => $originY + $legDeltaY ),
|
|
|
|
|
array( 'x' => $originX, 'y' => $originY - $radius )
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
$draws[] = $draw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$spec['draws'] = $draws;
|
|
|
|
|
|
|
|
|
|
return $spec;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-18 03:36:44 +00:00
|
|
|
/**
|
|
|
|
|
* Given array( array('x' => 10, 'y' => 20), array( 'x' => 30, y=> 5 ) )
|
|
|
|
|
* returns "10,20 30,5"
|
|
|
|
|
* Useful for SVG and imagemagick command line arguments
|
|
|
|
|
* @param $shape: Array of arrays, each array containing x & y keys mapped to numeric values
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
static function shapePointsToString( $shape ) {
|
|
|
|
|
$points = array();
|
2011-12-08 13:02:34 +00:00
|
|
|
foreach ( $shape as $point ) {
|
2011-06-18 03:36:44 +00:00
|
|
|
$points[] = $point['x'] . ',' . $point['y'];
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-06-18 03:36:44 +00:00
|
|
|
return join( " ", $points );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-04-07 23:32:36 +00:00
|
|
|
/**
|
|
|
|
|
* Based on image specification, write a very simple SVG file to disk.
|
|
|
|
|
* Ignores the background spec because transparency is cool. :)
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param array $spec spec describing background and shapes to draw
|
|
|
|
|
* @param string $format file format to write (which is obviously always svg here)
|
|
|
|
|
* @param string $filename filename to write to
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
2011-04-07 23:32:36 +00:00
|
|
|
*/
|
2011-12-08 13:02:34 +00:00
|
|
|
public function writeSvg( $spec, $format, $filename ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
$svg = new SimpleXmlElement( '<svg/>' );
|
|
|
|
|
$svg->addAttribute( 'xmlns', 'http://www.w3.org/2000/svg' );
|
2011-12-08 13:02:34 +00:00
|
|
|
$svg->addAttribute( 'version', '1.1' );
|
|
|
|
|
$svg->addAttribute( 'width', $spec['width'] );
|
|
|
|
|
$svg->addAttribute( 'height', $spec['height'] );
|
2011-04-07 23:32:36 +00:00
|
|
|
$g = $svg->addChild( 'g' );
|
|
|
|
|
foreach ( $spec['draws'] as $drawSpec ) {
|
2011-06-17 23:26:45 +00:00
|
|
|
$shape = $g->addChild( 'polygon' );
|
2011-12-08 13:02:34 +00:00
|
|
|
$shape->addAttribute( 'fill', $drawSpec['fill'] );
|
2011-06-18 03:36:44 +00:00
|
|
|
$shape->addAttribute( 'points', self::shapePointsToString( $drawSpec['shape'] ) );
|
2013-02-14 13:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$fh = fopen( $filename, 'w' ) ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
throw new Exception( "couldn't open $filename for writing" );
|
|
|
|
|
}
|
|
|
|
|
fwrite( $fh, $svg->asXML() );
|
2013-02-14 13:10:38 +00:00
|
|
|
if ( !fclose( $fh ) ) {
|
2011-04-07 23:32:36 +00:00
|
|
|
throw new Exception( "couldn't close $filename" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* Based on an image specification, write such an image to disk, using Imagick PHP extension
|
2013-11-15 14:32:12 +00:00
|
|
|
* @param array $spec spec describing background and circles to draw
|
|
|
|
|
* @param string $format file format to write
|
|
|
|
|
* @param string $filename filename to write to
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
|
|
|
|
public function writeImageWithApi( $spec, $format, $filename ) {
|
2011-12-08 13:02:34 +00:00
|
|
|
// this is a hack because I can't get setImageOrientation() to work. See below.
|
2011-06-21 03:40:53 +00:00
|
|
|
global $wgExiv2Command;
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$image = new Imagick();
|
2011-06-21 03:40:53 +00:00
|
|
|
/**
|
2011-12-08 13:02:34 +00:00
|
|
|
* If the format is 'jpg', will also add a random orientation -- the image will be drawn rotated with triangle points
|
2011-06-21 03:40:53 +00:00
|
|
|
* facing in some direction (0, 90, 180 or 270 degrees) and a countering rotation should turn the triangle points upward again
|
|
|
|
|
*/
|
|
|
|
|
$orientation = self::$orientations[0]; // default is normal orientation
|
|
|
|
|
if ( $format == 'jpg' ) {
|
2013-02-14 13:10:38 +00:00
|
|
|
$orientation = self::$orientations[array_rand( self::$orientations )];
|
2011-12-08 13:02:34 +00:00
|
|
|
$spec = self::rotateImageSpec( $spec, $orientation['counterRotation'] );
|
2011-06-21 03:40:53 +00:00
|
|
|
}
|
2011-12-08 13:02:34 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$image->newImage( $spec['width'], $spec['height'], new ImagickPixel( $spec['fill'] ) );
|
|
|
|
|
|
|
|
|
|
foreach ( $spec['draws'] as $drawSpec ) {
|
|
|
|
|
$draw = new ImagickDraw();
|
|
|
|
|
$draw->setFillColor( $drawSpec['fill'] );
|
2011-06-17 23:26:45 +00:00
|
|
|
$draw->polygon( $drawSpec['shape'] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$image->drawImage( $draw );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$image->setImageFormat( $format );
|
2011-06-21 03:40:53 +00:00
|
|
|
|
|
|
|
|
// this doesn't work, even though it's documented to do so...
|
|
|
|
|
// $image->setImageOrientation( $orientation['exifCode'] );
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$image->writeImage( $filename );
|
2011-06-21 03:40:53 +00:00
|
|
|
|
|
|
|
|
// because the above setImageOrientation call doesn't work... nor can I get an external imagemagick binary to do this either...
|
|
|
|
|
// hacking this for now (only works if you have exiv2 installed, a program to read and manipulate exif)
|
|
|
|
|
if ( $wgExiv2Command ) {
|
|
|
|
|
$cmd = wfEscapeShellArg( $wgExiv2Command )
|
|
|
|
|
. " -M "
|
|
|
|
|
. wfEscapeShellArg( "set Exif.Image.Orientation " . $orientation['exifCode'] )
|
2011-12-08 13:02:34 +00:00
|
|
|
. " "
|
2011-06-21 03:40:53 +00:00
|
|
|
. wfEscapeShellArg( $filename );
|
|
|
|
|
|
|
|
|
|
$retval = 0;
|
|
|
|
|
$err = wfShellExec( $cmd, $retval );
|
|
|
|
|
if ( $retval !== 0 ) {
|
|
|
|
|
print "Error with $cmd: $retval, $err\n";
|
|
|
|
|
}
|
2011-08-04 22:33:04 +00:00
|
|
|
}
|
2011-06-21 03:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given an image specification, produce rotated version
|
2013-05-10 11:51:06 +00:00
|
|
|
* This is used when simulating a rotated image capture with Exif orientation
|
2011-12-08 13:02:34 +00:00
|
|
|
* @param $spec Object returned by getImageSpec
|
2011-06-21 03:40:53 +00:00
|
|
|
* @param $matrix 2x2 transformation matrix
|
2013-11-15 14:32:12 +00:00
|
|
|
* @return array transformed Spec
|
2011-06-21 03:40:53 +00:00
|
|
|
*/
|
|
|
|
|
private static function rotateImageSpec( &$spec, $matrix ) {
|
|
|
|
|
$tSpec = array();
|
|
|
|
|
$dims = self::matrixMultiply2x2( $matrix, $spec['width'], $spec['height'] );
|
|
|
|
|
$correctionX = 0;
|
|
|
|
|
$correctionY = 0;
|
|
|
|
|
if ( $dims['x'] < 0 ) {
|
|
|
|
|
$correctionX = abs( $dims['x'] );
|
2011-12-08 13:02:34 +00:00
|
|
|
}
|
|
|
|
|
if ( $dims['y'] < 0 ) {
|
2011-06-21 03:40:53 +00:00
|
|
|
$correctionY = abs( $dims['y'] );
|
|
|
|
|
}
|
|
|
|
|
$tSpec['width'] = abs( $dims['x'] );
|
|
|
|
|
$tSpec['height'] = abs( $dims['y'] );
|
|
|
|
|
$tSpec['fill'] = $spec['fill'];
|
|
|
|
|
$tSpec['draws'] = array();
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $spec['draws'] as $draw ) {
|
2011-12-08 13:02:34 +00:00
|
|
|
$tDraw = array(
|
2011-06-21 03:40:53 +00:00
|
|
|
'fill' => $draw['fill'],
|
|
|
|
|
'shape' => array()
|
|
|
|
|
);
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $draw['shape'] as $point ) {
|
2011-06-21 03:40:53 +00:00
|
|
|
$tPoint = self::matrixMultiply2x2( $matrix, $point['x'], $point['y'] );
|
|
|
|
|
$tPoint['x'] += $correctionX;
|
|
|
|
|
$tPoint['y'] += $correctionY;
|
|
|
|
|
$tDraw['shape'][] = $tPoint;
|
|
|
|
|
}
|
|
|
|
|
$tSpec['draws'][] = $tDraw;
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-06-21 03:40:53 +00:00
|
|
|
return $tSpec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a matrix and a pair of images, return new position
|
2011-12-08 13:02:34 +00:00
|
|
|
* @param $matrix: 2x2 rotation matrix
|
2011-06-21 03:40:53 +00:00
|
|
|
* @param $x: x-coordinate number
|
|
|
|
|
* @param $y: y-coordinate number
|
2011-12-08 13:02:34 +00:00
|
|
|
* @return Array transformed with properties x, y
|
2011-06-21 03:40:53 +00:00
|
|
|
*/
|
|
|
|
|
private static function matrixMultiply2x2( $matrix, $x, $y ) {
|
2011-12-08 13:02:34 +00:00
|
|
|
return array(
|
2011-06-21 03:40:53 +00:00
|
|
|
'x' => $x * $matrix[0][0] + $y * $matrix[0][1],
|
|
|
|
|
'y' => $x * $matrix[1][0] + $y * $matrix[1][1]
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Based on an image specification, write such an image to disk, using the command line ImageMagick program ('convert').
|
|
|
|
|
*
|
|
|
|
|
* Sample command line:
|
2011-12-08 13:02:34 +00:00
|
|
|
* $ convert -size 100x60 xc:rgb(90,87,45) \
|
2013-02-14 13:10:38 +00:00
|
|
|
* -draw 'fill rgb(12,34,56) polygon 41,39 44,57 50,57 41,39' \
|
2011-12-08 13:02:34 +00:00
|
|
|
* -draw 'fill rgb(99,123,231) circle 59,39 56,57' \
|
|
|
|
|
* -draw 'fill rgb(240,12,32) circle 50,21 50,3' filename.png
|
2010-12-14 16:26:35 +00:00
|
|
|
*
|
2013-11-15 14:32:12 +00:00
|
|
|
* @param array $spec spec describing background and shapes to draw
|
|
|
|
|
* @param string $format file format to write (unused by this method but kept so it has the same signature as
|
|
|
|
|
* writeImageWithApi)
|
|
|
|
|
* @param string $filename filename to write to
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
|
|
|
|
public function writeImageWithCommandLine( $spec, $format, $filename ) {
|
|
|
|
|
global $wgImageMagickConvertCommand;
|
|
|
|
|
$args = array();
|
|
|
|
|
$args[] = "-size " . wfEscapeShellArg( $spec['width'] . 'x' . $spec['height'] );
|
|
|
|
|
$args[] = wfEscapeShellArg( "xc:" . $spec['fill'] );
|
2013-02-14 13:10:38 +00:00
|
|
|
foreach ( $spec['draws'] as $draw ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$fill = $draw['fill'];
|
2011-06-18 03:36:44 +00:00
|
|
|
$polygon = self::shapePointsToString( $draw['shape'] );
|
2011-06-17 23:26:45 +00:00
|
|
|
$drawCommand = "fill $fill polygon $polygon";
|
2010-12-14 16:26:35 +00:00
|
|
|
$args[] = '-draw ' . wfEscapeShellArg( $drawCommand );
|
|
|
|
|
}
|
|
|
|
|
$args[] = wfEscapeShellArg( $filename );
|
|
|
|
|
|
|
|
|
|
$command = wfEscapeShellArg( $wgImageMagickConvertCommand ) . " " . implode( " ", $args );
|
|
|
|
|
$retval = null;
|
|
|
|
|
wfShellExec( $command, $retval );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
return ( $retval === 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-07 23:32:36 +00:00
|
|
|
* Generate a string of random colors for ImageMagick or SVG, like "rgb(12, 37, 98)"
|
2010-12-14 16:26:35 +00:00
|
|
|
*
|
2013-11-15 14:32:12 +00:00
|
|
|
* @return string
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
|
|
|
|
public function getRandomColor() {
|
|
|
|
|
$components = array();
|
2013-02-14 13:10:38 +00:00
|
|
|
for ( $i = 0; $i <= 2; $i++ ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$components[] = mt_rand( 0, 255 );
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-02-14 13:10:38 +00:00
|
|
|
return 'rgb(' . join( ', ', $components ) . ')';
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of random pairs of random words, like array( array( 'foo', 'bar' ), array( 'quux', 'baz' ) );
|
|
|
|
|
*
|
|
|
|
|
* @param $number 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 );
|
2013-02-14 13:10:38 +00:00
|
|
|
for ( $i = 0; $i < $count; $i += 2 ) {
|
|
|
|
|
$pairs[] = array( $lines[$i], $lines[$i + 1] );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
return $pairs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return N random lines from a file
|
|
|
|
|
*
|
|
|
|
|
* Will throw exception if the file could not be read or if it had fewer lines than requested.
|
|
|
|
|
*
|
2013-11-15 14:32:12 +00:00
|
|
|
* @param int $number_desired number of lines desired
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @return array of exactly n elements, drawn randomly from lines the file
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
|
|
|
|
private function getRandomLines( $number_desired ) {
|
|
|
|
|
$filepath = $this->dictionaryFile;
|
|
|
|
|
|
|
|
|
|
// 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" );
|
|
|
|
|
if ( !$fh ) {
|
2013-02-14 13:10:38 +00:00
|
|
|
throw new Exception( "couldn't open $filepath" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$line_number = 0;
|
|
|
|
|
$max_index = $number_desired - 1;
|
2013-02-14 13:10:38 +00:00
|
|
|
while ( !feof( $fh ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$line = fgets( $fh );
|
|
|
|
|
if ( $line !== false ) {
|
|
|
|
|
$line_number++;
|
|
|
|
|
$line = trim( $line );
|
|
|
|
|
if ( mt_rand( 0, $line_number ) <= $max_index ) {
|
2013-02-14 13:10:38 +00:00
|
|
|
$lines[mt_rand( 0, $max_index )] = $line;
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose( $fh );
|
|
|
|
|
if ( $line_number < $number_desired ) {
|
|
|
|
|
throw new Exception( "not enough lines in $filepath" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
}
|