cvs: pear /Image_Puzzle Puzzle.php /Image_Puzzle/Puzzle Edge.php Piece.php /Image_Puzzle/Puzzle/Edge Arc.php Default.php HalfCircle.php Line.php
Sinus.php
| From: | Michal Felski | Date: | Sun, 05 Mar 2006 10:09:54 +0000 |
| Subject: | cvs: pear /Image_Puzzle Puzzle.php /Image_Puzzle/Puzzle Edge.php Piece.php /Image_Puzzle/Puzzle/Edge Arc.php Default.php HalfCircle.php Line.php Sinus.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-38917@lists.php.net to get a copy of this message | ||
fela Sun Mar 5 10:09:54 2006 UTC
Added files:
/pear/Image_Puzzle Puzzle.php
/pear/Image_Puzzle/Puzzle Edge.php Piece.php
/pear/Image_Puzzle/Puzzle/Edge Arc.php Default.php HalfCircle.php
Line.php Sinus.php
Log:
initial development release of Puzzle package
http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle.php +++ pear/Image_Puzzle/Puzzle.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@php.net> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Require PEAR_Exception * */ require_once 'PEAR/Exception.php'; /** * Puzzle piece class required * */ require_once 'Image/Puzzle/Piece.php'; /** * Edge factory required * */ require_once 'Image/Puzzle/Edge.php'; /** * Image_Puzzle generate puzzle pieces from any image. There is an edges factory * to make puzzle in different shapes. An example of using this is as bellow: * * <code> * require_once 'Image/Puzzle.php'; * $options = array( * 'cols' => 4, * 'rows' => 4, * 'edge' => 'default' * ); * $puzzle = new Image_Puzzle($options); * $puzzle->createFromFile('image.jpg'); * $puzzle->saveAll('piece_[row]_[col].gif'); * </code> * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@php.net> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle { /** * Options for puzzle maker * * @var array * */ private $_options = array ( 'transparentColor' => 'white', 'cols' => 5, 'rows' => 5, 'edge' => 'default', ); /** * GD resource of source puzzle image recieved by imagecreate * * @var resource * */ private $_source; /** * Width of source puzzle image * * @var int * */ private $_sourceWidth; /** * Height of source puzzle image * * @var int * */ private $_sourceHeight; /** * List of pieces * * @var array * */ private $_pieces; /** * Puzzle object constructor. Options parameter is optional. * It allow to set how puzzle will be generated.<br /> * Available options are :<br /> * transparentColor - Color name or hex string or an RGB array of color * wich will be used as transparent. Default is white (string | array)<br /> * cols - Number of columns to create. Default is 5 (integer)<br /> * rows - Number of rows to create. Default is 5 (integer)<br /> * edge - Name of edge wich will be used. For example default, sinus (string) * * @param array $options an associative array of option names and values * @return Image_Puzzle a new puzzle object */ public function __construct($options = array()) { foreach ($options as $key=>$value){ if (in_array($key, array_keys($this->_options))) { $this->_options[$key] = $value; } } } /** * Create puzzle directly from file. * Currently it is only one avaliable method to create puzzle. * * @param string $filename Filename of source image */ public function createFromFile($filename) { if (!is_readable($filename)) { throw new PEAR_Exception("Can't read from $filename."); } $size = getimagesize($filename); $this->_sourceWidth = $size[0]; $this->_sourceHeight = $size[1]; $this->_source = imagecreatefromjpeg($filename); $this->_createPuzzle(); } /** * Returns puzzle piece at row $row and col $col. * * @param int $row Number of row started from 1 * @param int $col Number og col started from 1 * @return Image_Puzzle_Piece */ public function getPiece($row, $col) { if ($row < 1 || $row > $this->_options['rows']) { throw new PEAR_Exception("Illegal row number"); } if ($col < 1 || $col > $this->_options['cols']) { throw new PEAR_Exception("Illegal col number"); } return $this->_pieces[$row - 1][$col - 1]; } /** * Save all pieces to separate files with given name pattern. * $pattern parameter is used to define filename for each piece. * * @param string Name pattern of files. It can contain [row] and [col] * symbols which are replaced to current row and col number */ public function saveAll($pattern = 'piece[row]_[col].gif') { for ($col = 0; $col < $this->_options['cols']; $col++){ for ($row = 0; $row < $this->_options['rows']; $row++){ $filename = str_replace('[row]', $row + 1, $pattern); $filename = str_replace('[col]', $col + 1, $filename); $this->_pieces[$row][$col]->save($filename); } } } /** * Private method which create puzzle. Before it is called _source, * _sourceWidth and _sourceHeight properties need to be set. */ private function _createPuzzle() { $pieceWidth = round($this->_sourceWidth / $this->_options['cols']); $pieceHeight = round($this->_sourceHeight / $this->_options['rows']); $this->_createPieces($pieceWidth, $pieceHeight); $this->_createEdges($pieceWidth, $pieceHeight); $this->_setPictures(); } /** * Fills _pieces array with piece objects. * * @param int $width Piece width * @param int $height Piece height */ private function _createPieces($width, $height) { $this->_pieces = array(); for ($col = 0; $col < $this->_options['cols']; $col++) { for ($row = 0; $row < $this->_options['rows']; $row++) { $this->_pieces[$row][$col] = new Image_Puzzle_Piece( $width * $col, $height * $row, $width, $height); } } } /** * Sets edges for each piece * * @param int $width Piece width * @param int $height Piece height */ private function _createEdges($pieceWidth, $pieceHeight) { $edge = Image_Puzzle_Edge::factory('line', $pieceWidth, $pieceHeight); for ($col = 0; $col < $this->_options['cols']; $col++){ $this->_pieces[0][$col]->setTopEdge($edge); $this->_pieces[$this->_options['rows']-1][$col]->setBottomEdge($edge); } $edge = Image_Puzzle_Edge::factory('line', $pieceHeight, $pieceWidth); for ($row = 0; $row < $this->_options['rows']; $row++){ $this->_pieces[$row][0]->setLeftEdge($edge); $this->_pieces[$row][$this->_options['cols']-1]->setRightEdge($edge); } for ($col = 0; $col < $this->_options['cols']-1; $col++){ for ($row = 0; $row < $this->_options['rows']; $row++){ $edge = Image_Puzzle_Edge::factory($this->_options['edge'], $pieceHeight, $pieceWidth); $this->_pieces[$row][$col+1]->setLeftEdge($edge); $this->_pieces[$row][$col]->setRightEdge($edge); } } for ($col = 0; $col < $this->_options['cols']; $col++){ for ($row = 0; $row < $this->_options['rows']-1; $row++){ $edge = Image_Puzzle_Edge::factory($this->_options['edge'], $pieceWidth, $pieceHeight); $this->_pieces[$row+1][$col]->setTopEdge($edge); $this->_pieces[$row][$col]->setBottomEdge($edge); } } } /** * creating small pictures from source and adding transparent color on edges * for each piece. */ private function _setPictures() { for ($col = 0; $col < $this->_options['cols']; $col++) { for ($row = 0; $row < $this->_options['rows']; $row++) { $piece = $this->_pieces[$row][$col]; $image = imagecreatetruecolor($piece->getWidth(), $piece->getHeight()); imagecopy($image, $this->_source, 0, 0, $piece->getLeft(), $piece->getTop(), $piece->getWidth(), $piece->getHeight()); $piece->setImage($image); $piece->addTransparent($this->_options['transparentColor']); } } } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge.php +++ pear/Image_Puzzle/Puzzle/Edge.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Abstract class for puzzle edge * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ abstract class Image_Puzzle_Edge { /** * Longitude length of edge in pixels * * @var int */ protected $longitude; /** * Transversal length of edge in pixels * * @var int */ protected $transversal; /** * Edge constructor. * Creates new edge object. * * @param int $longitude Longitude length of edge in pixels * @param int $transversal Transversal length of edge in pixels */ public function __construct($longitude, $transversal) { $this->longitude = $longitude; $this->transversal = $transversal; } /** * Returns left margin for vertical edge * or top margin for horizontal edge. * */ abstract public function getLeftTopMargin(); /** * Returns right margin for vertical edge * or bottom margin for horizontal edge. * */ abstract public function getRightBottomMargin(); /** * Returns true if point x,y on the edge should be transparent. * * @param int $x X coordinate * @param int $y Y coordinate * @return boolean Returns true if point x,y should be transparent. */ public function isTransparent($x, $y){ return false; } /** * Edge factory for creating new edge objects in the basis of their names. * * @param string $edgeName Name of the edge class * @param int $longitude Longitude length of edge in pixels * @param int $transversal Transversal length of edge in pixels * @return Image_Puzzle_Edge Returns new edge object */ static public function factory($edgeName, $longitude, $transversal) { $edgeName = ucfirst($edgeName); $edgeFile = "Image/Puzzle/Edge/$edgeName.php"; $edgeClass = "Image_Puzzle_Edge_$edgeName"; if (!file_exists($edgeFile)) { throw new PEAR_Exception("Unknown puzzle edge $edgeName"); } require_once $edgeFile; if (!class_exists($edgeClass)) { throw new PEAR_Exception("Edge $edgeName not found in $edgeFile"); } $edge = new $edgeClass($longitude, $transversal); if (!$edge instanceof Image_Puzzle_Edge) { throw new PEAR_Exception("Edge $edgeName does not implements edge interface"); } return $edge; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Piece.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Piece.php +++ pear/Image_Puzzle/Puzzle/Piece.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Image_Puzzle_Piece class * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Piece { /** * Initial value of X position * * @var integer */ private $_left; /** * Initial value of Y position * * @var integer */ private $_top; /** * Initial piece width * * @var integer */ private $_width; /** * Initial piece height * * @var integer */ private $_height; /** * GD resource image * * @var resource */ private $_image; private $_marginCache = array(); /** * Array of Image_Puzzle_Edge objects * * @var array */ private $_edges = array('Top' => null, 'Bottom' => null, 'Left' => null, 'Right' => null); private $_transparentColor; /** * Constructor * * @param integer $left * @param integer $top * @param integer $width * @param integer $height */ public function __construct($left, $top, $width, $height) { $this->_left = $left; $this->_top = $top; $this->_width = $width; $this->_height = $height; } /** * Set left edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setLeftEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Left', $edge); } /** * Set right edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setRightEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Right', $edge); } /** * Set top edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setTopEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Top', $edge); } /** * Set bottom edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setBottomEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Bottom', $edge); } /** * Set piece image. It should be used only by Image_Puzzle class * to set image with needed margins * * @param resource $image */ public function setImage($image) { if ($this->_image !== null) { throw new PEAR_Exception('Image already set'); } $this->_image = $image; } /** * Returns X coordinate of this piece according to whole image * * @return integer */ public function getLeft() { return $this->_left - $this->_marginCache['Left']['LeftTop']; } /** * Returns Y coordinate of this piece according to whole image * * @return integer */ public function getTop() { return $this->_top - $this->_marginCache['Top']['LeftTop']; } /** * Returns width in pixels of the piece * * @return integer */ public function getWidth() { return $this->_width + $this->_marginCache['Left']['LeftTop'] + $this->_marginCache['Right']['RightBottom']; } /** * Returns height in pixels of the piece * * @return integer */ public function getHeight() { return $this->_height + $this->_marginCache['Top']['LeftTop'] + $this->_marginCache['Bottom']['RightBottom']; } /** * Saves image to the disk on given $filename * * @param string $filename */ public function save($filename) { imagegif($this->_image, $filename); } /** * Make transparent pixels which should be not visible * Color parameter is a color accepted by Image_Color2 * @see Image_Color2 * * @param array|string|Image_Color2_Model $src specifying a color. * Non-RGB arrays should include the type element to specify a * color model. Strings will be interpreted as hex if they * begin with a #, otherwise they'll be treated as named colors. */ public function addTransparent($color) { $this->_setTransparentColor($color); foreach (array_keys($this->_edges) as $edgeName) { $this->_makeEdgeTransparent($edgeName); } $this->_makeCornersTransparent(); } /** * Allocates transparent color to resource image * * @param array|string|Image_Color2_Model $src specifying a color. * Non-RGB arrays should include the type element to specify a * color model. Strings will be interpreted as hex if they * begin with a #, otherwise they'll be treated as named colors. */ private function _setTransparentColor($color) { require_once 'Image/Color2.php'; $conversion = new Image_Color2($color); $rgb = $conversion->getRgb(); $this->_transparentColor = imagecolorallocate($this->_image, $rgb[0], $rgb[1], $rgb[2]); imagecolortransparent($this->_image, $this->_transparentColor); } /** * One edge process for adding transparent pixels * * @param string $edgeName */ private function _makeEdgeTransparent($edgeName) { for ($i = 0; $i <= $this->_getEdgeLength($edgeName); $i++){ for ($j = - $this->_marginCache[$edgeName]['RightBottom']; $j <= $this->_marginCache[$edgeName]['LeftTop']; $j++) { $this->_makeEdgePointTransparent($edgeName, $i, $j); } } } /** * Fills died corners with transparent color * */ private function _makeCornersTransparent() { $top = $this->_marginCache['Top']['LeftTop']; $left = $this->_marginCache['Left']['LeftTop']; imagefilledrectangle($this->_image, 0, 0, $left - 1, $top - 1, $this->_transparentColor); imagefilledrectangle($this->_image, $left + $this->_width, 0, $this->getWidth(), $top, $this->_transparentColor); imagefilledrectangle($this->_image, 0, $top + $this->_height, $left, $this->getHeight(), $this->_transparentColor); imagefilledrectangle($this->_image, $left + $this->_width, $top + $this->_height, $this->getWidth(), $this->getHeight(), $this->_transparentColor); } /** * Returns length of edge without margins * * @param string $edgeName * @return integer */ private function _getEdgeLength($edgeName) { if ($edgeName == 'Top' || $edgeName == 'Bottom') { return $this->_width; } return $this->_height; } /** * Returns offsest value of edge points according to resource image * X-coordinate * * @param string $edgeName * @param integer $i * @param integer $j * @return integer */ private function _getXOfset($edgeName, $i, $j) { switch ($edgeName) { case 'Top': case 'Bottom': return $i; case 'Left': return - $j; case 'Right': return $this->_width - $j; } } /** * Returns offsest value of edge points according to resource image * Y-coordinate * * @param string $edgeName * @param integer $i * @param integer $j * @return integer */ private function _getYOfset($edgeName, $i, $j) { switch ($edgeName) { case 'Top': return - $j; case 'Left': case 'Right': return $i; case 'Bottom': return $this->_height - $j; } } /** * Returns true is point should be transparent * * @param string $edgeName * @param integer $i * @param integer $j * @return boolean */ private function _pointShouldBeTransparent($edgeName, $i, $j) { $transparent = $this->_edges[$edgeName]->isTransparent($i,$j); if ($edgeName == 'Right' || $edgeName == 'Bottom') { return !$transparent; } return $transparent; } /** * Set point as transparent * i,j coordinates on edge $edgeName * * @param string $edgeName * @param integer $i * @param integer $j */ private function _makeEdgePointTransparent($edgeName, $i, $j) { if (!$this->_pointShouldBeTransparent($edgeName, $i, $j)) { return; } $this->_setPixelTransparent( $this->_marginCache['Left']['LeftTop'] + $this->_getXOfset($edgeName, $i, $j), $this->_marginCache['Top']['LeftTop'] + $this->_getYOfset($edgeName, $i, $j) ); } /** * Puts edge into array on $edgeName index * * @param string $edgeName * @param Image_Puzzle_Edge $edge */ private function _setEdge($edgeName, Image_Puzzle_Edge $edge) { $this->_edges[$edgeName] = $edge; $this->_marginCache[$edgeName]['LeftTop'] = $edge->getLeftTopMargin(); $this->_marginCache[$edgeName]['RightBottom'] = $edge->getRightBottomMargin(); } /** * set pixel as transparent. Now it uses GD library * x,y point coordinates * * @param integer $x * @param integer $y */ private function _setPixelTransparent($x, $y) { imagesetpixel($this->_image, $x, $y, $this->_transparentColor); } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Arc.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Arc.php +++ pear/Image_Puzzle/Puzzle/Edge/Arc.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Arc edge * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Arc extends Image_Puzzle_Edge { private $_radius = 0; private $_side = 0; private $_factor; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_factor = rand(85,95) /100; $this->_radius = $longitude / 2 / sqrt(1-$this->_factor * $this->_factor); $this->_side = rand()&1; } public function getLeftTopMargin() { return (1 - $this->_factor) * $this->_radius * !$this->_side; } public function getRightBottomMargin() { return (1 - $this->_factor) * $this->_radius * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { $y = $y - $this->_factor * $this->_radius; return $x*$x + $y*$y < $this->_radius * $this->_radius; } else { $y = $y + $this->_factor * $this->_radius; return $x*$x + $y*$y > $this->_radius * $this->_radius; } } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Default.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Default.php +++ pear/Image_Puzzle/Puzzle/Edge/Default.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Default edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Default extends Image_Puzzle_Edge { private $_longRadius; private $_transRadius; private $_side = 0; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $factor = rand(10,12) / 100; $this->_longRadius = max($longitude, $transversal) * $factor; $this->_transRadius = min($longitude, $transversal) * $factor; $this->_side = rand()&1; } public function getLeftTopMargin() { return $this->_transRadius * 2.2 * !$this->_side; } public function getRightBottomMargin() { return $this->_transRadius * 2.2 * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { return $this->_isInsideCircle($x, $y + 1.2 * $this->_transRadius) || $this->_isInsideSquare($x, $y); } else { return !($this->_isInsideCircle($x, $y - 1.2 * $this->_transRadius) || $this->_isInsideSquare($x, $y)); } } private function _isInsideCircle($x, $y){ return $x*$x / $this->_longRadius / $this->_longRadius + $y * $y / $this->_transRadius / $this->_transRadius < 1; } private function _isInsideSquare($x, $y){ return (abs($x) < $this->_longRadius * 0.5) && (abs($y) < $this->_transRadius * 0.5); } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php +++ pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Half circle edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_HalfCircle extends Image_Puzzle_Edge { private $_radius = 0; private $_side = 0; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_radius = min($longitude, $transversal) / rand(40,60) * 10; $this->_side = rand()&1; } public function getLeftTopMargin() { return $this->_radius * !$this->_side; } public function getRightBottomMargin() { return $this->_radius * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { return $x*$x + $y*$y < $this->_radius * $this->_radius; } return $x*$x + $y*$y > $this->_radius * $this->_radius; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Line.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Line.php +++ pear/Image_Puzzle/Puzzle/Edge/Line.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Linear edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Line extends Image_Puzzle_Edge { public function getLeftTopMargin() { return 0; } public function getRightBottomMargin() { return 0; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Sinus.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Sinus.php +++ pear/Image_Puzzle/Puzzle/Edge/Sinus.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ require_once 'Image/Puzzle/Edge.php'; /** * Sinus edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Sinus extends Image_Puzzle_Edge { private $_periods; private $_margin; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_periods = rand(3,6); $this->_margin = $transversal * 0.08 * rand(8,12) / 10; } public function getLeftTopMargin() { return $this->_margin; } public function getRightBottomMargin() { return $this->_margin; } public function isTransparent($x, $y) { return $y > sin(deg2rad($x / $this->longitude * 360 * $this->_periods)) * $this->_margin; } } ?>
http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle.php +++ pear/Image_Puzzle/Puzzle.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@php.net> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Require PEAR_Exception * */ require_once 'PEAR/Exception.php'; /** * Puzzle piece class required * */ require_once 'Image/Puzzle/Piece.php'; /** * Edge factory required * */ require_once 'Image/Puzzle/Edge.php'; /** * Image_Puzzle generate puzzle pieces from any image. There is an edges factory * to make puzzle in different shapes. An example of using this is as bellow: * * <code> * require_once 'Image/Puzzle.php'; * $options = array( * 'cols' => 4, * 'rows' => 4, * 'edge' => 'default' * ); * $puzzle = new Image_Puzzle($options); * $puzzle->createFromFile('image.jpg'); * $puzzle->saveAll('piece_[row]_[col].gif'); * </code> * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@php.net> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle { /** * Options for puzzle maker * * @var array * */ private $_options = array ( 'transparentColor' => 'white', 'cols' => 5, 'rows' => 5, 'edge' => 'default', ); /** * GD resource of source puzzle image recieved by imagecreate * * @var resource * */ private $_source; /** * Width of source puzzle image * * @var int * */ private $_sourceWidth; /** * Height of source puzzle image * * @var int * */ private $_sourceHeight; /** * List of pieces * * @var array * */ private $_pieces; /** * Puzzle object constructor. Options parameter is optional. * It allow to set how puzzle will be generated.<br /> * Available options are :<br /> * transparentColor - Color name or hex string or an RGB array of color * wich will be used as transparent. Default is white (string | array)<br /> * cols - Number of columns to create. Default is 5 (integer)<br /> * rows - Number of rows to create. Default is 5 (integer)<br /> * edge - Name of edge wich will be used. For example default, sinus (string) * * @param array $options an associative array of option names and values * @return Image_Puzzle a new puzzle object */ public function __construct($options = array()) { foreach ($options as $key=>$value){ if (in_array($key, array_keys($this->_options))) { $this->_options[$key] = $value; } } } /** * Create puzzle directly from file. * Currently it is only one avaliable method to create puzzle. * * @param string $filename Filename of source image */ public function createFromFile($filename) { if (!is_readable($filename)) { throw new PEAR_Exception("Can't read from $filename."); } $size = getimagesize($filename); $this->_sourceWidth = $size[0]; $this->_sourceHeight = $size[1]; $this->_source = imagecreatefromjpeg($filename); $this->_createPuzzle(); } /** * Returns puzzle piece at row $row and col $col. * * @param int $row Number of row started from 1 * @param int $col Number og col started from 1 * @return Image_Puzzle_Piece */ public function getPiece($row, $col) { if ($row < 1 || $row > $this->_options['rows']) { throw new PEAR_Exception("Illegal row number"); } if ($col < 1 || $col > $this->_options['cols']) { throw new PEAR_Exception("Illegal col number"); } return $this->_pieces[$row - 1][$col - 1]; } /** * Save all pieces to separate files with given name pattern. * $pattern parameter is used to define filename for each piece. * * @param string Name pattern of files. It can contain [row] and [col] * symbols which are replaced to current row and col number */ public function saveAll($pattern = 'piece[row]_[col].gif') { for ($col = 0; $col < $this->_options['cols']; $col++){ for ($row = 0; $row < $this->_options['rows']; $row++){ $filename = str_replace('[row]', $row + 1, $pattern); $filename = str_replace('[col]', $col + 1, $filename); $this->_pieces[$row][$col]->save($filename); } } } /** * Private method which create puzzle. Before it is called _source, * _sourceWidth and _sourceHeight properties need to be set. */ private function _createPuzzle() { $pieceWidth = round($this->_sourceWidth / $this->_options['cols']); $pieceHeight = round($this->_sourceHeight / $this->_options['rows']); $this->_createPieces($pieceWidth, $pieceHeight); $this->_createEdges($pieceWidth, $pieceHeight); $this->_setPictures(); } /** * Fills _pieces array with piece objects. * * @param int $width Piece width * @param int $height Piece height */ private function _createPieces($width, $height) { $this->_pieces = array(); for ($col = 0; $col < $this->_options['cols']; $col++) { for ($row = 0; $row < $this->_options['rows']; $row++) { $this->_pieces[$row][$col] = new Image_Puzzle_Piece( $width * $col, $height * $row, $width, $height); } } } /** * Sets edges for each piece * * @param int $width Piece width * @param int $height Piece height */ private function _createEdges($pieceWidth, $pieceHeight) { $edge = Image_Puzzle_Edge::factory('line', $pieceWidth, $pieceHeight); for ($col = 0; $col < $this->_options['cols']; $col++){ $this->_pieces[0][$col]->setTopEdge($edge); $this->_pieces[$this->_options['rows']-1][$col]->setBottomEdge($edge); } $edge = Image_Puzzle_Edge::factory('line', $pieceHeight, $pieceWidth); for ($row = 0; $row < $this->_options['rows']; $row++){ $this->_pieces[$row][0]->setLeftEdge($edge); $this->_pieces[$row][$this->_options['cols']-1]->setRightEdge($edge); } for ($col = 0; $col < $this->_options['cols']-1; $col++){ for ($row = 0; $row < $this->_options['rows']; $row++){ $edge = Image_Puzzle_Edge::factory($this->_options['edge'], $pieceHeight, $pieceWidth); $this->_pieces[$row][$col+1]->setLeftEdge($edge); $this->_pieces[$row][$col]->setRightEdge($edge); } } for ($col = 0; $col < $this->_options['cols']; $col++){ for ($row = 0; $row < $this->_options['rows']-1; $row++){ $edge = Image_Puzzle_Edge::factory($this->_options['edge'], $pieceWidth, $pieceHeight); $this->_pieces[$row+1][$col]->setTopEdge($edge); $this->_pieces[$row][$col]->setBottomEdge($edge); } } } /** * creating small pictures from source and adding transparent color on edges * for each piece. */ private function _setPictures() { for ($col = 0; $col < $this->_options['cols']; $col++) { for ($row = 0; $row < $this->_options['rows']; $row++) { $piece = $this->_pieces[$row][$col]; $image = imagecreatetruecolor($piece->getWidth(), $piece->getHeight()); imagecopy($image, $this->_source, 0, 0, $piece->getLeft(), $piece->getTop(), $piece->getWidth(), $piece->getHeight()); $piece->setImage($image); $piece->addTransparent($this->_options['transparentColor']); } } } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge.php +++ pear/Image_Puzzle/Puzzle/Edge.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Abstract class for puzzle edge * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ abstract class Image_Puzzle_Edge { /** * Longitude length of edge in pixels * * @var int */ protected $longitude; /** * Transversal length of edge in pixels * * @var int */ protected $transversal; /** * Edge constructor. * Creates new edge object. * * @param int $longitude Longitude length of edge in pixels * @param int $transversal Transversal length of edge in pixels */ public function __construct($longitude, $transversal) { $this->longitude = $longitude; $this->transversal = $transversal; } /** * Returns left margin for vertical edge * or top margin for horizontal edge. * */ abstract public function getLeftTopMargin(); /** * Returns right margin for vertical edge * or bottom margin for horizontal edge. * */ abstract public function getRightBottomMargin(); /** * Returns true if point x,y on the edge should be transparent. * * @param int $x X coordinate * @param int $y Y coordinate * @return boolean Returns true if point x,y should be transparent. */ public function isTransparent($x, $y){ return false; } /** * Edge factory for creating new edge objects in the basis of their names. * * @param string $edgeName Name of the edge class * @param int $longitude Longitude length of edge in pixels * @param int $transversal Transversal length of edge in pixels * @return Image_Puzzle_Edge Returns new edge object */ static public function factory($edgeName, $longitude, $transversal) { $edgeName = ucfirst($edgeName); $edgeFile = "Image/Puzzle/Edge/$edgeName.php"; $edgeClass = "Image_Puzzle_Edge_$edgeName"; if (!file_exists($edgeFile)) { throw new PEAR_Exception("Unknown puzzle edge $edgeName"); } require_once $edgeFile; if (!class_exists($edgeClass)) { throw new PEAR_Exception("Edge $edgeName not found in $edgeFile"); } $edge = new $edgeClass($longitude, $transversal); if (!$edge instanceof Image_Puzzle_Edge) { throw new PEAR_Exception("Edge $edgeName does not implements edge interface"); } return $edge; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Piece.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Piece.php +++ pear/Image_Puzzle/Puzzle/Piece.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Image_Puzzle_Piece class * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Piece { /** * Initial value of X position * * @var integer */ private $_left; /** * Initial value of Y position * * @var integer */ private $_top; /** * Initial piece width * * @var integer */ private $_width; /** * Initial piece height * * @var integer */ private $_height; /** * GD resource image * * @var resource */ private $_image; private $_marginCache = array(); /** * Array of Image_Puzzle_Edge objects * * @var array */ private $_edges = array('Top' => null, 'Bottom' => null, 'Left' => null, 'Right' => null); private $_transparentColor; /** * Constructor * * @param integer $left * @param integer $top * @param integer $width * @param integer $height */ public function __construct($left, $top, $width, $height) { $this->_left = $left; $this->_top = $top; $this->_width = $width; $this->_height = $height; } /** * Set left edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setLeftEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Left', $edge); } /** * Set right edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setRightEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Right', $edge); } /** * Set top edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setTopEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Top', $edge); } /** * Set bottom edge for this piece * * @param Image_Puzzle_Edge $edge */ public function setBottomEdge(Image_Puzzle_Edge $edge) { $this->_setEdge('Bottom', $edge); } /** * Set piece image. It should be used only by Image_Puzzle class * to set image with needed margins * * @param resource $image */ public function setImage($image) { if ($this->_image !== null) { throw new PEAR_Exception('Image already set'); } $this->_image = $image; } /** * Returns X coordinate of this piece according to whole image * * @return integer */ public function getLeft() { return $this->_left - $this->_marginCache['Left']['LeftTop']; } /** * Returns Y coordinate of this piece according to whole image * * @return integer */ public function getTop() { return $this->_top - $this->_marginCache['Top']['LeftTop']; } /** * Returns width in pixels of the piece * * @return integer */ public function getWidth() { return $this->_width + $this->_marginCache['Left']['LeftTop'] + $this->_marginCache['Right']['RightBottom']; } /** * Returns height in pixels of the piece * * @return integer */ public function getHeight() { return $this->_height + $this->_marginCache['Top']['LeftTop'] + $this->_marginCache['Bottom']['RightBottom']; } /** * Saves image to the disk on given $filename * * @param string $filename */ public function save($filename) { imagegif($this->_image, $filename); } /** * Make transparent pixels which should be not visible * Color parameter is a color accepted by Image_Color2 * @see Image_Color2 * * @param array|string|Image_Color2_Model $src specifying a color. * Non-RGB arrays should include the type element to specify a * color model. Strings will be interpreted as hex if they * begin with a #, otherwise they'll be treated as named colors. */ public function addTransparent($color) { $this->_setTransparentColor($color); foreach (array_keys($this->_edges) as $edgeName) { $this->_makeEdgeTransparent($edgeName); } $this->_makeCornersTransparent(); } /** * Allocates transparent color to resource image * * @param array|string|Image_Color2_Model $src specifying a color. * Non-RGB arrays should include the type element to specify a * color model. Strings will be interpreted as hex if they * begin with a #, otherwise they'll be treated as named colors. */ private function _setTransparentColor($color) { require_once 'Image/Color2.php'; $conversion = new Image_Color2($color); $rgb = $conversion->getRgb(); $this->_transparentColor = imagecolorallocate($this->_image, $rgb[0], $rgb[1], $rgb[2]); imagecolortransparent($this->_image, $this->_transparentColor); } /** * One edge process for adding transparent pixels * * @param string $edgeName */ private function _makeEdgeTransparent($edgeName) { for ($i = 0; $i <= $this->_getEdgeLength($edgeName); $i++){ for ($j = - $this->_marginCache[$edgeName]['RightBottom']; $j <= $this->_marginCache[$edgeName]['LeftTop']; $j++) { $this->_makeEdgePointTransparent($edgeName, $i, $j); } } } /** * Fills died corners with transparent color * */ private function _makeCornersTransparent() { $top = $this->_marginCache['Top']['LeftTop']; $left = $this->_marginCache['Left']['LeftTop']; imagefilledrectangle($this->_image, 0, 0, $left - 1, $top - 1, $this->_transparentColor); imagefilledrectangle($this->_image, $left + $this->_width, 0, $this->getWidth(), $top, $this->_transparentColor); imagefilledrectangle($this->_image, 0, $top + $this->_height, $left, $this->getHeight(), $this->_transparentColor); imagefilledrectangle($this->_image, $left + $this->_width, $top + $this->_height, $this->getWidth(), $this->getHeight(), $this->_transparentColor); } /** * Returns length of edge without margins * * @param string $edgeName * @return integer */ private function _getEdgeLength($edgeName) { if ($edgeName == 'Top' || $edgeName == 'Bottom') { return $this->_width; } return $this->_height; } /** * Returns offsest value of edge points according to resource image * X-coordinate * * @param string $edgeName * @param integer $i * @param integer $j * @return integer */ private function _getXOfset($edgeName, $i, $j) { switch ($edgeName) { case 'Top': case 'Bottom': return $i; case 'Left': return - $j; case 'Right': return $this->_width - $j; } } /** * Returns offsest value of edge points according to resource image * Y-coordinate * * @param string $edgeName * @param integer $i * @param integer $j * @return integer */ private function _getYOfset($edgeName, $i, $j) { switch ($edgeName) { case 'Top': return - $j; case 'Left': case 'Right': return $i; case 'Bottom': return $this->_height - $j; } } /** * Returns true is point should be transparent * * @param string $edgeName * @param integer $i * @param integer $j * @return boolean */ private function _pointShouldBeTransparent($edgeName, $i, $j) { $transparent = $this->_edges[$edgeName]->isTransparent($i,$j); if ($edgeName == 'Right' || $edgeName == 'Bottom') { return !$transparent; } return $transparent; } /** * Set point as transparent * i,j coordinates on edge $edgeName * * @param string $edgeName * @param integer $i * @param integer $j */ private function _makeEdgePointTransparent($edgeName, $i, $j) { if (!$this->_pointShouldBeTransparent($edgeName, $i, $j)) { return; } $this->_setPixelTransparent( $this->_marginCache['Left']['LeftTop'] + $this->_getXOfset($edgeName, $i, $j), $this->_marginCache['Top']['LeftTop'] + $this->_getYOfset($edgeName, $i, $j) ); } /** * Puts edge into array on $edgeName index * * @param string $edgeName * @param Image_Puzzle_Edge $edge */ private function _setEdge($edgeName, Image_Puzzle_Edge $edge) { $this->_edges[$edgeName] = $edge; $this->_marginCache[$edgeName]['LeftTop'] = $edge->getLeftTopMargin(); $this->_marginCache[$edgeName]['RightBottom'] = $edge->getRightBottomMargin(); } /** * set pixel as transparent. Now it uses GD library * x,y point coordinates * * @param integer $x * @param integer $y */ private function _setPixelTransparent($x, $y) { imagesetpixel($this->_image, $x, $y, $this->_transparentColor); } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Arc.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Arc.php +++ pear/Image_Puzzle/Puzzle/Edge/Arc.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Arc edge * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Arc extends Image_Puzzle_Edge { private $_radius = 0; private $_side = 0; private $_factor; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_factor = rand(85,95) /100; $this->_radius = $longitude / 2 / sqrt(1-$this->_factor * $this->_factor); $this->_side = rand()&1; } public function getLeftTopMargin() { return (1 - $this->_factor) * $this->_radius * !$this->_side; } public function getRightBottomMargin() { return (1 - $this->_factor) * $this->_radius * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { $y = $y - $this->_factor * $this->_radius; return $x*$x + $y*$y < $this->_radius * $this->_radius; } else { $y = $y + $this->_factor * $this->_radius; return $x*$x + $y*$y > $this->_radius * $this->_radius; } } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Default.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Default.php +++ pear/Image_Puzzle/Puzzle/Edge/Default.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Default edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Default extends Image_Puzzle_Edge { private $_longRadius; private $_transRadius; private $_side = 0; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $factor = rand(10,12) / 100; $this->_longRadius = max($longitude, $transversal) * $factor; $this->_transRadius = min($longitude, $transversal) * $factor; $this->_side = rand()&1; } public function getLeftTopMargin() { return $this->_transRadius * 2.2 * !$this->_side; } public function getRightBottomMargin() { return $this->_transRadius * 2.2 * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { return $this->_isInsideCircle($x, $y + 1.2 * $this->_transRadius) || $this->_isInsideSquare($x, $y); } else { return !($this->_isInsideCircle($x, $y - 1.2 * $this->_transRadius) || $this->_isInsideSquare($x, $y)); } } private function _isInsideCircle($x, $y){ return $x*$x / $this->_longRadius / $this->_longRadius + $y * $y / $this->_transRadius / $this->_transRadius < 1; } private function _isInsideSquare($x, $y){ return (abs($x) < $this->_longRadius * 0.5) && (abs($y) < $this->_transRadius * 0.5); } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php +++ pear/Image_Puzzle/Puzzle/Edge/HalfCircle.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Half circle edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_HalfCircle extends Image_Puzzle_Edge { private $_radius = 0; private $_side = 0; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_radius = min($longitude, $transversal) / rand(40,60) * 10; $this->_side = rand()&1; } public function getLeftTopMargin() { return $this->_radius * !$this->_side; } public function getRightBottomMargin() { return $this->_radius * $this->_side; } public function isTransparent($x, $y) { $x = $x - $this->longitude / 2; if ($this->_side) { return $x*$x + $y*$y < $this->_radius * $this->_radius; } return $x*$x + $y*$y > $this->_radius * $this->_radius; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Line.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Line.php +++ pear/Image_Puzzle/Puzzle/Edge/Line.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ /** * Linear edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Line extends Image_Puzzle_Edge { public function getLeftTopMargin() { return 0; } public function getRightBottomMargin() { return 0; } } ?> http://cvs.php.net/viewcvs.cgi/pear/Image_Puzzle/Puzzle/Edge/Sinus.php?view=markup&rev=1.1 Index: pear/Image_Puzzle/Puzzle/Edge/Sinus.php +++ pear/Image_Puzzle/Puzzle/Edge/Sinus.php <?php /** * PHP Puzzle Maker. * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version $id$ */ require_once 'Image/Puzzle/Edge.php'; /** * Sinus edge. * * @category Image * @package Image_Puzzle * @author Michal Felski <fela@fela.pl> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @link http://pear.php.net/package/Image_Puzzle * @version @package_version@ */ class Image_Puzzle_Edge_Sinus extends Image_Puzzle_Edge { private $_periods; private $_margin; public function __construct($longitude, $transversal) { parent::__construct($longitude, $transversal); $this->_periods = rand(3,6); $this->_margin = $transversal * 0.08 * rand(8,12) / 10; } public function getLeftTopMargin() { return $this->_margin; } public function getRightBottomMargin() { return $this->_margin; } public function isTransparent($x, $y) { return $y > sin(deg2rad($x / $this->longitude * 360 * $this->_periods)) * $this->_margin; } } ?>