formats.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. File formats
  2. ============
  3. .. contents::
  4. :local:
  5. Input formats
  6. -------------
  7. Files are read from a file or URL by using the :meth:`Image::fromFile()` function:
  8. .. code-block:: php
  9. use Mike42\GfxPhp\Image;
  10. $tux = Image::fromFile("tux.png")
  11. If the your image is not being read from a file, then :meth:`Image::fromBlob()` can load it from a binary string:
  12. .. code-block:: php
  13. use Mike42\GfxPhp\Image;
  14. $tuxStr = "...";
  15. $tux = Image::fromBlob($tuxStr, "tux.png");
  16. In either case, the input format is determined using the file's `magic number`_.
  17. .. _magic number: https://en.wikipedia.org/wiki/Magic_number_(programming)
  18. PNG
  19. ^^^
  20. The PNG codec is used where the input has the ``png`` file extension.
  21. All valid PNG files can be read, including:
  22. - RGB or RGBA images
  23. - Indexed images
  24. - Monochrome images, from 1 to 16 bits per pixel
  25. - Interlaced images
  26. This library currently has limited support for transparency, and will discard any alpha channel from a PNG file when it is loaded.
  27. GIF
  28. ^^^
  29. The GIF codec is used where the input has the ``gif`` file extension. Any well-formed GIF file can be read, but there are some limitations:
  30. - If a GIF file contains multiple images, then only the first one will be loaded
  31. - If a transparent color is present, then this will be mixed to white
  32. A GIF image will always be loaded into an instance of :class:`IndexedRasterImage`, which makes palette information available.
  33. BMP
  34. ^^^
  35. The BMP codec is used where the input has the ``bmp`` or ``dib`` file extensions. Most uncompressed or run-length encoded bitmap files can be read by this library.
  36. The returned object will be an instance of instance of :class:`IndexedRasterImage` if the color depth is 8 or lower.
  37. 24-bit, 16-bit and 32-bit bitmap images are also supported, and will be loaded into an instance of :class:`RgbRasterImage`. If an alpha channel is used, then it will be mixed to white.
  38. Netpbm Formats
  39. ^^^^^^^^^^^^^^
  40. The Netpbm formats are a series of uncompressed bitmap formats, which can represent most types of image. These formats can be read by ``gfx-php``:
  41. :PNM: This is a file extension only. Files carrying ``.pnm`` extension can carry any of the below formats.
  42. :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 :class:`RgbRasterImage`.
  43. :PGM: This is a monochrome raster format. A PGM file is identified by the P5 magic number, and will be loaded instance of :class:`GrayscaleRasterImage`.
  44. :PBM: This is a 1-bit bitmap format. A PBM file is identified by the P4 header, and loaded into an instance of :class:`BlackAndWhiteRasterImage`.
  45. Each of these formats has both a binary and text encoding. ``gfx-php`` only supports the binary encodings at this stage.
  46. WBMP
  47. ^^^
  48. The WBMP codec is used where the input has the ``wbmp`` file extension. A WBMP image will always be loaded into a :class:`BlackAndWhiteRasterImage` object.
  49. Output formats
  50. --------------
  51. When you write a :class:`RasterImage` to a file, you need to specify a filename. The extension on this file is used to determine the desired output format.
  52. There is currently no mechanism to write a file directly to a string.
  53. PNG
  54. ^^^
  55. The PNG format is selected by using the ``png`` file extension when you call :func:`RasterImage::write()`.
  56. .. code-block:: php
  57. $tux -> write("tux.png");
  58. This library will currently output PNG files as RGB data. If you write to PNG from an instance of :class:`RgbRasterImage`, then no conversion has to be done, so the output is significantly faster.
  59. GIF
  60. ^^^
  61. The GIF format is selected by using the ``gif`` file extension.
  62. .. code-block:: php
  63. $tux -> write("tux.gif");
  64. This format is limited to using a 256-color palette.
  65. - If your image is not an `IndexedRasterImage`, then it is indexed when you write.
  66. - 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.
  67. When you are creating GIF images, then you can avoid these conversions by using a :class:`IndexedRasterImage` with a palette of fewer than 256 colors.
  68. There is no encoder for multi-image GIF files at this stage.
  69. BMP
  70. ^^^
  71. The BMP format is selected by using the ``bmp`` file extension.
  72. .. code-block:: php
  73. $tux -> write("tux.bmp");
  74. This library will currently output BMP files using an uncompressed 24-bit RGB representation of the image.
  75. WBMP
  76. ^^^
  77. The WBMP format is selected by using the ``wbmp`` file extension.
  78. .. code-block:: php
  79. $tux -> write("tux.wbmp");
  80. The image will be converted to a 1-bit monochrome representation, which is the only type of image supported by WBMP.
  81. Netpbm Formats
  82. ^^^^^^^^^^^^^^
  83. The Netpbm formats can be used for output. Each format is identified by their respective file extension:
  84. .. code-block:: php
  85. $tux -> write("tux.ppm");
  86. $tux -> write("tux.pgm";
  87. $tux -> write("tux.pbm");
  88. Since each of these formats has a different raster data representation, you should be aware that
  89. :PPM: For this output format, the file is converted to a :class:`RgbRasterImage` and typically written with a 24 bit color depth. In some cases, a 48 bit color depth will be used.
  90. :PGM: The file is converted to a :class:`GrayscaleRasterImage` and written with a depth of 8 or 16 bits per pixel.
  91. :PPM: The file is converted to a :class:`BlackAndWhiteRasterImage` and written with 1 bit per pixel.
  92. 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.
  93. .. code-block:: php
  94. $tux -> write("tux.pnm");