PHP Classes

File: docs/content/examples/

Recommend this page to a friend!
  Classes of Jose Luis Quintana   GImage   docs/content/examples/   Download  
File: docs/content/examples/
Role: Example script
Content type: text/markdown
Description: Example script
Class: GImage
Create graphic images with a fluent interface
Author: By
Last change:
Date: 8 months ago
Size: 1,012 bytes


Class file image Download

Image resource loading

The following example loads an image resource (PHP 7.4) or GdImage (PHP 8.x) using the load($src) method.

!!! tip "GdImage class"

Since PHP `8.0` onwards, a new `GdImage` class object replaces the GD image `resource`.<br>
See more details at


use GImage\Image;

// Create a image rectangle (240x100)
$width = 240;
$height = 100;
$rectangle = imagecreatetruecolor($width, $height);
$green = imagecolorallocate($rectangle, 0, 255, 0);
imagefilledrectangle($rectangle, 0, 0, $width, $height, $green);

// Load and process the rectangle image resource
$img = new Image();
  // Indicate the image type beforehand
  ->toPNG()// (1)!
  // scale to 50%
  // let's change again the image type just for fun

  1. It's necessary to tell `GImage` about the image type in those cases since the input is just a `resource` or `GdImage` object.