WideImage logo

WideImage Wiki : CustomImageFormats

MainPage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
SourceForge.net Logo

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()


save()


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]

WideImage - an Object-Oriented PHP Image Library for Image manipulation
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.2
Page was generated in 0.8232 seconds