Think C#, make PHP

How to create dynamic images with C#?



Lang.Php.Graph namespace contains Image class that represents PHP resource related to GD image that can be obtained using functions like imagecreatetruecolor. Image class is also container for image related methods.

On PHP side we have a number of functions with name starting from image prefix accepting GD image resource as first argument like this.

On C# side we can use instance methods usually with shorter names and with reduced argument list. For example imagecreatetruecolor equivalent is defined as:

And we can use this method like this:

Color, Font and other

Lets take a look at imagestringfunction defined as

This function accepts font and color as integer value, so it is easy to make mistake by putting any integer value that was not intended to be font or color.

To avoid this ambiguity Lang.Php.Graph namespace contains classes like Color or Font. By using this classes we can produce more reliable code even if on PHP side they are nothing more than integers. That’s why DrawString (imagestring equivalent) is defined as

How can we use Color class?
ColorAllocate method returns Color while its PHP prototype returns int (if success) or FALSE when action fails.
Meticulous approach to programming requires error checking. Color class contains static function IsValid, defined as

And its typical usage is

which is translated into


Code below is a part of example available on GitHub. Class DynamicImage can generate images like this




Installed with WPInstant