The standard way to deal with images and mainly icons in php-gtk is
using XPMs.
XPM is short for XPixMap and describes an ASCII image format
as well as a C library for dealing with the images. Originally the format
was developed because the X libraries just had support for 1 bit depth
images, the XBM (X BitMap) which is just not enough for applications on
a graphical system. Although it is has not been standardized by the X
Consortium, it is the de facto standard for icons in X.
In this tutorial we will omit the library because we're programming in
php and not in C.
An icon in XPM format is just an ASCII file which can be edited with
a normal text editor. As it was designed to be included in C sources,
the only content is a C styled array with the image information.
You can even have comments in the file - it's C source.
The basic format of a XPM is an array composed as follows:
/* XPM */
static char * <pixmap name>[] = {
"Values-string",
"Colors-strings",
"Pixel-strings"
}; |
Note that the header comment with only the word XPM in it is required.
I leave out the Extension strings as we don't need them here.
The Values-string has to contain four integer values:
- Width of the image in pixels
- Height of the image in pixels
- Number of colors in the image
- Number of characters per pixel
Example:
"16 16 8 1" defines an icon of size 16x16 pixels with 8
colors and 1 char for defining one pixel.
First it may be strange that one has to define the number of characters per
pixel, but this becomes clear if you consider the following:
Each pixel is represented by a char, e.g. "b", with "b" having the color blue. Now
you want another pixel in red, so you give it the char "r" and define "r" being the
red color. The ASCII standard defines 128 chars, and with an extended codepage of 255
you would still be limited to somewhat of 250 colors (you can't use quotes and some other
chars). To avoid this limitation you can use more than one char to describe a pixel
and a color: If you use 2 chars per pixel it is possible to use about 250x250 = 62500
colors by defining pairs of chars to be one color, e.g. "mr" being a medium red and
'db' being a dark blue.
The Colors-strings contains as many strings as there are colors.
Each string has the following format:
"chars key value [key value]*" |
The chars value is one or more chars (depending on the
number of characters setting) which define the "pixel name"
of the color. A key can have one of the following values:
- m for a monochrome visual
- c for a color visual
- g4 for 4-level grayscale
- g for grayscale with more than 4 levels
- s for a symbolic name
The color
value itself can be the following:
- A color name, e.g. "blue"
- A hexadecimal RGB code like in HTML, e.h. '#FF8000'
- The string "None" which means the transparent color
Different key-value pairs can be concatenated to optimize the image for
several visuals
"r m white c red",
"b m black c blue" |
The example would define two colors: A color named "r" which is displayed white
on a monochrome display and red on a color display. The second is named "b" and
is displayed black on a monochrome visual and blue on a color one.
The Pixel-strings are height string of
which each is width * chars per pixel wide.
例 2.1. A small sample XPM
/* XPM */
static char * dot[] = {
"5 5 2 1",
". c None",
"X c Red",
" X ",
" XXX ",
"XXXXX",
" XXX ",
" X ",
}; |
As you probably don't want to create these images by hand, here is a
list of programs which support the creation of XPMs:
- The GIMP which should be on every linux system; it
is also available for Windows.
- ImageMagick is a command line tool which can be used
to batch-convert other image formats to xpm. Available for unix and Windows.