Back to Image class

Method loadFile

public void
loadFile
(mixed $path)
Method to load a file into the Image object as the resource.
Parameters
  • string $path The filesystem path to load as an image.
Returns
  • void
Since
  • 2.5.0
-
  • \InvalidArgumentException
  • \RuntimeException
Class: Image
Project: Joomla

Method loadFile - Source code

/**
 * Method to load a file into the Image object as the resource.
 *
 * @param   string  $path  The filesystem path to load as an image.
 *
 * @return  void
 *
 * @since   2.5.0
 * @throws  \InvalidArgumentException
 * @throws  \RuntimeException
 */
public function loadFile($path)
{
    // Destroy the current image handle if it exists
    $this->destroy();
    // Make sure the file exists.
    if (!is_file($path)) {
        throw new \InvalidArgumentException('The image file does not exist.');
    }
    // Get the image properties.
    $properties = static::getImageFileProperties($path);
    // Attempt to load the image based on the MIME-Type
    switch ($properties->mime) {
        case 'image/gif':
            // Make sure the image type is supported.
            if (empty(static::$formats[IMAGETYPE_GIF])) {
                throw new \RuntimeException('Attempting to load an image of unsupported type GIF.');
            }
            // Attempt to create the image handle.
            $handle = imagecreatefromgif($path);
            $type = 'GIF';
            break;
        case 'image/jpeg':
            // Make sure the image type is supported.
            if (empty(static::$formats[IMAGETYPE_JPEG])) {
                throw new \RuntimeException('Attempting to load an image of unsupported type JPG.');
            }
            // Attempt to create the image handle.
            $handle = imagecreatefromjpeg($path);
            $type = 'JPEG';
            break;
        case 'image/png':
            // Make sure the image type is supported.
            if (empty(static::$formats[IMAGETYPE_PNG])) {
                throw new \RuntimeException('Attempting to load an image of unsupported type PNG.');
            }
            // Attempt to create the image handle.
            $handle = imagecreatefrompng($path);
            $type = 'PNG';
            break;
        case 'image/webp':
            // Make sure the image type is supported.
            if (empty(static::$formats[IMAGETYPE_WEBP])) {
                throw new \RuntimeException('Attempting to load an image of unsupported type WebP.');
            }
            // Attempt to create the image handle.
            $handle = imagecreatefromwebp($path);
            $type = 'WebP';
            break;
        default:
            throw new \InvalidArgumentException('Attempting to load an image of unsupported type ' . $properties->mime);
    }
    /**
     * Check if handle has been created successfully
     * @todo: Remove check for resource when we only support PHP 8
     */
    if (!(\is_object($handle) || \is_resource($handle))) {
        throw new \RuntimeException('Unable to process ' . $type . ' image.');
    }
    $this->handle = $handle;
    // Set the filesystem path to the source image.
    $this->path = $path;
}