Files are read from a file or URL by using the
use Mike42\GfxPhp\Image; $tux = Image::fromFile("tux.png")
If the your image is not being read from a file, then
Image::fromBlob() can load it from a binary string:
use Mike42\GfxPhp\Image; $tuxStr = "..."; $tux = Image::fromBlob($tuxStr, "tux.png");
In either case, the input format is determined using the file’s magic number.
The PNG codec is used where the input has the
png file extension.
All valid PNG files can be read, including:
- RGB or RGBA images
- Indexed images
- Monochrome images, from 1 to 16 bits per pixel
- Interlaced images
This library currently has limited support for transparency, and will discard any alpha channel from a PNG file when it is loaded.
The Netpbm formats are a series of uncompressed bitmap formats, which can represent most types of image. These formats can be read by
|PNM:||This is a file extension only. Files carrying
|PPM:||This is a color raster format. A PPM file is identified by the P6 magic number, and will be loaded into an instance of
|PGM:||This is a monochrome raster format. A PGM file is identified by the P5 magic number, and will be loaded instance of
|PBM:||This is a 1-bit bitmap format. A PBM file is identified by the P4 header, and loaded into an instance of
Each of these formats has both a binary and text encoding.
gfx-php only supports the binary encodings at this stage.
When you write a
RasterImage to a file, you need to specify a filename. The extension on this file is used to determine the desired output format.
There is currently no mechanism to write a file directly to a string.
The PNG format is selected by using the
png file extension when you call
$tux -> write("tux.png");
This library will currently output PNG files as RGB data. If you write to PNG from an instance of
RgbRasterImage, then no conversion has to be done, so the output is significantly faster.
The GIF format is selected by using the
gif file extension.
$tux -> write("tux.gif");
This format is limited to using a 256-color palette.
- If your image is not an IndexedRasterImage, then it is indexed when you write.
- If the image uses more than 256 colors, then it will be converted to an 8-bit RGB representation (3 bits red, 3 bits green, 2 bits blue), which introduces some distortions.
When you are creating GIF images, then you can avoid these conversions by using a
IndexedRasterImage with a palette of fewer than 256 colors.
There is no encoder for multi-image GIF files at this stage.
The BMP format is selected by using the
bmp file extension.
$tux -> write("tux.bmp");
This library will currently output BMP files using an uncompressed 24-bit RGB representation of the image.
The Netpbm formats can be used for output. Each format is identified by their respective file extension:
$tux -> write("tux.ppm"); $tux -> write("tux.pgm"; $tux -> write("tux.pbm");
Since each of these formats has a different raster data representation, you should be aware that
|PPM:||For this output format, the file is converted to a
|PGM:||The file is converted to a
|PPM:||The file is converted to a
If you want to avoid these conversions, then you should use the
pnm extension to write your files. Since files with this extension can hold any of the above formats, the output encoder will avoid converting the raster data where possible.
$tux -> write("tux.pnm");