Contao\GdImage::fromFile PHP Method

fromFile() public static method

Get the GD image object from an image file
public static fromFile ( contao\File $file ) : static
$file contao\File The file object
return static The GD image object
    public static function fromFile(File $file)
    {
        $extension = strtolower($file->extension);
        $function = null;
        if ($extension === 'jpg') {
            $extension = 'jpeg';
        }
        if (in_array($extension, array('gif', 'jpeg', 'png'))) {
            $function = 'imagecreatefrom' . $extension;
        }
        if ($function === null || !is_callable($function)) {
            throw new \InvalidArgumentException('Image type "' . $file->extension . '" cannot be processed by GD');
        }
        $image = $function(TL_ROOT . '/' . $file->path);
        if ($image === false) {
            throw new \RuntimeException('Image "' . $file->path . '" failed to be processed by GD');
        }
        return new static($image);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Tests the fromFile() method with an invalid type.
  *
  * @expectedException \InvalidArgumentException
  */
 public function testFromFileInvalidType()
 {
     GdImage::fromFile(new \File('test.xyz'));
 }