Custom Image Formats
If you want to support a custom image format, you have to declare a "file mapper" class for each format. It's role is to map images to files and vice versa. The class has to conform to a few simple rules.
Naming the class
You have to name the class properly:
wiImageFileMapper_FORMAT. So, if you want to support
tga files, you have to name the class wiImageFileMapper_TGA.
WideImage will automatically create and use the mapper.
Methods
In order for the mapper to work, a few methods must be defined properly; a parameterless constructor, load method and save method.
load()
- Signature: load($uri)
- Parameters:
- $uri string - URI (file or URL) of an image
- Return value: null or a valid GD image resource, like one created with imagecreate()∞ or imagecreatetruecolor()∞.
save()
- Signature: save($handle, $uri = null [, custom parameters])
- Parameters:
- $handle resource - a GD image resource
- $uri string/null - target file. Null for no file (mapper must output image directly).
- custom parameters - whatever you define
- Return value: none.
Example of a File Mapper
class wiImageFileMapper_PCX
{
function load
($uri)
{
$data =
file_get_contents($uri);
// determine width & height from the file header
$handle = imagecreatetruecolor
($width,
$height);
// read pixels from the file data and write them to $handle
return $handle;
}
// define some custom parameters with default values
function save
($handle,
$uri =
null,
$quality =
100,
$interlaced =
false)
{
if ($uri ==
null)
{
// output image
}
else
{
// write to file
$fp =
fopen($uri,
'w');
// write to file
fclose($fp);
}
}
}
And now let's use it:
$img = wiImage::
load('/path/to/image.pcx');
// do stuff
// save to a file, $quality=70, $interlaced=true
$img->
saveToFile('/path/to/new_image.pcx',
70,
true);
// or output the file directly, $quality=90, use default value for $interlaced
echo $img->
asString('pcx',
90);
There are no comments on this page. [Add comment]