Custom Image Operations
If you want to use a custom image operation, you only have to declare an operation class somewhere in your code or autoloader. The class has to conform to a few simple rules.
Naming the class
You have to name the class properly:
wioOPERATION_NAME, for example wioMyOperation.
WideImage will automatically create and use the operation.
Methods
In order for the operation to work, two methods must be defined properly; a parameterless constructor and the execute method.
execute()
- Signature: execute($image[, custom parameters])
- Parameters:
- $image wiImage - The object to perform the operation on.
- custom parameters can be defined. They are passed from the user call.
- Return value must be a valid wiImage object (this means wiTrueColorImage or wiPaletteImage).
Note: The operation isn't supposed to modify the source image, it's supposed to return a newly created and modified image. This is not necessary, but it's a good and expected practice.
Example of an operation
Let's define a simple operation that draws a number of white points at random location on the image.
class wioDrawWhiteDots
{
function execute
($image,
$n =
5)
{
// make a copy
$new =
$image->
copy();
// allocate a white color
$white =
$new->
allocateColor(255,
255,
255);
// draw a point $n times
for ($i =
0;
$i <
$n;
$i++
)
{
// random position
$x =
rand(0,
$image->
getWidth() -
1);
$y =
rand(0,
$image->
getHeight() -
1);
// draw the point
$new->
setRGBAt($x,
$y,
$white);
}
return $new;
}
}
And now let's use it:
$img = wiImage::load('/path/to/image.png');
// let's do it
$modified = $img->drawWhiteDots();
// let's draw 100 white points instead of 5
$modified = $img->drawWhiteDots(100);
// of course, the custom operation can be chained as well
$modified = $img->resize(200, 200)->drawWhiteDots(70)->rotate(45);
There are no comments on this page. [Add comment]