Proposal for "Image_Canvas"

» Metadata » Status
» Description

Image_Canvas

The idea with this package is to provide a common interface to different image formats. This is achieved by using a strategy pattern in the form of a driver with a set of primitives. These primitives are selected to provide as much functionality for each format as possible, while still retaining a simple and common interface.

The code is based upon the driver mechanism as implemented in Image_Graph. However there is no doubt that this implementation has to be refined.

Justification

As mentioned this idea is already in use by Image_Graph and could be used with advantage by other packages (for example the proposed & accepted but not yet released Image_Isometric and the new cool package proposal Image_3D).

This not only provides more functionality it also makes the packages (i.e. Image_Graph/Image_Isometric) being independent upon the actual graph library API. The packages will have a common interface to _all_ the supported output formats. The proposed solution is not a method to replace these libraries, i.e. the possibility to output in PNG without GD or SWF without Ming (fx).

Introducing such a mechanism also makes it possible for these packages to "interact", i.e. Image_Graph can use Image_Isometric to create 3D graphs, simply y using a common canvas!

Currently Supported Drawing-Functions

The current implementation/version supports the following drawing functions (I'm sure somebody has input for more!!!):

  • Line
  • Rectangle
  • Polygon
  • Spline
  • Ellipse
  • Pie slice
  • Text output
  • Image overlay ("draw" an image)

Supported styles are (also depending on output format):

  • Line styles
    • Solid
    • Thickness
    • "Patterned"
  • Fills
    • Solid
    • Alpha blending
    • Image fill
    • Gradients
  • Fonts
    • Mostly true-types (for server-side at least)

Possibility to output to file or browser.

Example usage

This is but a short example:
<php>
require_once 'Image/Canvas.php';

$Canvas =& Image_Canvas::factory('png', array('width' => 400, 'height' => 300, 'transparent' => true));
$Canvas->setLineColor('red');
$Canvas->line(0, 0, 100, 100);
$Canvas->setFont(array('name' => 'Gothic', 'size' => 10, 'color' => 'orange'));
$Canvas->write(50, 50, 'This is a test', array('horizontal' => 'center', 'vertical' => 'center'));
$Canvas->done();

$Canvas =& Image_Canvas::factory('svg', array('width' => 600, 'height' => 400));
$Canvas->setLineColor('gray');
$Canvas->setGradientFill(array('direction' => 'horizontal', 'start' => 'blue', 'end' => 'red'));
$Canvas->rectangle(0, 0, 100, 100);
$Canvas->done(array('filename' => './output.svg'));
</php>

As mentioned this is the implementation/code as adopted by Image_Graph.

» Dependencies » Links
  • Image_Color
  • gd
» Timeline » Changelog
  • First Draft: 2005-02-14
  • Proposal: 2005-02-14
  • Call for Votes: 2005-07-25
  • Jesper Veggerby
    [2005-03-19 18:58 UTC]

    Changed description to accomodate Philippe's comment

  • Jesper Veggerby
    [2005-03-21 19:12 UTC]

    Changed name to Image_Canvas as suggested by Alan.

  • Jesper Veggerby
    [2005-07-20 10:40 UTC]

    Added links to (some/main) source files

  • Jesper Veggerby
    [2005-07-21 20:47 UTC]

    Updated source with requests by toby:

    • Changed write() to addText()
    • Split done() into show() and save()
    • Changed all methods with more than 1 parameter to use hashed array as parameters