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

declare(strict_types=1);

namespace 
InterventionImageDrivers;

use 
InterventionImageEncodedImage;
use 
InterventionImageExceptionsLogicException;
use 
InterventionImageExceptionsStreamException;
use 
InterventionImageExceptionsInvalidArgumentException;
use 
InterventionImageInterfacesEncodedImageInterface;
use 
InterventionImageInterfacesEncoderInterface;
use 
InterventionImageInterfacesImageInterface;
use 
InterventionImageInterfacesSpecializedInterface;
use 
InterventionImageTraitsCanBuildStream;

abstract class 
AbstractEncoder implements EncoderInterface
{
    use 
CanBuildStream;

    
/**
     * Default encoding quality.
     */
    
public const int DEFAULT_QUALITY 75;

    
/**
     * {@inheritdoc}
     *
     * @see EncoderInterface::encode()
     *
     * @throws LogicException
     */
    
public function encode(ImageInterface $image): EncodedImageInterface
    
{
        if (
$this instanceof SpecializedInterface) {
            throw new 
LogicException(
                
"Specialized class '" . static::class . "' must override encode()",
            );
        }

        return 
$image->encode($this);
    }

    
/**
     * Build new stream, run callback with it and return result as encoded image.
     *
     * @throws InvalidArgumentException
     * @throws StreamException
     */
    
protected function createEncodedImage(callable $callback, ?string $mediaType null): EncodedImage
    
{
        
$stream self::buildStreamOrFail();
        
$callback($stream);

        return 
is_string($mediaType) ? new EncodedImage($stream$mediaType) : new EncodedImage($stream);
    }

    
/**
     * {@inheritdoc}
     *
     * @see EncoderInterface::setOptions()
     *
     * @throws InvalidArgumentException
     */
    
public function setOptions(mixed ...$options): self
    
{
        foreach (
$options as $key => $value) {
            if (!
property_exists($this, (string) $key)) {
                throw new 
InvalidArgumentException(
                    
'Option $' $key ' does not exist on ' $this::class,
                );
            }
            
$this->{$key} = $value;
        }

        return 
$this;
    }
}
Онлайн: 1
Реклама