Back to Image class

Method __construct

public
__construct
(mixed $source = null)
Class constructor.
Parameters
  • mixed $source Either a file path for a source image or a GD resource handler for an image.
Since
  • 1.7.3
-
  • \RuntimeException
Class: Image
Project: Joomla

Method __construct - Source code

/**
 * Class constructor.
 *
 * @param   mixed  $source  Either a file path for a source image or a GD resource handler for an image.
 *
 * @since   1.7.3
 * @throws  \RuntimeException
 */
public function __construct($source = null)
{
    // Verify that GD support for PHP is available.
    if (!\extension_loaded('gd')) {
        // @codeCoverageIgnoreStart
        throw new \RuntimeException('The GD extension for PHP is not available.');
        // @codeCoverageIgnoreEnd
    }
    // Determine which image types are supported by GD, but only once.
    if (empty(static::$formats)) {
        $info = gd_info();
        static::$formats[IMAGETYPE_JPEG] = $info['JPEG Support'];
        static::$formats[IMAGETYPE_PNG] = $info['PNG Support'];
        static::$formats[IMAGETYPE_GIF] = $info['GIF Read Support'];
        static::$formats[IMAGETYPE_WEBP] = $info['WebP Support'];
    }
    /**
     * If the source input is a resource, set it as the image handle.
     * @todo: Remove check for resource when we only support PHP 8
     */
    if ($source && (\is_object($source) && get_class($source) == 'GdImage') || \is_resource($source) && get_resource_type($source) == 'gd') {
        $this->handle = $source;
    } elseif (!empty($source) && \is_string($source)) {
        // If the source input is not empty, assume it is a path and populate the image handle.
        $this->loadFile($source);
    }
}