Вход Регистрация
Файл: vendor/intervention/image/src/Drivers/Gd/Driver.php
Строк: 171
<?php

declare(strict_types=1);

namespace 
InterventionImageDriversGd;

use 
GdImage;
use 
InterventionImageDriversAbstractDriver;
use 
InterventionImageExceptionsDriverException;
use 
InterventionImageExceptionsMissingDependencyException;
use 
InterventionImageExceptionsInvalidArgumentException;
use 
InterventionImageFormat;
use 
InterventionImageFileExtension;
use 
InterventionImageImage;
use 
InterventionImageInterfacesColorProcessorInterface;
use 
InterventionImageInterfacesCoreInterface;
use 
InterventionImageInterfacesFontProcessorInterface;
use 
InterventionImageInterfacesImageInterface;
use 
InterventionImageMediaType;

class 
Driver extends AbstractDriver
{
    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::id()
     */
    
public function id(): string
    
{
        return 
'GD';
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::checkHealth()
     *
     * @codeCoverageIgnore
     */
    
public function checkHealth(): void
    
{
        if (!
extension_loaded('gd') || !function_exists('gd_info')) {
            throw new 
MissingDependencyException(
                
'GD PHP extension must be installed to use this driver',
            );
        }
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::createImage()
     *
     * @throws InvalidArgumentException
     * @throws DriverException
     */
    
public function createImage(int $widthint $height): ImageInterface
    
{
        if (
$width || $height 1) {
            throw new 
InvalidArgumentException('Invalid image size. Only use int<1, max>');
        }

        
// build new transparent GDImage
        
$data imagecreatetruecolor($width$height);
        if (!
$data instanceof GDImage) {
            throw new 
DriverException('Failed to create new image');
        }

        
imagesavealpha($datatrue);
        
$background imagecolorallocatealpha($data255255255127);

        
imagealphablending($datafalse);
        
imagefill($data00$background);
        
imagecolortransparent($data$background);
        
imageresolution($data7272);

        return new 
Image($this, new Core([new Frame($data)]));
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::createCore()
     */
    
public function createCore(array $frames): CoreInterface
    
{
        return new 
Core($frames);
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::colorProcessor()
     */
    
public function colorProcessor(ImageInterface $image): ColorProcessorInterface
    
{
        return new 
ColorProcessor();
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::fontProcessor()
     */
    
public function fontProcessor(): FontProcessorInterface
    
{
        return new 
FontProcessor();
    }

    
/**
     * {@inheritdoc}
     *
     * @see DriverInterface::supports()
     */
    
public function supports(string|Format|FileExtension|MediaType $identifier): bool
    
{
        return 
match (Format::tryCreate($identifier)) {
            
Format::JPEG => boolval(imagetypes() & IMG_JPEG),
            
Format::WEBP => boolval(imagetypes() & IMG_WEBP),
            
Format::GIF => boolval(imagetypes() & IMG_GIF),
            
Format::PNG => boolval(imagetypes() & IMG_PNG),
            
Format::AVIF => boolval(imagetypes() & IMG_AVIF),
            
Format::BMP => boolval(imagetypes() & IMG_BMP),
            default => 
false,
        };
    }

    
/**
     * Return version of GD library
     */
    
public function version(): string
    
{
        return 
gd_info()['GD Version'];
    }
}
Онлайн: 1
Реклама