Back to Image class

Method toFile

public bool
toFile
(mixed $path, mixed $type = IMAGETYPE_JPEG, array $options = [])
Method to write the current image out to a file or output directly.
Parameters
  • mixed $path The filesystem path to save the image. When null, the raw image stream will be outputted directly.
  • int $type The image type to save the file as.
  • array $options The image type options to use in saving the file. For PNG and JPEG formats use `quality` key to set compression level (0..9 and 0..100)
Returns
  • bool
Since
  • 2.5.0
-
  • http://www.php.net/manual/image.constants.php
  • \LogicException
Class: Image
Project: Joomla

Method toFile - Source code

/**
 * Method to write the current image out to a file or output directly.
 *
 * @param   mixed    $path     The filesystem path to save the image.
 *                             When null, the raw image stream will be outputted directly.
 * @param   integer  $type     The image type to save the file as.
 * @param   array    $options  The image type options to use in saving the file.
 *                             For PNG and JPEG formats use `quality` key to set compression level (0..9 and 0..100)
 *
 * @return  boolean
 *
 * @link    http://www.php.net/manual/image.constants.php
 * @since   2.5.0
 * @throws  \LogicException
 */
public function toFile($path, $type = IMAGETYPE_JPEG, array $options = [])
{
    switch ($type) {
        case IMAGETYPE_GIF:
            return imagegif($this->getHandle(), $path);
        case IMAGETYPE_PNG:
            return imagepng($this->getHandle(), $path, \array_key_exists('quality', $options) ? $options['quality'] : 0);
        case IMAGETYPE_WEBP:
            return imagewebp($this->getHandle(), $path, \array_key_exists('quality', $options) ? $options['quality'] : 100);
    }
    // Case IMAGETYPE_JPEG & default
    return imagejpeg($this->getHandle(), $path, \array_key_exists('quality', $options) ? $options['quality'] : 100);
}