Creating Editors

The editors are use to manipulate images. The recommended way is to use Grafika::createEditor(). It automatically selects the best editor available. It will check if Imagick is available. If not, it will fall back to using GD.

use Grafika\Grafika; // Import package

$editor = Grafika::createEditor(); // Create the best available editor

Imagick Editor

You can also use the Imagick editor only.

use Grafika\Imagick\Editor; // Import package

$editor = new Editor(); // Imagick editor

Be careful when using the Imagick editor as some PHP installs dont have it by default. You need to add some safety checks:

use Grafika\Imagick\Editor; // Import package

$editor = new Editor(); // Imagick editor

if( $editor->isAvailable() ) { // Safety check

    // Your code here

}

GD Editor

You can also use the GD editor only.

use Grafika\Gd\Editor; // Import package

$editor = new Editor(); // Gd editor

if( $editor->isAvailable() ) { // Safety check

    // Your code here

}

Try..Catch Statement

You can also wrap the code inside a try..catch statement to catch all possible errors. You don't need to use isAvailable.

use Grafika\Grafika; // Import package

try {

    $editor = Grafika::createEditor(); // Create best available editor

    // Do something

} catch (Exception $e){ // Catch exceptions for safety
    echo $e->getMessage();
}

Change Editor Order

You can change the order of editor evaluation. For example, to always check for GD first:

use Grafika\Grafika; // Import package

try {

    $editor = Grafika::createEditor( array('Gd', 'Imagick') ); // Create best available editor

    // Do something

} catch (Exception $e){ // Catch exceptions for safety
    echo $e->getMessage();
}

However, you might not need the code above as GD is mostly available. You can just create a GD editor directly.

Change Editor Order Globally

The previous code will only change the order of editor evaluation one time for that function call. Sometimes it is useful to change the order of evaluation globally. For example if you prefer to use GD when creating drawing objects or filters. Below, all succeeding function calls will use the new order of evaluation:

use Grafika\Grafika; // Import package

try {

    Grafika::setEditorList(array('Gd', 'Imagick')); // Change order globally

    // createFilter and createDrawingObject will now use the new order
    $dither = Grafika::createFilter('Dither');
    $line = Grafika::createDrawingObject('Line', array(100, 0), array(100, 200))

} catch (Exception $e){ // Catch exceptions for safety
    echo $e->getMessage();
}