Вход Регистрация
Файл: gapps/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php
Строк: 323
<?php

namespace InterventionImage;

abstract class 
AbstractDecoder
{
    
/**
     * Initiates new image from path in filesystem
     *
     * @param  string $path
     * @return InterventionImageImage
     */
    
abstract public function initFromPath($path);

    
/**
     * Initiates new image from binary data
     *
     * @param  string $data
     * @return InterventionImageImage
     */
    
abstract public function initFromBinary($data);

    
/**
     * Initiates new image from GD resource
     *
     * @param  Resource $resource
     * @return InterventionImageImage
     */
    
abstract public function initFromGdResource($resource);

    
/**
     * Initiates new image from Imagick object
     *
     * @param Imagick $object
     * @return InterventionImageImage
     */
    
abstract public function initFromImagick(Imagick $object);

    
/**
     * Buffer of input data
     *
     * @var mixed
     */
    
private $data;

    
/**
     * Creates new Decoder with data
     *
     * @param mixed $data
     */
    
public function __construct($data null)
    {
        
$this->data $data;
    }

    
/**
     * Init from fiven URL
     *
     * @param  string $url
     * @return InterventionImageImage
     */
    
public function initFromUrl($url)
    {
        
        
$options = array(
            
'http' => array(
                
'method'=>"GET",
                
'header'=>"Accept-language: enrn".
                
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2rn"
          
)
        );
        
        
$context  stream_context_create($options);
        

        if (
$data = @file_get_contents($urlfalse$context)) {
            return 
$this->initFromBinary($data);
        }

        throw new 
InterventionImageExceptionNotReadableException(
            
"Unable to init from given url (".$url.")."
        
);
    }

    
/**
     * Init from given stream
     *
     * @param $stream
     * @return InterventionImageImage
     */
    
public function initFromStream($stream)
    {
        
$offset ftell($stream);
        
$shouldAndCanSeek $offset !== && $this->isStreamSeekable($stream);

        if (
$shouldAndCanSeek) {
            
rewind($stream);
        }

        
$data = @stream_get_contents($stream);

        if (
$shouldAndCanSeek) {
            
fseek($stream$offset);
        }

        if (
$data) {
            return 
$this->initFromBinary($data);
        }

        throw new 
InterventionImageExceptionNotReadableException(
            
"Unable to init from given stream"
        
);
    }

    
/**
     * Checks if we can move the pointer for this stream
     *
     * @param resource $stream
     * @return bool
     */
    
private function isStreamSeekable($stream)
    {
        
$metadata stream_get_meta_data($stream);
        return 
$metadata['seekable'];
    }

    
/**
     * Determines if current source data is GD resource
     *
     * @return boolean
     */
    
public function isGdResource()
    {
        if (
is_resource($this->data)) {
            return (
get_resource_type($this->data) == 'gd');
        }

        return 
false;
    }

    
/**
     * Determines if current source data is Imagick object
     *
     * @return boolean
     */
    
public function isImagick()
    {
        return 
is_a($this->data'Imagick');
    }

    
/**
     * Determines if current source data is InterventionImageImage object
     *
     * @return boolean
     */
    
public function isInterventionImage()
    {
        return 
is_a($this->data'InterventionImageImage');
    }

    
/**
     * Determines if current data is SplFileInfo object
     *
     * @return boolean
     */
    
public function isSplFileInfo()
    {
        return 
is_a($this->data'SplFileInfo');
    }

    
/**
     * Determines if current data is Symfony UploadedFile component
     *
     * @return boolean
     */
    
public function isSymfonyUpload()
    {
        return 
is_a($this->data'SymfonyComponentHttpFoundationFileUploadedFile');
    }

    
/**
     * Determines if current source data is file path
     *
     * @return boolean
     */
    
public function isFilePath()
    {
        if (
is_string($this->data)) {
            return 
is_file($this->data);
        }

        return 
false;
    }

    
/**
     * Determines if current source data is url
     *
     * @return boolean
     */
    
public function isUrl()
    {
        return (bool) 
filter_var($this->dataFILTER_VALIDATE_URL);
    }

    
/**
     * Determines if current source data is a stream resource
     *
     * @return boolean
     */
    
public function isStream()
    {
        if (!
is_resource($this->data)) return false;
        if (
get_resource_type($this->data) !== 'stream') return false;

        return 
true;
    }

    
/**
     * Determines if current source data is binary data
     *
     * @return boolean
     */
    
public function isBinary()
    {
        if (
is_string($this->data)) {
            
$mime finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
            return (
substr($mime04) != 'text' && $mime != 'application/x-empty');
        }

        return 
false;
    }

    
/**
     * Determines if current source data is data-url
     *
     * @return boolean
     */
    
public function isDataUrl()
    {
        
$data $this->decodeDataUrl($this->data);

        return 
is_null($data) ? false true;
    }

    
/**
     * Determines if current source data is base64 encoded
     *
     * @return boolean
     */
    
public function isBase64()
    {
        if (!
is_string($this->data)) {
            return 
false;
        }

        return 
base64_encode(base64_decode($this->data)) === $this->data;
    }

    
/**
     * Initiates new Image from InterventionImageImage
     *
     * @param  Image $object
     * @return InterventionImageImage
     */
    
public function initFromInterventionImage($object)
    {
        return 
$object;
    }

    
/**
     * Parses and decodes binary image data from data-url
     *
     * @param  string $data_url
     * @return string
     */
    
private function decodeDataUrl($data_url)
    {
        if (!
is_string($data_url)) {
            return 
null;
        }

        
$pattern "/^data:(?:image/[a-zA-Z-.]+)(?:charset=".+")?;base64,(?P<data>.+)$/";
        
preg_match($pattern$data_url$matches);

        if (
is_array($matches) && array_key_exists('data'$matches)) {
            return 
base64_decode($matches['data']);
        }

        return 
null;
    }

    
/**
     * Initiates new image from mixed data
     *
     * @param  mixed $data
     * @return InterventionImageImage
     */
    
public function init($data)
    {
        
$this->data $data;

        switch (
true) {

            case 
$this->isGdResource():
                return 
$this->initFromGdResource($this->data);

            case 
$this->isImagick():
                return 
$this->initFromImagick($this->data);

            case 
$this->isInterventionImage():
                return 
$this->initFromInterventionImage($this->data);

            case 
$this->isSplFileInfo():
                return 
$this->initFromPath($this->data->getRealPath());

            case 
$this->isBinary():
                return 
$this->initFromBinary($this->data);

            case 
$this->isUrl():
                return 
$this->initFromUrl($this->data);

            case 
$this->isStream():
                return 
$this->initFromStream($this->data);

            case 
$this->isFilePath():
                return 
$this->initFromPath($this->data);

            case 
$this->isDataUrl():
                return 
$this->initFromBinary($this->decodeDataUrl($this->data));

            case 
$this->isBase64():
                return 
$this->initFromBinary(base64_decode($this->data));

            default:
                throw new 
ExceptionNotReadableException("Image source not readable");
        }
    }

    
/**
     * Decoder object transforms to string source data
     *
     * @return string
     */
    
public function __toString()
    {
        return (string) 
$this->data;
    }
}
Онлайн: 0
Реклама