PHP Classes

PHP Draw: Render an image using drawing command language

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 41%Total: 1,334 All time: 2,901 This week: 206Up
Version License PHP version Categories
phpdraw 1.2GNU Lesser Genera...5.1PHP 5, Graphics
Description 

Author

This package can be used to render an image using a drawing command language.

It takes a string with commands that define the graphic primitives to be drawn in an image.

Several commands are currently supported: right, left, up, down, ellipse, filledellipse, filledarc, rectangle, polygon, color, x, y, pixel, fontsize, text and file.

The main class interprets the drawing commands by calling separate classes that draw the requested graphic primitives in an image.

The image with the rendered graphics may be saved to file in the PNG format.

The same image file can be used in subsequent requests to allow the user to perform interactive graphic drawings.

Innovation Award
PHP Programming Innovation award nominee
November 2007
Number 6
AJAX allows updating Web pages overlaying graphics without page reloading.

This class can be used to implement a graphics drawing Web application that renders overlayed graphic shapes using AJAX to update the page drawn by the user.

Manuel Lemos
Picture of Johan Barbier
Name: Johan Barbier <contact>
Classes: 17 packages by
Country: France France
Age: 53
All time rank: 402 in France France
Week rank: 195 Down10 in France France Down
Innovation award
Innovation award
Nominee: 6x

Example

<?php
require_once '../inc/inc.main.php';
header('Content-type: text/xml');

if(isset(
$_POST['EXPR'])) {
    try {
        if(!empty(
$_POST['FILE'])) {
           
$oInterpreter = new interpreter('../'.$_POST['FILE']);
        } else {
           
$oInterpreter = new interpreter;
        }
       
        if(!
is_dir('../tmpimg')) {
           
mkdir('../tmpimg', 0755);
        }
       
$aDir = scandir('../tmpimg');
       
$aDir = array_diff($aDir, array('.', '..'));
        foreach(
$aDir as $sToBeDeleted) {
           
unlink('../tmpimg/'.$sToBeDeleted);
        }
       
       
$sFile = uniqid().'.png';
       
       
$oInterpreter->X = (int)$_POST['X'];
       
$oInterpreter->Y = (int)$_POST['Y'];
       
$oInterpreter->COLOR = $_POST['COLOR'];
       
$oInterpreter->PIXEL = (int)$_POST['PIXEL'];
       
$oInterpreter->FONTSIZE = (int)$_POST['FONTSIZE'];
       
$oInterpreter->interpret($_POST['EXPR']);
       
$oInterpreter->getSavedMove('../tmpimg/'.$sFile);
       
$oDom = new DOMDocument('1.0', 'iso-8859-1');
       
$oRoot = $oDom->createElement('phpdraw');
       
$oDom->appendChild($oRoot);
       
$oFile = $oDom->createElement('file', substr('../tmpimg/'.$sFile, 3));
       
$oX = $oDom->createElement('x', $oInterpreter->X);
       
$oY = $oDom->createElement('y', $oInterpreter->Y);
       
$oColor = $oDom->createElement('color', $oInterpreter->COLOR);
       
$oThickNess = $oDom->createElement('thickness', $oInterpreter->PIXEL);
       
$oFontSize = $oDom->createElement('fontsize', $oInterpreter->FONTSIZE);
       
$oRoot->appendChild($oFile);
       
$oRoot->appendChild($oX);
       
$oRoot->appendChild($oY);
       
$oRoot->appendChild($oColor);
       
$oRoot->appendChild($oThickNess);
       
$oRoot->appendChild($oFontSize);
       
        echo
utf8_decode($oDom->saveXML());
    } catch(
Exception $e) {
       
$oDom = new DOMDocument('1.0', 'iso-8859-1');
       
$oRoot = $oDom->createElement('phpdraw');
       
$oDom->appendChild($oRoot);
       
$oErr = $oDom->createElement('error', $e);
       
$oRoot->appendChild($oErr);
       
        echo
utf8_decode($oDom->saveXML());
    }
}
?>


Details

PHPDRAW, the wannabe Photoshop ;-) This application is only a fun application, and cannot probably be of any other use than having fun and be educationnal; it allows you to draw an image using commands just like we used to with good old LOGO :-) Its use is easy : a command followed by a space and its parameter(s). Here are all the commands : right, left, down, up followed by an integer in pixel : draws a line. pixel followed by an integer in pixel : sets pixels thickness. color followed by an integer 9 characters long (rrrgggbbb) : sets the color of your next commands. x, y followed by an integer in pixel : moves to the X, Y position (command is not x, y...command is either x followed by its position, or y followed by its position) ellipse followed by 2 integers in pixel separated by the character - : draws an ellipse using current X and Y position for the start. filledellipse followed by 2 integers in pixel separated by the character - : same as above, but for a filled ellipse. filledarc followed by 4 integers in pixel separated by the character - : same as above, bu for a filled arc. rectangle followed by 2 integers in pixel separated by the character - : draws a rectangle. polygon followed by N integers in pixel separated by the character _ : draws a polygon. Beware : N must be even. fontsize followed by an integer between 1 and 5 : sets the font size. text followed by a string delimited by the character " : draws the text (example : text "Hello World") file followed by a string: saves your masterpiece to a file named after the given string :-) Some examples : right 40 draws a line going to the right and of 40 pixels long x 10 moves to position x = 10 color 255000000 sets color to red rectangle 40-40 draws a rectangle which upper left starts at X-Y current position and bottom right ends at 40-40 polygon 40-40-100-120-10-20 draws a triangle (3 summits, because we have 3 pairs of integers) And of course, you can use a chain of commands, each of them will be interpreted one after the other : color 000000255 right 40 pixel 2 up 60 fontsize 4 text "Hello World" file myMasterPiece.png

  Files folder image Files (9)  
File Role Description
Files folder imageajax (1 file)
Files folder imageclass (2 files)
Files folder imageinc (1 file)
Files folder imagejs (2 files)
Plain text file doc_en.txt Doc. English doc
Plain text file doc_fr.txt Doc. French doc
Plain text file index.html Data index file

  Files folder image Files (9)  /  ajax  
File Role Description
  Accessible without login Plain text file ajax.draw.php Example Ajax script

  Files folder image Files (9)  /  class  
File Role Description
  Plain text file class.exceptions.php Class Exceptions classes
  Plain text file class.interpreter.php Class PHPDRAW engine classe

  Files folder image Files (9)  /  inc  
File Role Description
  Plain text file inc.main.php Aux. Definitions and inclusions

  Files folder image Files (9)  /  js  
File Role Description
  Plain text file phpdraw.js Data Javascript for phpdraw GUI
  Plain text file prototype.js Data Prototype

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,334
This week:0
All time:2,901
This week:206Up
 User Ratings  
 
 All time
Utility:41%StarStarStar
Consistency:66%StarStarStarStar
Documentation:75%StarStarStarStar
Examples:66%StarStarStarStar
Tests:-
Videos:-
Overall:41%StarStarStar
Rank:3708