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
- PHP version 5+
- GD library installed and configured on the webserver
- PHP 5.2+ if you are planning to use filters
- PHP compiled with truetype support if you want to use special fonts on your image texts
Examples (coming soon!)
Troubleshooting
If you are having trouble getting Canvas to work, please check the following list before asking for help:
- Are you using the correct PHP version (5+)?
- Is the GD library enabled and configured on your web server?
- Do you have write permissions? (in case you are trying to save some image)
- 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:
- DHKold - imagecreatefrombmp
- Estefanio Nunes - Utility belt
- Fernando VR - proportional resize fixes
- Gilton Guma - resize fixes
- Giolvani - image merge positions
- Noah Winecoff - crop and flip
- Patrick Kaminski - oraculum framework
- ... and everybody else that mailed me suggestions and commented on both posts about Canvas! :)
Aller font by Dalton Maag, interface elements by Wijmo.