Canvas

Canvas is the core of Jaguar which makes applying filters , drawing shapes and loading images from files or strings possible.it the warpper for all kinds of supported formats in Jaguar which are :

  • Jpeg : Support jpeg format
  • Png : Support png format
  • Gif : Support gif format
  • Gd : Support Gd2 format

Probably you will never need to deal directly with any of those formats but you will only deal with Canvas object which is smart enough to guess the right format for a given image file or image string . But if you want to declare the formate by your own you can do it is straightforward

Creating New Canvas

The folowing example demonstrates creating a png canvas (w=100,h=100) filled with red color

<?php

use Jaguar\Canvas,
        Jaguar\Dimension,
        Jaguar\Color\RGBColor;

// We want png canvas (by default png is the default format )
$canvas = new Canvas(
        new Dimension(100, 100)
        , Canvas::Format_PNG
);

$canvas->fill(new RGBColor(255))
           // save the canvas to the given path
           ->save('/save/path')
   // send the canvas to browser
           ->show();

Output :

../_images/red-canvas.png

Loading Canvas From Files Or Strings

As you can see Jaguar’s canvas can create new image with no effort and also it can load images from files and string easily.

The following example will demonstrate loading images from a file.

<?php

use Jaguar\Canvas;

// No need to define the format
// canvas will make a guess for you
// and find the right format
$canvas = new Canvas('images/canvas-1.jpg');

$canvas->save('/save/canvas-1.jpg')
                ->show();

Output :

../_images/canvas-1.jpg

Adding New Foramts

Canvas object is not limited to the current formats and it can be extended to support more by creating a new canvas which implements CanvasInterface or exteds the AbstractCanvas and a new format which implements the CanvasFactory

The following example demonstrates createing a new format called Foo

<?php

use Jaguar\AbstractCanvas,
        Jaguar\CanvasFactory,
        Jaguar\Canvas;

/**
 * Foo Format
 */
class Foo extends AbstractCanvas
{

        protected function doLoadFromFile($file)
        {
                //put your code here
        }

        protected function doSave($path)
        {
                //put your code here
        }

        protected function getToStringProperties()
        {
                return array();
        }

}

/**
 * Foo Format Factory
 */
class FooFactory implements CanvasFactory
{

        public function getCanvas()
        {
                return new Foo();
        }

        public function getExtension($includeDot = true)
        {
                return ($includeDot) ? '.foo' : 'foo';
        }

        public function getMimeType()
        {
                return 'image/foo';
        }

        public function isSupported($file)
        {
                // check if the Foo format can handle
                // the given file
        }

}

/* add my new format to the canvas */
$canvas = new Canvas();
$canvas->addFactory('Foo', new FooFactory());

print_r($canvas->getFactories());

Output :

Array
(
        [factory.jpeg] => Jaguar\Factory\JpegFactory Object()
        [factory.gif] => Jaguar\Factory\GifFactory Object()
        [factory.png] => Jaguar\Factory\PngFactory Object()
        [factory.gd2] => Jaguar\Factory\GdFactory Object()
        [Foo] => FooFactory Object()

)
Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.