Texture2D¶
The Texture2D class is part of the PHP-GLFW OpenGL extension. It loads images / textures from common formats like PNG, JPG, GIF, BMP, TGA etc.
and converts the raw bitmap to a GL\UByteBuffer instance. The GL\UByteBuffer instance can be uploaded to the GPU using the glTexImage2D function.
Usage¶
A very basic example of loading a file from disk and printing its metadata. Then creating A GPU texture and uploading the image data to the GPU.
// loading an image from disk (1024x1024)
$textureData = Texture2D::fromDisk(__DIR__ . '/picture.png');
echo $textureData->width(); // prints: 1024
echo $textureData->height(); // prints: 1024
echo $textureData->channels(); // prints: 4
// create & upload the texture on the GPU
glGenTextures(1, $texture);
glBindTexture(GL_TEXTURE_2D, $texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, $textureData->width(), $textureData->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, $textureData->buffer());
Read more about the glTexImage2D function to learn about the arguments.
Methods¶
fromDisk¶
Loads a texture / image from a file on disk and returns a Texture2D object.
- arguments
- 
- string- $fileThe path to the image file to load.
 
- returns
- 
\GL\Texture\Texture2DThe loaded texture object.
fromBuffer¶
Loads a texture / image from a buffer and returns a Texture2D object.
static function fromBuffer(int $width, int $height, \GL\Buffer\UByteBuffer $buffer, int $channels = \GL\Texture\Texture2D::CHANNEL_RGBA) : \GL\Texture\Texture2D
The buffer is not copied, the Texture2D object will hold a reference to the buffer given.
width¶
Returns the width of the image.
- returns
- 
intThe width of the image.
height¶
Returns the height of the image.
- returns
- 
intThe height of the image.
channels¶
Returns the number of channels of the image.
- returns
- 
intThe number of channels of the image.
buffer¶
Returns a reference to the internal UByteBuffer instance of the current texture.
- returns
- 
\GL\UByteBufferThe loaded image data.
writeJPG¶
Writes the image data to a file on disk. (JPEG)
- arguments
- 
- string- $fileThe path to the file to write to.
- int- $qualityThe quality of the image. (0 - 100)
 
- returns
- 
void
writePNG¶
Writes the image data to a file on disk. (PNG)
- arguments
- 
- string- $fileThe path to the file to write to.
 
writeBMP¶
Writes the image data to a file on disk. (BMP)
- arguments
- 
- string- $fileThe path to the file to write to.
 
writeTGA¶
Writes the image data to a file on disk. (TGA)
- arguments
- 
- string- $fileThe path to the file to write to.