Banksky!

Canvas is a PHP class to manipulate images, including resize functions, filters, crop, conversion and method chaining.

How to use

The code below will load an image file called "image.jpg", resize it to 30% of its width and convert it to grayscale. Once it's all done, the image gets displayed on the screen.

<?php 
require_once "canvas.php";
$img = new canvas;
$img->load("image.jpg")->resize("30%")->filter("grayscale")->show();

Next one will load the file called "banksky3.jpg" located at the "img" directory (this time using the class constructor), resize with crop (setting manually the crop coordinates), add some credits text to the image and finally save it as a new image, with a different format ("new_image.png" at "/tmp" dir).

<?php 
require_once "canvas.php";
$img = new canvas("img/banksky3.jpg");
$img->set_crop_coordinates(-400, -410)
    ->resize("938", "240", "crop")
    ->filter("grayscale")
    ->text("banksky.co.uk", array(
           "color" => "#000",
           "background_color" => "#ffff00", 
           "size" => 4, 
           "x" => "right", 
           "y" => "top"))
    ->save("/tmp/new_image.png");

For a full list of features, check the

Requirements

Examples (coming soon!)

Troubleshooting

If you are having trouble getting Canvas to work, please check the following list before asking for help:

  1. Are you using the correct PHP version (5+)?
  2. Is the GD library enabled and configured on your web server?
  3. Do you have write permissions? (in case you are trying to save some image)
  4. Is your script returning anything before the image you are trying to show?

Also, for debugging purposes, enable PHP error reporting on your script:

error_reporting(E_ALL ^ E_NOTICE ^ E_USER_NOTICE);
ini_set( 'display_errors',1 );
ini_set( 'display_startup_errors', true );

If you are still experiencing trouble, please drop a new issue at GitHub and I'll take a look at it! You can also follow me on twitter for updates and help.

Who is using it

Credits

Canvas PHP image manipulation class written by Davi Ferreira.

I would like to thank the following people for helping me out with this class along the way:

Aller font by Dalton Maag, interface elements by Wijmo.