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

declare(strict_types=1);

namespace 
InterventionImageDriversGdDecoders;

use 
InterventionImageDriversSpecializableDecoder;
use 
InterventionImageExceptionsDirectoryNotFoundException;
use 
InterventionImageExceptionsFileNotFoundException;
use 
InterventionImageExceptionsFileNotReadableException;
use 
InterventionImageExceptionsImageDecoderException;
use 
InterventionImageExceptionsInvalidArgumentException;
use 
InterventionImageExceptionsNotSupportedException;
use 
InterventionImageInterfacesSpecializedInterface;
use 
InterventionImageMediaType;
use 
InterventionImageTraitsCanParseFilePath;
use 
TypeError;
use 
ValueError;

abstract class 
AbstractDecoder extends SpecializableDecoder implements SpecializedInterface
{
    use 
CanParseFilePath;

    
/**
     * Return media (mime) type of the file at given file path
     *
     * @throws InvalidArgumentException
     * @throws ImageDecoderException
     * @throws NotSupportedException
     * @throws DirectoryNotFoundException
     * @throws FileNotFoundException
     * @throws FileNotReadableException
     */
    
protected function mediaTypeByFilePath(string $filepath): MediaType
    
{
        
$filepath self::readableFilePathOrFail($filepath);

        if (
function_exists('finfo_file') && function_exists('finfo_open')) {
            
$mediaType finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);
            if (
is_string($mediaType)) {
                try {
                    return 
MediaType::from($mediaType);
                } catch (
ValueError TypeError) {
                    throw new 
NotSupportedException('Unsupported media type (MIME) ' $mediaType '.');
                }
            }
        }

        
$info = @getimagesize($filepath);

        if (!
is_array($info)) {
            throw new 
ImageDecoderException('Failed to read media (MIME) type from data in file path');
        }

        try {
            return 
MediaType::from($info['mime']);
        } catch (
ValueError TypeError) {
            throw new 
NotSupportedException('Unsupported media type (MIME) ' $info['mime'] . '.');
        }
    }

    
/**
     * Return media (mime) type of the given image data
     *
     * @throws ImageDecoderException
     * @throws NotSupportedException
     */
    
protected function mediaTypeByBinary(string $data): MediaType
    
{
        if (
function_exists('finfo_buffer') && function_exists('finfo_open')) {
            
$mediaType finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
            if (
is_string($mediaType)) {
                try {
                    return 
MediaType::from($mediaType);
                } catch (
ValueError TypeError) {
                    throw new 
NotSupportedException('Unsupported media type (MIME) ' $mediaType '.');
                }
            }
        }

        
$info = @getimagesizefromstring($data);

        if (!
is_array($info)) {
            throw new 
ImageDecoderException('Failed to read media (MIME) type from binary data');
        }

        try {
            return 
MediaType::from($info['mime']);
        } catch (
ValueError TypeError) {
            throw new 
NotSupportedException('Unsupported media type (MIME) ' $info['mime'] . '.');
        }
    }
}
Онлайн: 1
Реклама