charger une image

This commit is contained in:
2026-01-09 10:51:56 +01:00
parent d0f4ed4eb2
commit e5714b47c6
518 changed files with 40175 additions and 44 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
upLoadImage/vendor

18
upLoadImage/composer.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "david/up-load-image",
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"authors": [
{
"name": "pestak",
"email": "wawawaformation@gmail.com"
}
],
"require": {
"intervention/image": "^3.11",
"symfony/var-dumper": "^7.4"
}
}

402
upLoadImage/composer.lock generated Normal file
View File

@@ -0,0 +1,402 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "96bf757a5ca346a045f7159c18329289",
"packages": [
{
"name": "intervention/gif",
"version": "4.2.4",
"source": {
"type": "git",
"url": "https://github.com/Intervention/gif.git",
"reference": "c3598a16ebe7690cd55640c44144a9df383ea73c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/gif/zipball/c3598a16ebe7690cd55640c44144a9df383ea73c",
"reference": "c3598a16ebe7690cd55640c44144a9df383ea73c",
"shasum": ""
},
"require": {
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"type": "library",
"autoload": {
"psr-4": {
"Intervention\\Gif\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"description": "Native PHP GIF Encoder/Decoder",
"homepage": "https://github.com/intervention/gif",
"keywords": [
"animation",
"gd",
"gif",
"image"
],
"support": {
"issues": "https://github.com/Intervention/gif/issues",
"source": "https://github.com/Intervention/gif/tree/4.2.4"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"time": "2026-01-04T09:27:23+00:00"
},
{
"name": "intervention/image",
"version": "3.11.6",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "5f6d27d9fd56312c47f347929e7ac15345c605a1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/5f6d27d9fd56312c47f347929e7ac15345c605a1",
"reference": "5f6d27d9fd56312c47f347929e7ac15345c605a1",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"intervention/gif": "^4.2",
"php": "^8.1"
},
"require-dev": {
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"suggest": {
"ext-exif": "Recommended to be able to read EXIF data properly."
},
"type": "library",
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io"
}
],
"description": "PHP Image Processing",
"homepage": "https://image.intervention.io",
"keywords": [
"gd",
"image",
"imagick",
"resize",
"thumbnail",
"watermark"
],
"support": {
"issues": "https://github.com/Intervention/image/issues",
"source": "https://github.com/Intervention/image/tree/3.11.6"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"time": "2025-12-17T13:38:29+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v3.6.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62",
"reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/contracts",
"name": "symfony/contracts"
},
"branch-alias": {
"dev-main": "3.6-dev"
}
},
"autoload": {
"files": [
"function.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.6.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-09-25T14:21:43+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.33.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493",
"shasum": ""
},
"require": {
"ext-iconv": "*",
"php": ">=7.2"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/polyfill",
"name": "symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-12-23T08:48:59+00:00"
},
{
"name": "symfony/var-dumper",
"version": "v7.4.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/var-dumper.git",
"reference": "7e99bebcb3f90d8721890f2963463280848cba92"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/7e99bebcb3f90d8721890f2963463280848cba92",
"reference": "7e99bebcb3f90d8721890f2963463280848cba92",
"shasum": ""
},
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"symfony/console": "<6.4"
},
"require-dev": {
"symfony/console": "^6.4|^7.0|^8.0",
"symfony/http-kernel": "^6.4|^7.0|^8.0",
"symfony/process": "^6.4|^7.0|^8.0",
"symfony/uid": "^6.4|^7.0|^8.0",
"twig/twig": "^3.12"
},
"bin": [
"Resources/bin/var-dump-server"
],
"type": "library",
"autoload": {
"files": [
"Resources/functions/dump.php"
],
"psr-4": {
"Symfony\\Component\\VarDumper\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Provides mechanisms for walking through any arbitrary PHP variable",
"homepage": "https://symfony.com",
"keywords": [
"debug",
"dump"
],
"support": {
"source": "https://github.com/symfony/var-dumper/tree/v7.4.3"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2025-12-18T07:04:31+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {},
"platform-dev": {},
"plugin-api-version": "2.9.0"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

BIN
upLoadImage/img/test.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -1,48 +1,11 @@
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo '<pre>';
print_r($_FILES);
echo '</pre>';
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
require 'vendor/autoload.php';
if(isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$dirUpload = 'img/';
$tmpName = $_FILES['image']['tmp_name'];
$name = basename($_FILES['image']['name']);
$uploadPath = $dirUpload . $name;
if (move_uploaded_file($tmpName, $uploadPath)) {
echo 'Fichier téléchargé avec succès : ' . htmlspecialchars($name);
} else {
echo 'Erreur lors du déplacement du fichier téléchargé.';
}
} else {
echo 'Erreur lors du téléchargement du fichier. Code d\'erreur : ' . $_FILES['image']['error'];
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="image">Choisissez une image à télécharger :</label> <br>
<input type="file" name="image" id="image" required>
<div class="submit">
<input type="submit" value="Téléverser l'image">
</div>
</form>
</body>
</html>
$interventionImage = new ImageManager(new Driver());
$image = $interventionImage->read('img/registre_a_decalage_schema.jpg');
$image->resize(300, 200);
$image->save('img/test.webp', 60);

22
upLoadImage/vendor/autoload.php vendored Normal file
View File

@@ -0,0 +1,22 @@
<?php
// autoload.php @generated by Composer
if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
throw new RuntimeException($err);
}
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit3b209c2e463c36e66e539cfd67f7b941::getLoader();

119
upLoadImage/vendor/bin/var-dump-server vendored Executable file
View File

@@ -0,0 +1,119 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../symfony/var-dumper/Resources/bin/var-dump-server)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (
(function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true))
|| (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper'))
) {
return include("phpvfscomposer://" . __DIR__ . '/..'.'/symfony/var-dumper/Resources/bin/var-dump-server');
}
}
return include __DIR__ . '/..'.'/symfony/var-dumper/Resources/bin/var-dump-server';

View File

@@ -0,0 +1,579 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see https://www.php-fig.org/psr/psr-0/
* @see https://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
/** @var \Closure(string):void */
private static $includeFile;
/** @var string|null */
private $vendorDir;
// PSR-4
/**
* @var array<string, array<string, int>>
*/
private $prefixLengthsPsr4 = array();
/**
* @var array<string, list<string>>
*/
private $prefixDirsPsr4 = array();
/**
* @var list<string>
*/
private $fallbackDirsPsr4 = array();
// PSR-0
/**
* List of PSR-0 prefixes
*
* Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))
*
* @var array<string, array<string, list<string>>>
*/
private $prefixesPsr0 = array();
/**
* @var list<string>
*/
private $fallbackDirsPsr0 = array();
/** @var bool */
private $useIncludePath = false;
/**
* @var array<string, string>
*/
private $classMap = array();
/** @var bool */
private $classMapAuthoritative = false;
/**
* @var array<string, bool>
*/
private $missingClasses = array();
/** @var string|null */
private $apcuPrefix;
/**
* @var array<string, self>
*/
private static $registeredLoaders = array();
/**
* @param string|null $vendorDir
*/
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
self::initializeIncludeClosure();
}
/**
* @return array<string, list<string>>
*/
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
}
return array();
}
/**
* @return array<string, list<string>>
*/
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
/**
* @return list<string>
*/
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
/**
* @return list<string>
*/
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
/**
* @return array<string, string> Array of classname => path
*/
public function getClassMap()
{
return $this->classMap;
}
/**
* @param array<string, string> $classMap Class to filename map
*
* @return void
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param list<string>|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*
* @return void
*/
public function add($prefix, $paths, $prepend = false)
{
$paths = (array) $paths;
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
$paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
$paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
$paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param list<string>|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
$paths = (array) $paths;
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
$paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
$paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
$paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param list<string>|string $paths The PSR-0 base directories
*
* @return void
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param list<string>|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*
* @return void
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*
* @return void
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*
* @return void
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*
* @return void
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
if (null === $this->vendorDir) {
return;
}
if ($prepend) {
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
} else {
unset(self::$registeredLoaders[$this->vendorDir]);
self::$registeredLoaders[$this->vendorDir] = $this;
}
}
/**
* Unregisters this instance as an autoloader.
*
* @return void
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
if (null !== $this->vendorDir) {
unset(self::$registeredLoaders[$this->vendorDir]);
}
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return true|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
$includeFile = self::$includeFile;
$includeFile($file);
return true;
}
return null;
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
/**
* Returns the currently registered loaders keyed by their corresponding vendor directories.
*
* @return array<string, self>
*/
public static function getRegisteredLoaders()
{
return self::$registeredLoaders;
}
/**
* @param string $class
* @param string $ext
* @return string|false
*/
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
/**
* @return void
*/
private static function initializeIncludeClosure()
{
if (self::$includeFile !== null) {
return;
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param string $file
* @return void
*/
self::$includeFile = \Closure::bind(static function($file) {
include $file;
}, null, null);
}
}

View File

@@ -0,0 +1,396 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer;
use Composer\Autoload\ClassLoader;
use Composer\Semver\VersionParser;
/**
* This class is copied in every Composer installed project and available to all
*
* See also https://getcomposer.org/doc/07-runtime.md#installed-versions
*
* To require its presence, you can require `composer-runtime-api ^2.0`
*
* @final
*/
class InstalledVersions
{
/**
* @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
* @internal
*/
private static $selfDir = null;
/**
* @var mixed[]|null
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
*/
private static $installed;
/**
* @var bool
*/
private static $installedIsLocalDir;
/**
* @var bool|null
*/
private static $canGetVendors;
/**
* @var array[]
* @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
*/
private static $installedByVendor = array();
/**
* Returns a list of all package names which are present, either by being installed, replaced or provided
*
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackages()
{
$packages = array();
foreach (self::getInstalled() as $installed) {
$packages[] = array_keys($installed['versions']);
}
if (1 === \count($packages)) {
return $packages[0];
}
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
}
/**
* Returns a list of all package names with a specific type e.g. 'library'
*
* @param string $type
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackagesByType($type)
{
$packagesByType = array();
foreach (self::getInstalled() as $installed) {
foreach ($installed['versions'] as $name => $package) {
if (isset($package['type']) && $package['type'] === $type) {
$packagesByType[] = $name;
}
}
}
return $packagesByType;
}
/**
* Checks whether the given package is installed
*
* This also returns true if the package name is provided or replaced by another package
*
* @param string $packageName
* @param bool $includeDevRequirements
* @return bool
*/
public static function isInstalled($packageName, $includeDevRequirements = true)
{
foreach (self::getInstalled() as $installed) {
if (isset($installed['versions'][$packageName])) {
return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
}
}
return false;
}
/**
* Checks whether the given package satisfies a version constraint
*
* e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
*
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
*
* @param VersionParser $parser Install composer/semver to have access to this class and functionality
* @param string $packageName
* @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
* @return bool
*/
public static function satisfies(VersionParser $parser, $packageName, $constraint)
{
$constraint = $parser->parseConstraints((string) $constraint);
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
return $provided->matches($constraint);
}
/**
* Returns a version constraint representing all the range(s) which are installed for a given package
*
* It is easier to use this via isInstalled() with the $constraint argument if you need to check
* whether a given version of a package is installed, and not just whether it exists
*
* @param string $packageName
* @return string Version constraint usable with composer/semver
*/
public static function getVersionRanges($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
$ranges = array();
if (isset($installed['versions'][$packageName]['pretty_version'])) {
$ranges[] = $installed['versions'][$packageName]['pretty_version'];
}
if (array_key_exists('aliases', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
}
if (array_key_exists('replaced', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
}
if (array_key_exists('provided', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
}
return implode(' || ', $ranges);
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['version'])) {
return null;
}
return $installed['versions'][$packageName]['version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getPrettyVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['pretty_version'])) {
return null;
}
return $installed['versions'][$packageName]['pretty_version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
*/
public static function getReference($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['reference'])) {
return null;
}
return $installed['versions'][$packageName]['reference'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
*/
public static function getInstallPath($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @return array
* @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
*/
public static function getRootPackage()
{
$installed = self::getInstalled();
return $installed[0]['root'];
}
/**
* Returns the raw installed.php data for custom implementations
*
* @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
* @return array[]
* @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}
*/
public static function getRawData()
{
@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
self::$installed = include __DIR__ . '/installed.php';
} else {
self::$installed = array();
}
}
return self::$installed;
}
/**
* Returns the raw data of all installed.php which are currently loaded for custom implementations
*
* @return array[]
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
*/
public static function getAllRawData()
{
return self::getInstalled();
}
/**
* Lets you reload the static array from another file
*
* This is only useful for complex integrations in which a project needs to use
* this class but then also needs to execute another project's autoloader in process,
* and wants to ensure both projects have access to their version of installed.php.
*
* A typical case would be PHPUnit, where it would need to make sure it reads all
* the data it needs from this class, then call reload() with
* `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
* the project in which it runs can then also use this class safely, without
* interference between PHPUnit's dependencies and the project's dependencies.
*
* @param array[] $data A vendor/composer/installed.php data set
* @return void
*
* @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data
*/
public static function reload($data)
{
self::$installed = $data;
self::$installedByVendor = array();
// when using reload, we disable the duplicate protection to ensure that self::$installed data is
// always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not,
// so we have to assume it does not, and that may result in duplicate data being returned when listing
// all installed packages for example
self::$installedIsLocalDir = false;
}
/**
* @return string
*/
private static function getSelfDir()
{
if (self::$selfDir === null) {
self::$selfDir = strtr(__DIR__, '\\', '/');
}
return self::$selfDir;
}
/**
* @return array[]
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
*/
private static function getInstalled()
{
if (null === self::$canGetVendors) {
self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
}
$installed = array();
$copiedLocalDir = false;
if (self::$canGetVendors) {
$selfDir = self::getSelfDir();
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
$vendorDir = strtr($vendorDir, '\\', '/');
if (isset(self::$installedByVendor[$vendorDir])) {
$installed[] = self::$installedByVendor[$vendorDir];
} elseif (is_file($vendorDir.'/composer/installed.php')) {
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
$required = require $vendorDir.'/composer/installed.php';
self::$installedByVendor[$vendorDir] = $required;
$installed[] = $required;
if (self::$installed === null && $vendorDir.'/composer' === $selfDir) {
self::$installed = $required;
self::$installedIsLocalDir = true;
}
}
if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) {
$copiedLocalDir = true;
}
}
}
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
$required = require __DIR__ . '/installed.php';
self::$installed = $required;
} else {
self::$installed = array();
}
}
if (self::$installed !== array() && !$copiedLocalDir) {
$installed[] = self::$installed;
}
return $installed;
}
}

21
upLoadImage/vendor/composer/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,10 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
);

View File

@@ -0,0 +1,12 @@
<?php
// autoload_files.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'6e3fae29631ef280660b3cdad06f25a8' => $vendorDir . '/symfony/deprecation-contracts/function.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
'667aeda72477189d0494fecd327c3641' => $vendorDir . '/symfony/var-dumper/Resources/functions/dump.php',
);

View File

@@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
);

View File

@@ -0,0 +1,14 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Symfony\\Component\\VarDumper\\' => array($vendorDir . '/symfony/var-dumper'),
'Intervention\\Image\\' => array($vendorDir . '/intervention/image/src'),
'Intervention\\Gif\\' => array($vendorDir . '/intervention/gif/src'),
'App\\' => array($baseDir . '/src'),
);

View File

@@ -0,0 +1,50 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit3b209c2e463c36e66e539cfd67f7b941
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
require __DIR__ . '/platform_check.php';
spl_autoload_register(array('ComposerAutoloaderInit3b209c2e463c36e66e539cfd67f7b941', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit3b209c2e463c36e66e539cfd67f7b941', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941::getInitializer($loader));
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
require $file;
}
}, null, null);
foreach ($filesToLoad as $fileIdentifier => $file) {
$requireFile($fileIdentifier, $file);
}
return $loader;
}
}

View File

@@ -0,0 +1,68 @@
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941
{
public static $files = array (
'6e3fae29631ef280660b3cdad06f25a8' => __DIR__ . '/..' . '/symfony/deprecation-contracts/function.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
'667aeda72477189d0494fecd327c3641' => __DIR__ . '/..' . '/symfony/var-dumper/Resources/functions/dump.php',
);
public static $prefixLengthsPsr4 = array (
'S' =>
array (
'Symfony\\Polyfill\\Mbstring\\' => 26,
'Symfony\\Component\\VarDumper\\' => 28,
),
'I' =>
array (
'Intervention\\Image\\' => 19,
'Intervention\\Gif\\' => 17,
),
'A' =>
array (
'App\\' => 4,
),
);
public static $prefixDirsPsr4 = array (
'Symfony\\Polyfill\\Mbstring\\' =>
array (
0 => __DIR__ . '/..' . '/symfony/polyfill-mbstring',
),
'Symfony\\Component\\VarDumper\\' =>
array (
0 => __DIR__ . '/..' . '/symfony/var-dumper',
),
'Intervention\\Image\\' =>
array (
0 => __DIR__ . '/..' . '/intervention/image/src',
),
'Intervention\\Gif\\' =>
array (
0 => __DIR__ . '/..' . '/intervention/gif/src',
),
'App\\' =>
array (
0 => __DIR__ . '/../..' . '/src',
),
);
public static $classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit3b209c2e463c36e66e539cfd67f7b941::$classMap;
}, null, ClassLoader::class);
}
}

View File

@@ -0,0 +1,404 @@
{
"packages": [
{
"name": "intervention/gif",
"version": "4.2.4",
"version_normalized": "4.2.4.0",
"source": {
"type": "git",
"url": "https://github.com/Intervention/gif.git",
"reference": "c3598a16ebe7690cd55640c44144a9df383ea73c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/gif/zipball/c3598a16ebe7690cd55640c44144a9df383ea73c",
"reference": "c3598a16ebe7690cd55640c44144a9df383ea73c",
"shasum": ""
},
"require": {
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"time": "2026-01-04T09:27:23+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"Intervention\\Gif\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"description": "Native PHP GIF Encoder/Decoder",
"homepage": "https://github.com/intervention/gif",
"keywords": [
"animation",
"gd",
"gif",
"image"
],
"support": {
"issues": "https://github.com/Intervention/gif/issues",
"source": "https://github.com/Intervention/gif/tree/4.2.4"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"install-path": "../intervention/gif"
},
{
"name": "intervention/image",
"version": "3.11.6",
"version_normalized": "3.11.6.0",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "5f6d27d9fd56312c47f347929e7ac15345c605a1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/5f6d27d9fd56312c47f347929e7ac15345c605a1",
"reference": "5f6d27d9fd56312c47f347929e7ac15345c605a1",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"intervention/gif": "^4.2",
"php": "^8.1"
},
"require-dev": {
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"suggest": {
"ext-exif": "Recommended to be able to read EXIF data properly."
},
"time": "2025-12-17T13:38:29+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io"
}
],
"description": "PHP Image Processing",
"homepage": "https://image.intervention.io",
"keywords": [
"gd",
"image",
"imagick",
"resize",
"thumbnail",
"watermark"
],
"support": {
"issues": "https://github.com/Intervention/image/issues",
"source": "https://github.com/Intervention/image/tree/3.11.6"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"install-path": "../intervention/image"
},
{
"name": "symfony/deprecation-contracts",
"version": "v3.6.0",
"version_normalized": "3.6.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62",
"reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"time": "2024-09-25T14:21:43+00:00",
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/contracts",
"name": "symfony/contracts"
},
"branch-alias": {
"dev-main": "3.6-dev"
}
},
"installation-source": "dist",
"autoload": {
"files": [
"function.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.6.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"install-path": "../symfony/deprecation-contracts"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.33.0",
"version_normalized": "1.33.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493",
"shasum": ""
},
"require": {
"ext-iconv": "*",
"php": ">=7.2"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"time": "2024-12-23T08:48:59+00:00",
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/polyfill",
"name": "symfony/polyfill"
}
},
"installation-source": "dist",
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"install-path": "../symfony/polyfill-mbstring"
},
{
"name": "symfony/var-dumper",
"version": "v7.4.3",
"version_normalized": "7.4.3.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/var-dumper.git",
"reference": "7e99bebcb3f90d8721890f2963463280848cba92"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/7e99bebcb3f90d8721890f2963463280848cba92",
"reference": "7e99bebcb3f90d8721890f2963463280848cba92",
"shasum": ""
},
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"symfony/console": "<6.4"
},
"require-dev": {
"symfony/console": "^6.4|^7.0|^8.0",
"symfony/http-kernel": "^6.4|^7.0|^8.0",
"symfony/process": "^6.4|^7.0|^8.0",
"symfony/uid": "^6.4|^7.0|^8.0",
"twig/twig": "^3.12"
},
"time": "2025-12-18T07:04:31+00:00",
"bin": [
"Resources/bin/var-dump-server"
],
"type": "library",
"installation-source": "dist",
"autoload": {
"files": [
"Resources/functions/dump.php"
],
"psr-4": {
"Symfony\\Component\\VarDumper\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Provides mechanisms for walking through any arbitrary PHP variable",
"homepage": "https://symfony.com",
"keywords": [
"debug",
"dump"
],
"support": {
"source": "https://github.com/symfony/var-dumper/tree/v7.4.3"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"install-path": "../symfony/var-dumper"
}
],
"dev": true,
"dev-package-names": []
}

View File

@@ -0,0 +1,68 @@
<?php return array(
'root' => array(
'name' => 'david/up-load-image',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'd0f4ed4eb2a09d5a71e5a7ff525514067ef181ae',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev' => true,
),
'versions' => array(
'david/up-load-image' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'd0f4ed4eb2a09d5a71e5a7ff525514067ef181ae',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'dev_requirement' => false,
),
'intervention/gif' => array(
'pretty_version' => '4.2.4',
'version' => '4.2.4.0',
'reference' => 'c3598a16ebe7690cd55640c44144a9df383ea73c',
'type' => 'library',
'install_path' => __DIR__ . '/../intervention/gif',
'aliases' => array(),
'dev_requirement' => false,
),
'intervention/image' => array(
'pretty_version' => '3.11.6',
'version' => '3.11.6.0',
'reference' => '5f6d27d9fd56312c47f347929e7ac15345c605a1',
'type' => 'library',
'install_path' => __DIR__ . '/../intervention/image',
'aliases' => array(),
'dev_requirement' => false,
),
'symfony/deprecation-contracts' => array(
'pretty_version' => 'v3.6.0',
'version' => '3.6.0.0',
'reference' => '63afe740e99a13ba87ec199bb07bbdee937a5b62',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/deprecation-contracts',
'aliases' => array(),
'dev_requirement' => false,
),
'symfony/polyfill-mbstring' => array(
'pretty_version' => 'v1.33.0',
'version' => '1.33.0.0',
'reference' => '6d857f4d76bd4b343eac26d6b539585d2bc56493',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/polyfill-mbstring',
'aliases' => array(),
'dev_requirement' => false,
),
'symfony/var-dumper' => array(
'pretty_version' => 'v7.4.3',
'version' => '7.4.3.0',
'reference' => '7e99bebcb3f90d8721890f2963463280848cba92',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/var-dumper',
'aliases' => array(),
'dev_requirement' => false,
),
),
);

View File

@@ -0,0 +1,25 @@
<?php
// platform_check.php @generated by Composer
$issues = array();
if (!(PHP_VERSION_ID >= 80200)) {
$issues[] = 'Your Composer dependencies require a PHP version ">= 8.2.0". You are running ' . PHP_VERSION . '.';
}
if ($issues) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL);
} elseif (!headers_sent()) {
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
}
}
throw new \RuntimeException(
'Composer detected issues in your platform: ' . implode(' ', $issues)
);
}

21
upLoadImage/vendor/intervention/gif/LICENSE vendored Executable file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2020-present Oliver Vogel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

101
upLoadImage/vendor/intervention/gif/README.md vendored Executable file
View File

@@ -0,0 +1,101 @@
# Intervention GIF
## Native PHP GIF Encoder/Decoder
[![Latest Version](https://img.shields.io/packagist/v/intervention/gif.svg)](https://packagist.org/packages/intervention/gif)
![build](https://github.com/Intervention/gif/actions/workflows/build.yml/badge.svg)
[![Monthly Downloads](https://img.shields.io/packagist/dm/intervention/gif.svg)](https://packagist.org/packages/intervention/gif/stats)
[![Support me on Ko-fi](https://raw.githubusercontent.com/Intervention/gif/main/.github/images/support.svg)](https://ko-fi.com/interventionphp)
Intervention GIF is a PHP encoder and decoder for the GIF image format that
does not depend on any image processing extension.
Only the special `Splitter::class` class divides the data stream of an animated
GIF into individual `GDImage` objects for each frame and is therefore dependent
on the GD library.
The library is the main component of [Intervention
Image](https://github.com/Intervention/image) for processing animated GIF files
with the GD library, but also works independently.
## Installation
You can easily install this package using [Composer](https://getcomposer.org).
Just request the package with the following command:
```bash
composer require intervention/gif
```
## Code Examples
### Decoding
```php
use Intervention\Gif\Decoder;
// Decode filepath to Intervention\Gif\GifDataStream::class
$gif = Decoder::decode('images/animation.gif');
// Decoder can also handle binary content directly
$gif = Decoder::decode($contents);
```
### Encoding
Use the Builder class to create a new GIF image.
```php
use Intervention\Gif\Builder;
// create new gif canvas
$gif = Builder::canvas(width: 32, height: 32);
// add animation frames to canvas
$delay = .25; // delay in seconds after next frame is displayed
$left = 0; // position offset (left)
$top = 0; // position offset (top)
// add animation frames with optional delay in seconds
// and optional position offset for each frame
$gif->addFrame('images/frame01.gif', $delay, $left, $top);
$gif->addFrame('images/frame02.gif', $delay, $left);
$gif->addFrame('images/frame03.gif', $delay);
$gif->addFrame('images/frame04.gif');
// set loop count; 0 for infinite looping
$gif->setLoops(12);
// encode
$data = $gif->encode();
```
## Requirements
- PHP >= 8.1
## Development & Testing
With this package comes a Docker image to build a test suite and analysis
container. To build this container you have to have Docker installed on your
system. You can run all tests with this command.
```bash
docker-compose run --rm --build tests
```
Run the static analyzer on the code base.
```bash
docker-compose run --rm --build analysis
```
## Authors
This library is developed and maintained by [Oliver Vogel](https://intervention.io)
Thanks to the community of [contributors](https://github.com/Intervention/gif/graphs/contributors) who have helped to improve this project.
## License
Intervention GIF is licensed under the [MIT License](LICENSE).

View File

@@ -0,0 +1,44 @@
{
"name": "intervention/gif",
"description": "Native PHP GIF Encoder/Decoder",
"homepage": "https://github.com/intervention/gif",
"keywords": [
"image",
"gd",
"gif",
"animation"
],
"license": "MIT",
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"require": {
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"phpstan/phpstan": "^2.1",
"squizlabs/php_codesniffer": "^3.8",
"slevomat/coding-standard": "~8.0"
},
"autoload": {
"psr-4": {
"Intervention\\Gif\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Intervention\\Gif\\Tests\\": "tests"
}
},
"minimum-stability": "stable",
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix=".php">./tests/Unit/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
use Intervention\Gif\Exceptions\EncoderException;
use Intervention\Gif\Traits\CanDecode;
use Intervention\Gif\Traits\CanEncode;
use ReflectionClass;
use Stringable;
abstract class AbstractEntity implements Stringable
{
use CanEncode;
use CanDecode;
public const TERMINATOR = "\x00";
/**
* Get short classname of current instance
*/
public static function getShortClassname(): string
{
return (new ReflectionClass(static::class))->getShortName();
}
/**
* Cast object to string
*
* @throws EncoderException
*/
public function __toString(): string
{
return $this->encode();
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
abstract class AbstractExtension extends AbstractEntity
{
public const MARKER = "\x21";
}

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractExtension;
use Intervention\Gif\Exceptions\RuntimeException;
class ApplicationExtension extends AbstractExtension
{
public const LABEL = "\xFF";
/**
* Application Identifier & Auth Code
*/
protected string $application = '';
/**
* Data Sub Blocks
*
* @var array<DataSubBlock>
*/
protected array $blocks = [];
/**
* Get size of block
*/
public function getBlockSize(): int
{
return strlen($this->application);
}
/**
* Set application name
*/
public function setApplication(string $value): self
{
$this->application = $value;
return $this;
}
/**
* Get application name
*/
public function getApplication(): string
{
return $this->application;
}
/**
* Add block to application extension
*/
public function addBlock(DataSubBlock $block): self
{
$this->blocks[] = $block;
return $this;
}
/**
* Set data sub blocks of instance
*
* @param array<DataSubBlock> $blocks
*/
public function setBlocks(array $blocks): self
{
$this->blocks = $blocks;
return $this;
}
/**
* Get blocks of ApplicationExtension
*
* @return array<DataSubBlock>
*/
public function getBlocks(): array
{
return $this->blocks;
}
/**
* Get first block of ApplicationExtension
*
* @throws RuntimeException
*/
public function getFirstBlock(): DataSubBlock
{
if (!array_key_exists(0, $this->blocks)) {
throw new RuntimeException('Unable to retrieve data sub block.');
}
return $this->blocks[0];
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class Color extends AbstractEntity
{
/**
* Create new instance
*/
public function __construct(
protected int $r = 0,
protected int $g = 0,
protected int $b = 0
) {
//
}
/**
* Get red value
*/
public function getRed(): int
{
return $this->r;
}
/**
* Set red value
*/
public function setRed(int $value): self
{
$this->r = $value;
return $this;
}
/**
* Get green value
*/
public function getGreen(): int
{
return $this->g;
}
/**
* Set green value
*/
public function setGreen(int $value): self
{
$this->g = $value;
return $this;
}
/**
* Get blue value
*/
public function getBlue(): int
{
return $this->b;
}
/**
* Set blue value
*/
public function setBlue(int $value): self
{
$this->b = $value;
return $this;
}
/**
* Return hash value of current color
*/
public function getHash(): string
{
return md5($this->r . $this->g . $this->b);
}
}

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class ColorTable extends AbstractEntity
{
/**
* Create new instance
*
* @param array<Color> $colors
* @return void
*/
public function __construct(protected array $colors = [])
{
//
}
/**
* Return array of current colors
*
* @return array<Color>
*/
public function getColors(): array
{
return array_values($this->colors);
}
/**
* Add color to table
*/
public function addRgb(int $r, int $g, int $b): self
{
$this->addColor(new Color($r, $g, $b));
return $this;
}
/**
* Add color to table
*/
public function addColor(Color $color): self
{
$this->colors[] = $color;
return $this;
}
/**
* Reset colors to array of color objects
*
* @param array<Color> $colors
*/
public function setColors(array $colors): self
{
$this->empty();
foreach ($colors as $color) {
$this->addColor($color);
}
return $this;
}
/**
* Count colors of current instance
*/
public function countColors(): int
{
return count($this->colors);
}
/**
* Determine if any colors are present on the current table
*/
public function hasColors(): bool
{
return $this->countColors() >= 1;
}
/**
* Empty color table
*/
public function empty(): self
{
$this->colors = [];
return $this;
}
/**
* Get size of color table in logical screen descriptor
*/
public function getLogicalSize(): int
{
return match ($this->countColors()) {
4 => 1,
8 => 2,
16 => 3,
32 => 4,
64 => 5,
128 => 6,
256 => 7,
default => 0,
};
}
/**
* Calculate the number of bytes contained by the current table
*/
public function getByteSize(): int
{
if (!$this->hasColors()) {
return 0;
}
return 3 * pow(2, $this->getLogicalSize() + 1);
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractExtension;
class CommentExtension extends AbstractExtension
{
public const LABEL = "\xFE";
/**
* Comment blocks
*
* @var array<string>
*/
protected array $comments = [];
/**
* Get all or one comment
*
* @return array<string>
*/
public function getComments(): array
{
return $this->comments;
}
/**
* Get one comment by key
*/
public function getComment(int $key): mixed
{
return $this->comments[$key] ?? null;
}
/**
* Set comment text
*/
public function addComment(string $value): self
{
$this->comments[] = $value;
return $this;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
use Intervention\Gif\Exceptions\FormatException;
class DataSubBlock extends AbstractEntity
{
/**
* Create new instance
*
* @throws FormatException
*/
public function __construct(protected string $value)
{
if ($this->getSize() > 255) {
throw new FormatException(
'Data Sub-Block can not have a block size larger than 255 bytes.'
);
}
}
/**
* Return size of current block
*/
public function getSize(): int
{
return strlen($this->value);
}
/**
* Return block value
*/
public function getValue(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,250 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
/**
* The GIF files that can be found on the Internet come in a wide variety
* of forms. Some strictly adhere to the original specification, others do
* not and differ in the actual sequence of blocks or their number.
*
* For this reason, this libary has this (kind of "virtual") FrameBlock,
* which can contain all possible blocks in different order that occur in
* a GIF animation.
*
* - Image Description
* - Local Color Table
* - Image Data Block
* - Plain Text Extension
* - Application Extension
* - Comment Extension
*
* The TableBasedImage block, which is a chain of ImageDescriptor, (Local
* Color Table) and ImageData, is used as a marker for terminating a
* FrameBlock.
*
* So far I have only seen GIF files that follow this scheme. However, there are
* examples which have one (or more) comment extensions added before the end. So
* there can be additional "global comments" that are not part of the FrameBlock
* and are appended to the GifDataStream afterwards.
*/
class FrameBlock extends AbstractEntity
{
protected ?GraphicControlExtension $graphicControlExtension = null;
protected ?ColorTable $colorTable = null;
protected ?PlainTextExtension $plainTextExtension = null;
/**
* @var array<ApplicationExtension> $applicationExtensions
*/
protected array $applicationExtensions = [];
/**
* @var array<CommentExtension> $commentExtensions
*/
protected array $commentExtensions = [];
public function __construct(
protected ImageDescriptor $imageDescriptor = new ImageDescriptor(),
protected ImageData $imageData = new ImageData()
) {
//
}
public function addEntity(AbstractEntity $entity): self
{
return match (true) {
$entity instanceof TableBasedImage => $this->setTableBasedImage($entity),
$entity instanceof GraphicControlExtension => $this->setGraphicControlExtension($entity),
$entity instanceof ImageDescriptor => $this->setImageDescriptor($entity),
$entity instanceof ColorTable => $this->setColorTable($entity),
$entity instanceof ImageData => $this->setImageData($entity),
$entity instanceof PlainTextExtension => $this->setPlainTextExtension($entity),
$entity instanceof NetscapeApplicationExtension,
$entity instanceof ApplicationExtension => $this->addApplicationExtension($entity),
$entity instanceof CommentExtension => $this->addCommentExtension($entity),
default => $this,
};
}
/**
* Return application extensions of current frame block
*
* @return array<ApplicationExtension>
*/
public function getApplicationExtensions(): array
{
return $this->applicationExtensions;
}
/**
* Return comment extensions of current frame block
*
* @return array<CommentExtension>
*/
public function getCommentExtensions(): array
{
return $this->commentExtensions;
}
/**
* Set the graphic control extension
*/
public function setGraphicControlExtension(GraphicControlExtension $extension): self
{
$this->graphicControlExtension = $extension;
return $this;
}
/**
* Get the graphic control extension of the current frame block
*/
public function getGraphicControlExtension(): ?GraphicControlExtension
{
return $this->graphicControlExtension;
}
/**
* Set the image descriptor
*/
public function setImageDescriptor(ImageDescriptor $descriptor): self
{
$this->imageDescriptor = $descriptor;
return $this;
}
/**
* Get the image descriptor of the frame block
*/
public function getImageDescriptor(): ImageDescriptor
{
return $this->imageDescriptor;
}
/**
* Set the color table of the current frame block
*/
public function setColorTable(ColorTable $table): self
{
$this->colorTable = $table;
return $this;
}
/**
* Get color table
*/
public function getColorTable(): ?ColorTable
{
return $this->colorTable;
}
/**
* Determine if frame block has color table
*/
public function hasColorTable(): bool
{
return !is_null($this->colorTable);
}
/**
* Set image data of frame block
*/
public function setImageData(ImageData $data): self
{
$this->imageData = $data;
return $this;
}
/**
* Get image data of current frame block
*/
public function getImageData(): ImageData
{
return $this->imageData;
}
/**
* Set plain text extension
*/
public function setPlainTextExtension(PlainTextExtension $extension): self
{
$this->plainTextExtension = $extension;
return $this;
}
/**
* Get plain text extension
*/
public function getPlainTextExtension(): ?PlainTextExtension
{
return $this->plainTextExtension;
}
/**
* Add given application extension to the current frame block
*/
public function addApplicationExtension(ApplicationExtension $extension): self
{
$this->applicationExtensions[] = $extension;
return $this;
}
/**
* Remove all application extensions from the current frame block.
*/
public function clearApplicationExtensions(): self
{
$this->applicationExtensions = [];
return $this;
}
/**
* Add given comment extension to the current frame block
*/
public function addCommentExtension(CommentExtension $extension): self
{
$this->commentExtensions[] = $extension;
return $this;
}
/**
* Return netscape extension of the frame block if available
*/
public function getNetscapeExtension(): ?NetscapeApplicationExtension
{
$extensions = array_filter(
$this->applicationExtensions,
fn(ApplicationExtension $extension): bool => $extension instanceof NetscapeApplicationExtension,
);
return count($extensions) ? reset($extensions) : null;
}
/**
* Set the table based image of the current frame block
*/
public function setTableBasedImage(TableBasedImage $tableBasedImage): self
{
$this->setImageDescriptor($tableBasedImage->getImageDescriptor());
if ($colorTable = $tableBasedImage->getColorTable()) {
$this->setColorTable($colorTable);
}
$this->setImageData($tableBasedImage->getImageData());
return $this;
}
}

View File

@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractExtension;
use Intervention\Gif\DisposalMethod;
class GraphicControlExtension extends AbstractExtension
{
public const LABEL = "\xF9";
public const BLOCKSIZE = "\x04";
/**
* Existance flag of transparent color
*/
protected bool $transparentColorExistance = false;
/**
* Transparent color index
*/
protected int $transparentColorIndex = 0;
/**
* User input flag
*/
protected bool $userInput = false;
/**
* Create new instance
*/
public function __construct(
protected int $delay = 0,
protected DisposalMethod $disposalMethod = DisposalMethod::UNDEFINED,
) {
//
}
/**
* Set delay time (1/100 second)
*/
public function setDelay(int $value): self
{
$this->delay = $value;
return $this;
}
/**
* Return delay time (1/100 second)
*/
public function getDelay(): int
{
return $this->delay;
}
/**
* Set disposal method
*/
public function setDisposalMethod(DisposalMethod $method): self
{
$this->disposalMethod = $method;
return $this;
}
/**
* Get disposal method
*/
public function getDisposalMethod(): DisposalMethod
{
return $this->disposalMethod;
}
/**
* Get transparent color index
*/
public function getTransparentColorIndex(): int
{
return $this->transparentColorIndex;
}
/**
* Set transparent color index
*/
public function setTransparentColorIndex(int $index): self
{
$this->transparentColorIndex = $index;
return $this;
}
/**
* Get current transparent color existance
*/
public function getTransparentColorExistance(): bool
{
return $this->transparentColorExistance;
}
/**
* Set existance flag of transparent color
*/
public function setTransparentColorExistance(bool $existance = true): self
{
$this->transparentColorExistance = $existance;
return $this;
}
/**
* Get user input flag
*/
public function getUserInput(): bool
{
return $this->userInput;
}
/**
* Set user input flag
*/
public function setUserInput(bool $value = true): self
{
$this->userInput = $value;
return $this;
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class Header extends AbstractEntity
{
/**
* Header signature
*/
public const SIGNATURE = 'GIF';
/**
* Current GIF version
*/
protected string $version = '89a';
/**
* Set GIF version
*/
public function setVersion(string $value): self
{
$this->version = $value;
return $this;
}
/**
* Return current version
*/
public function getVersion(): string
{
return $this->version;
}
}

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class ImageData extends AbstractEntity
{
/**
* LZW min. code size
*/
protected int $lzw_min_code_size;
/**
* Sub blocks
*
* @var array<DataSubBlock>
*/
protected array $blocks = [];
/**
* Get LZW min. code size
*/
public function getLzwMinCodeSize(): int
{
return $this->lzw_min_code_size;
}
/**
* Set lzw min. code size
*/
public function setLzwMinCodeSize(int $size): self
{
$this->lzw_min_code_size = $size;
return $this;
}
/**
* Get current data sub blocks
*
* @return array<DataSubBlock>
*/
public function getBlocks(): array
{
return $this->blocks;
}
/**
* Addd sub block
*/
public function addBlock(DataSubBlock $block): self
{
$this->blocks[] = $block;
return $this;
}
/**
* Determine if data sub blocks are present
*/
public function hasBlocks(): bool
{
return count($this->blocks) >= 1;
}
}

View File

@@ -0,0 +1,194 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class ImageDescriptor extends AbstractEntity
{
public const SEPARATOR = "\x2C";
/**
* Width of frame
*/
protected int $width = 0;
/**
* Height of frame
*/
protected int $height = 0;
/**
* Left position of frame
*/
protected int $left = 0;
/**
* Top position of frame
*/
protected int $top = 0;
/**
* Determine if frame is interlaced
*/
protected bool $interlaced = false;
/**
* Local color table flag
*/
protected bool $localColorTableExistance = false;
/**
* Sort flag of local color table
*/
protected bool $localColorTableSorted = false;
/**
* Size of local color table
*/
protected int $localColorTableSize = 0;
/**
* Get current width
*/
public function getWidth(): int
{
return intval($this->width);
}
/**
* Get current width
*/
public function getHeight(): int
{
return intval($this->height);
}
/**
* Get current Top
*/
public function getTop(): int
{
return intval($this->top);
}
/**
* Get current Left
*/
public function getLeft(): int
{
return intval($this->left);
}
/**
* Set size of current instance
*/
public function setSize(int $width, int $height): self
{
$this->width = $width;
$this->height = $height;
return $this;
}
/**
* Set position of current instance
*/
public function setPosition(int $left, int $top): self
{
$this->left = $left;
$this->top = $top;
return $this;
}
/**
* Determine if frame is interlaced
*/
public function isInterlaced(): bool
{
return $this->interlaced;
}
/**
* Set or unset interlaced value
*/
public function setInterlaced(bool $value = true): self
{
$this->interlaced = $value;
return $this;
}
/**
* Determine if local color table is present
*/
public function getLocalColorTableExistance(): bool
{
return $this->localColorTableExistance;
}
/**
* Alias for getLocalColorTableExistance
*/
public function hasLocalColorTable(): bool
{
return $this->getLocalColorTableExistance();
}
/**
* Set local color table flag
*/
public function setLocalColorTableExistance(bool $existance = true): self
{
$this->localColorTableExistance = $existance;
return $this;
}
/**
* Get local color table sorted flag
*/
public function getLocalColorTableSorted(): bool
{
return $this->localColorTableSorted;
}
/**
* Set local color table sorted flag
*/
public function setLocalColorTableSorted(bool $sorted = true): self
{
$this->localColorTableSorted = $sorted;
return $this;
}
/**
* Get size of local color table
*/
public function getLocalColorTableSize(): int
{
return $this->localColorTableSize;
}
/**
* Get byte size of global color table
*/
public function getLocalColorTableByteSize(): int
{
return 3 * pow(2, $this->getLocalColorTableSize() + 1);
}
/**
* Set size of local color table
*/
public function setLocalColorTableSize(int $size): self
{
$this->localColorTableSize = $size;
return $this;
}
}

View File

@@ -0,0 +1,201 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class LogicalScreenDescriptor extends AbstractEntity
{
/**
* Width
*/
protected int $width;
/**
* Height
*/
protected int $height;
/**
* Global color table flag
*/
protected bool $globalColorTableExistance = false;
/**
* Sort flag of global color table
*/
protected bool $globalColorTableSorted = false;
/**
* Size of global color table
*/
protected int $globalColorTableSize = 0;
/**
* Background color index
*/
protected int $backgroundColorIndex = 0;
/**
* Color resolution
*/
protected int $bitsPerPixel = 8;
/**
* Pixel aspect ration
*/
protected int $pixelAspectRatio = 0;
/**
* Set size
*/
public function setSize(int $width, int $height): self
{
$this->width = $width;
$this->height = $height;
return $this;
}
/**
* Get width of current instance
*/
public function getWidth(): int
{
return intval($this->width);
}
/**
* Get height of current instance
*/
public function getHeight(): int
{
return intval($this->height);
}
/**
* Determine if global color table is present
*/
public function getGlobalColorTableExistance(): bool
{
return $this->globalColorTableExistance;
}
/**
* Alias of getGlobalColorTableExistance
*/
public function hasGlobalColorTable(): bool
{
return $this->getGlobalColorTableExistance();
}
/**
* Set global color table flag
*/
public function setGlobalColorTableExistance(bool $existance = true): self
{
$this->globalColorTableExistance = $existance;
return $this;
}
/**
* Get global color table sorted flag
*/
public function getGlobalColorTableSorted(): bool
{
return $this->globalColorTableSorted;
}
/**
* Set global color table sorted flag
*/
public function setGlobalColorTableSorted(bool $sorted = true): self
{
$this->globalColorTableSorted = $sorted;
return $this;
}
/**
* Get size of global color table
*/
public function getGlobalColorTableSize(): int
{
return $this->globalColorTableSize;
}
/**
* Get byte size of global color table
*/
public function getGlobalColorTableByteSize(): int
{
return 3 * pow(2, $this->getGlobalColorTableSize() + 1);
}
/**
* Set size of global color table
*/
public function setGlobalColorTableSize(int $size): self
{
$this->globalColorTableSize = $size;
return $this;
}
/**
* Get background color index
*/
public function getBackgroundColorIndex(): int
{
return $this->backgroundColorIndex;
}
/**
* Set background color index
*/
public function setBackgroundColorIndex(int $index): self
{
$this->backgroundColorIndex = $index;
return $this;
}
/**
* Get current pixel aspect ration
*/
public function getPixelAspectRatio(): int
{
return $this->pixelAspectRatio;
}
/**
* Set pixel aspect ratio
*/
public function setPixelAspectRatio(int $ratio): self
{
$this->pixelAspectRatio = $ratio;
return $this;
}
/**
* Get color resolution
*/
public function getBitsPerPixel(): int
{
return $this->bitsPerPixel;
}
/**
* Set color resolution
*/
public function setBitsPerPixel(int $value): self
{
$this->bitsPerPixel = $value;
return $this;
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\Exceptions\FormatException;
use Intervention\Gif\Exceptions\RuntimeException;
class NetscapeApplicationExtension extends ApplicationExtension
{
public const IDENTIFIER = "NETSCAPE";
public const AUTH_CODE = "2.0";
public const SUB_BLOCK_PREFIX = "\x01";
/**
* Create new instance
*
* @throws FormatException
*/
public function __construct()
{
$this->setApplication(self::IDENTIFIER . self::AUTH_CODE);
$this->setBlocks([new DataSubBlock(self::SUB_BLOCK_PREFIX . "\x00\x00")]);
}
/**
* Get number of loops
*
* @throws RuntimeException
*/
public function getLoops(): int
{
$unpacked = unpack('v*', substr($this->getFirstBlock()->getValue(), 1));
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new RuntimeException('Unable to get loop count.');
}
return $unpacked[1];
}
/**
* Set number of loops
*
* @throws FormatException
*/
public function setLoops(int $loops): self
{
$this->setBlocks([
new DataSubBlock(self::SUB_BLOCK_PREFIX . pack('v*', $loops))
]);
return $this;
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractExtension;
class PlainTextExtension extends AbstractExtension
{
public const LABEL = "\x01";
/**
* Array of text
*
* @var array<string>
*/
protected array $text = [];
/**
* Get current text
*
* @return array<string>
*/
public function getText(): array
{
return $this->text;
}
/**
* Add text
*/
public function addText(string $text): self
{
$this->text[] = $text;
return $this;
}
/**
* Set text array of extension
*
* @param array<string> $text
*/
public function setText(array $text): self
{
$this->text = $text;
return $this;
}
/**
* Determine if any text is present
*/
public function hasText(): bool
{
return $this->text !== [];
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class TableBasedImage extends AbstractEntity
{
protected ImageDescriptor $imageDescriptor;
protected ?ColorTable $colorTable = null;
protected ImageData $imageData;
public function getImageDescriptor(): ImageDescriptor
{
return $this->imageDescriptor;
}
public function setImageDescriptor(ImageDescriptor $descriptor): self
{
$this->imageDescriptor = $descriptor;
return $this;
}
public function getImageData(): ImageData
{
return $this->imageData;
}
public function setImageData(ImageData $data): self
{
$this->imageData = $data;
return $this;
}
public function getColorTable(): ?ColorTable
{
return $this->colorTable;
}
public function setColorTable(ColorTable $table): self
{
$this->colorTable = $table;
return $this;
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Blocks;
use Intervention\Gif\AbstractEntity;
class Trailer extends AbstractEntity
{
public const MARKER = "\x3b";
}

View File

@@ -0,0 +1,204 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
use Exception;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\GraphicControlExtension;
use Intervention\Gif\Blocks\ImageDescriptor;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
use Intervention\Gif\Blocks\TableBasedImage;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\EncoderException;
use Intervention\Gif\Traits\CanHandleFiles;
class Builder
{
use CanHandleFiles;
/**
* Create new instance
*/
public function __construct(protected GifDataStream $gif = new GifDataStream())
{
//
}
/**
* Create new canvas
*/
public static function canvas(int $width, int $height): self
{
return (new self())->setSize($width, $height);
}
/**
* Get GifDataStream object we're currently building
*/
public function getGifDataStream(): GifDataStream
{
return $this->gif;
}
/**
* Set canvas size of gif
*/
public function setSize(int $width, int $height): self
{
$this->gif->getLogicalScreenDescriptor()->setSize($width, $height);
return $this;
}
/**
* Set loop count
*
* @throws Exception
*/
public function setLoops(int $loops): self
{
if ($loops < 0) {
throw new Exception('The loop count must be equal to or greater than 0');
}
if ($this->gif->getFrames() === []) {
throw new Exception('Add at least one frame before setting the loop count');
}
// with one single loop the netscape extension must be removed otherwise the
// gif is looped twice because the first repetition always takes place
if ($loops === 1) {
$this->gif->getFirstFrame()?->clearApplicationExtensions();
return $this;
}
// make sure a netscape extension is present to store the loop count
if (!$this->gif->getFirstFrame()?->getNetscapeExtension()) {
$this->gif->getFirstFrame()?->addApplicationExtension(
new NetscapeApplicationExtension()
);
}
// the loop count is reduced by one because what is referred to here as
// the “loop count” actually means repetitions in GIF format, and thus
// the first repetition always takes place. A loop count of 0 howerver
// means infinite repetitions and remains unaltered.
$loops = $loops === 0 ? $loops : $loops - 1;
// add loop count to netscape extension on first frame
$this->gif->getFirstFrame()?->getNetscapeExtension()?->setLoops($loops);
return $this;
}
/**
* Create new animation frame from given source
* which can be path to a file or GIF image data
*
* @throws DecoderException
*/
public function addFrame(
mixed $source,
float $delay = 0,
int $left = 0,
int $top = 0,
bool $interlaced = false
): self {
$frame = new FrameBlock();
$source = Decoder::decode($source);
// store delay
$frame->setGraphicControlExtension(
$this->buildGraphicControlExtension(
$source,
intval($delay * 100)
)
);
// store image
$frame->setTableBasedImage(
$this->buildTableBasedImage($source, $left, $top, $interlaced)
);
// add frame
$this->gif->addFrame($frame);
return $this;
}
/**
* Build new graphic control extension with given delay & disposal method
*/
protected function buildGraphicControlExtension(
GifDataStream $source,
int $delay,
DisposalMethod $disposalMethod = DisposalMethod::BACKGROUND
): GraphicControlExtension {
// create extension
$extension = new GraphicControlExtension($delay, $disposalMethod);
// set transparency index
$control = $source->getFirstFrame()->getGraphicControlExtension();
if ($control && $control->getTransparentColorExistance()) {
$extension->setTransparentColorExistance();
$extension->setTransparentColorIndex(
$control->getTransparentColorIndex()
);
}
return $extension;
}
/**
* Build table based image object from given source
*/
protected function buildTableBasedImage(
GifDataStream $source,
int $left,
int $top,
bool $interlaced
): TableBasedImage {
$block = new TableBasedImage();
$block->setImageDescriptor(new ImageDescriptor());
// set global color table from source as local color table
$block->getImageDescriptor()->setLocalColorTableExistance();
$block->setColorTable($source->getGlobalColorTable());
$block->getImageDescriptor()->setLocalColorTableSorted(
$source->getLogicalScreenDescriptor()->getGlobalColorTableSorted()
);
$block->getImageDescriptor()->setLocalColorTableSize(
$source->getLogicalScreenDescriptor()->getGlobalColorTableSize()
);
$block->getImageDescriptor()->setSize(
$source->getLogicalScreenDescriptor()->getWidth(),
$source->getLogicalScreenDescriptor()->getHeight()
);
// set position
$block->getImageDescriptor()->setPosition($left, $top);
// set interlaced flag
$block->getImageDescriptor()->setInterlaced($interlaced);
// add image data from source
$block->setImageData($source->getFirstFrame()->getImageData());
return $block;
}
/**
* Encode the current build
*
* @throws EncoderException
*/
public function encode(): string
{
return $this->gif->encode();
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\RuntimeException;
use Intervention\Gif\Traits\CanHandleFiles;
class Decoder
{
use CanHandleFiles;
/**
* Decode given input
*
* @throws DecoderException
*/
public static function decode(mixed $input): GifDataStream
{
try {
$handle = match (true) {
self::isFilePath($input) => self::getHandleFromFilePath($input),
is_string($input) => self::getHandleFromData($input),
self::isFileHandle($input) => $input,
default => throw new DecoderException(
'Decoder input must be either file path, file pointer resource or binary data.'
)
};
} catch (RuntimeException $e) {
throw new DecoderException($e->getMessage());
}
rewind($handle);
return GifDataStream::decode($handle);
}
/**
* Determine if input is file pointer resource
*/
private static function isFileHandle(mixed $input): bool
{
return is_resource($input) && get_resource_type($input) === 'stream';
}
}

View File

@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Exceptions\DecoderException;
abstract class AbstractDecoder
{
/**
* Decode current source
*/
abstract public function decode(): mixed;
/**
* Create new instance
*/
public function __construct(protected mixed $handle, protected ?int $length = null)
{
//
}
/**
* Set source to decode
*/
public function setHandle(mixed $handle): self
{
$this->handle = $handle;
return $this;
}
/**
* Read given number of bytes and move file pointer
*
* @throws DecoderException
*/
protected function getNextBytesOrFail(int $length): string
{
if ($length < 1) {
throw new DecoderException('The length passed must be at least one byte.');
}
$bytes = fread($this->handle, $length);
if ($bytes === false || strlen($bytes) !== $length) {
throw new DecoderException('Unexpected end of file.');
}
return $bytes;
}
/**
* Read given number of bytes and move pointer back to previous position
*
* @throws DecoderException
*/
protected function viewNextBytesOrFail(int $length): string
{
$bytes = $this->getNextBytesOrFail($length);
$this->movePointer($length * -1);
return $bytes;
}
/**
* Read next byte and move pointer back to previous position
*
* @throws DecoderException
*/
protected function viewNextByteOrFail(): string
{
return $this->viewNextBytesOrFail(1);
}
/**
* Read all remaining bytes from file handler
*/
protected function getRemainingBytes(): string
{
$all = '';
do {
$byte = fread($this->handle, 1);
$all .= $byte;
} while (!feof($this->handle));
return $all;
}
/**
* Get next byte in stream and move file pointer
*
* @throws DecoderException
*/
protected function getNextByteOrFail(): string
{
return $this->getNextBytesOrFail(1);
}
/**
* Move file pointer on handle by given offset
*/
protected function movePointer(int $offset): self
{
fseek($this->handle, $offset, SEEK_CUR);
return $this;
}
/**
* Decode multi byte value
*
* @throws DecoderException
*/
protected function decodeMultiByte(string $bytes): int
{
$unpacked = unpack('v*', $bytes);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode given bytes.');
}
return $unpacked[1];
}
/**
* Set length
*/
public function setLength(int $length): self
{
$this->length = $length;
return $this;
}
/**
* Get length
*/
public function getLength(): ?int
{
return $this->length;
}
/**
* Get current handle position
*
* @throws DecoderException
*/
public function getPosition(): int
{
$position = ftell($this->handle);
if ($position === false) {
throw new DecoderException('Unable to read current position from handle.');
}
return $position;
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Exceptions\DecoderException;
abstract class AbstractPackedBitDecoder extends AbstractDecoder
{
/**
* Decode packed byte
*
* @throws DecoderException
*/
protected function decodePackedByte(string $byte): int
{
$unpacked = unpack('C', $byte);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to get info block size.');
}
return intval($unpacked[1]);
}
/**
* Determine if packed bit is set
*
* @throws DecoderException
*/
protected function hasPackedBit(string $byte, int $num): bool
{
return (bool) $this->getPackedBits($byte)[$num];
}
/**
* Get packed bits
*
* @throws DecoderException
*/
protected function getPackedBits(string $byte, int $start = 0, int $length = 8): string
{
$bits = str_pad(decbin($this->decodePackedByte($byte)), 8, '0', STR_PAD_LEFT);
return substr($bits, $start, $length);
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\ApplicationExtension;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\FormatException;
class ApplicationExtensionDecoder extends AbstractDecoder
{
/**
* Decode current source
*
* @throws FormatException
* @throws DecoderException
*/
public function decode(): ApplicationExtension
{
$result = new ApplicationExtension();
$this->getNextByteOrFail(); // marker
$this->getNextByteOrFail(); // label
$blocksize = $this->decodeBlockSize($this->getNextByteOrFail());
$application = $this->getNextBytesOrFail($blocksize);
if ($application === NetscapeApplicationExtension::IDENTIFIER . NetscapeApplicationExtension::AUTH_CODE) {
$result = new NetscapeApplicationExtension();
// skip length
$this->getNextByteOrFail();
$result->setBlocks([
new DataSubBlock(
$this->getNextBytesOrFail(3)
)
]);
// skip terminator
$this->getNextByteOrFail();
return $result;
}
$result->setApplication($application);
// decode data sub blocks
$blocksize = $this->decodeBlockSize($this->getNextByteOrFail());
while ($blocksize > 0) {
$result->addBlock(new DataSubBlock($this->getNextBytesOrFail($blocksize)));
$blocksize = $this->decodeBlockSize($this->getNextByteOrFail());
}
return $result;
}
/**
* Decode block size of ApplicationExtension from given byte
*
* @throws DecoderException
*/
protected function decodeBlockSize(string $byte): int
{
$unpacked = @unpack('C', $byte);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode application extension block size.');
}
return intval($unpacked[1]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\Color;
use Intervention\Gif\Exceptions\DecoderException;
class ColorDecoder extends AbstractDecoder
{
/**
* Decode current source to Color
*
* @throws DecoderException
*/
public function decode(): Color
{
$color = new Color();
$color->setRed($this->decodeColorValue($this->getNextByteOrFail()));
$color->setGreen($this->decodeColorValue($this->getNextByteOrFail()));
$color->setBlue($this->decodeColorValue($this->getNextByteOrFail()));
return $color;
}
/**
* Decode red value from source
*
* @throws DecoderException
*/
protected function decodeColorValue(string $byte): int
{
$unpacked = unpack('C', $byte);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode color value.');
}
return $unpacked[1];
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\Color;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Exceptions\DecoderException;
class ColorTableDecoder extends AbstractDecoder
{
/**
* Decode given string to ColorTable
*
* @throws DecoderException
*/
public function decode(): ColorTable
{
$table = new ColorTable();
for ($i = 0; $i < ($this->getLength() / 3); $i++) {
$table->addColor(Color::decode($this->handle));
}
return $table;
}
}

View File

@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Exceptions\DecoderException;
class CommentExtensionDecoder extends AbstractDecoder
{
/**
* Decode current source
*
* @throws DecoderException
*/
public function decode(): CommentExtension
{
$this->getNextBytesOrFail(2); // skip marker & label
$extension = new CommentExtension();
foreach ($this->decodeComments() as $comment) {
$extension->addComment($comment);
}
return $extension;
}
/**
* Decode comment from current source
*
* @throws DecoderException
* @return array<string>
*/
protected function decodeComments(): array
{
$comments = [];
do {
$byte = $this->getNextByteOrFail();
$size = $this->decodeBlocksize($byte);
if ($size > 0) {
$comments[] = $this->getNextBytesOrFail($size);
}
} while ($byte !== CommentExtension::TERMINATOR);
return $comments;
}
/**
* Decode blocksize of following comment
*
* @throws DecoderException
*/
protected function decodeBlocksize(string $byte): int
{
$unpacked = @unpack('C', $byte);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode comment extension block size.');
}
return intval($unpacked[1]);
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\FormatException;
class DataSubBlockDecoder extends AbstractDecoder
{
/**
* Decode current sourc
*
* @throws FormatException
* @throws DecoderException
*/
public function decode(): DataSubBlock
{
$char = $this->getNextByteOrFail();
$unpacked = unpack('C', $char);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode data sub block.');
}
$size = (int) $unpacked[1];
return new DataSubBlock($this->getNextBytesOrFail($size));
}
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\AbstractExtension;
use Intervention\Gif\Blocks\ApplicationExtension;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\GraphicControlExtension;
use Intervention\Gif\Blocks\ImageDescriptor;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
use Intervention\Gif\Blocks\PlainTextExtension;
use Intervention\Gif\Blocks\TableBasedImage;
use Intervention\Gif\Exceptions\DecoderException;
class FrameBlockDecoder extends AbstractDecoder
{
/**
* Decode FrameBlock
*
* @throws DecoderException
*/
public function decode(): FrameBlock
{
$frame = new FrameBlock();
do {
$block = match ($this->viewNextBytesOrFail(2)) {
AbstractExtension::MARKER . GraphicControlExtension::LABEL
=> GraphicControlExtension::decode($this->handle),
AbstractExtension::MARKER . NetscapeApplicationExtension::LABEL
=> NetscapeApplicationExtension::decode($this->handle),
AbstractExtension::MARKER . ApplicationExtension::LABEL
=> ApplicationExtension::decode($this->handle),
AbstractExtension::MARKER . PlainTextExtension::LABEL
=> PlainTextExtension::decode($this->handle),
AbstractExtension::MARKER . CommentExtension::LABEL
=> CommentExtension::decode($this->handle),
default => match ($this->viewNextByteOrFail()) {
ImageDescriptor::SEPARATOR => TableBasedImage::decode($this->handle),
default => throw new DecoderException('Unable to decode Data Block'),
}
};
$frame->addEntity($block);
} while (!($block instanceof TableBasedImage));
return $frame;
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\AbstractExtension;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\Header;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
use Intervention\Gif\Blocks\Trailer;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\GifDataStream;
class GifDataStreamDecoder extends AbstractDecoder
{
/**
* Decode current source to GifDataStream
*
* @throws DecoderException
*/
public function decode(): GifDataStream
{
$gif = new GifDataStream();
$gif->setHeader(
Header::decode($this->handle),
);
$gif->setLogicalScreenDescriptor(
LogicalScreenDescriptor::decode($this->handle),
);
if ($gif->getLogicalScreenDescriptor()->hasGlobalColorTable()) {
$length = $gif->getLogicalScreenDescriptor()->getGlobalColorTableByteSize();
$gif->setGlobalColorTable(
ColorTable::decode($this->handle, $length)
);
}
while ($this->viewNextByteOrFail() !== Trailer::MARKER) {
match ($this->viewNextBytesOrFail(2)) {
// trailing "global" comment blocks which are not part of "FrameBlock"
AbstractExtension::MARKER . CommentExtension::LABEL
=> $gif->addComment(
CommentExtension::decode($this->handle)
),
default => $gif->addFrame(
FrameBlock::decode($this->handle)
),
};
}
return $gif;
}
}

View File

@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\GraphicControlExtension;
use Intervention\Gif\DisposalMethod;
use Intervention\Gif\Exceptions\DecoderException;
class GraphicControlExtensionDecoder extends AbstractPackedBitDecoder
{
/**
* Decode given string to current instance
*
* @throws DecoderException
*/
public function decode(): GraphicControlExtension
{
$result = new GraphicControlExtension();
// bytes 1-3
$this->getNextBytesOrFail(3); // skip marker, label & bytesize
// byte #4
$packedField = $this->getNextByteOrFail();
$result->setDisposalMethod($this->decodeDisposalMethod($packedField));
$result->setUserInput($this->decodeUserInput($packedField));
$result->setTransparentColorExistance($this->decodeTransparentColorExistance($packedField));
// bytes 5-6
$result->setDelay($this->decodeDelay($this->getNextBytesOrFail(2)));
// byte #7
$result->setTransparentColorIndex($this->decodeTransparentColorIndex(
$this->getNextByteOrFail()
));
// byte #8 (terminator)
$this->getNextByteOrFail();
return $result;
}
/**
* Decode disposal method
*
* @throws DecoderException
*/
protected function decodeDisposalMethod(string $byte): DisposalMethod
{
return DisposalMethod::from(
intval(bindec($this->getPackedBits($byte, 3, 3)))
);
}
/**
* Decode user input flag
*
* @throws DecoderException
*/
protected function decodeUserInput(string $byte): bool
{
return $this->hasPackedBit($byte, 6);
}
/**
* Decode transparent color existance
*
* @throws DecoderException
*/
protected function decodeTransparentColorExistance(string $byte): bool
{
return $this->hasPackedBit($byte, 7);
}
/**
* Decode delay value
*
* @throws DecoderException
*/
protected function decodeDelay(string $bytes): int
{
$unpacked = unpack('v*', $bytes);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode animation delay.');
}
return $unpacked[1];
}
/**
* Decode transparent color index
*
* @throws DecoderException
*/
protected function decodeTransparentColorIndex(string $byte): int
{
$unpacked = unpack('C', $byte);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode transparent color index.');
}
return $unpacked[1];
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Blocks\Header;
class HeaderDecoder extends AbstractDecoder
{
/**
* Decode current sourc
*
* @throws DecoderException
*/
public function decode(): Header
{
$header = new Header();
$header->setVersion($this->decodeVersion());
return $header;
}
/**
* Decode version string
*
* @throws DecoderException
*/
protected function decodeVersion(): string
{
$parsed = (bool) preg_match("/^GIF(?P<version>[0-9]{2}[a-z])$/", $this->getNextBytesOrFail(6), $matches);
if ($parsed === false) {
throw new DecoderException('Unable to parse file header.');
}
return $matches['version'];
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\AbstractEntity;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Blocks\ImageData;
use Intervention\Gif\Exceptions\DecoderException;
use Intervention\Gif\Exceptions\FormatException;
class ImageDataDecoder extends AbstractDecoder
{
/**
* Decode current source
*
* @throws DecoderException
* @throws FormatException
*/
public function decode(): ImageData
{
$data = new ImageData();
// LZW min. code size
$char = $this->getNextByteOrFail();
$unpacked = unpack('C', $char);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode lzw min. code size.');
}
$data->setLzwMinCodeSize(intval($unpacked[1]));
do {
// decode sub blocks
$char = $this->getNextByteOrFail();
$unpacked = unpack('C', $char);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode image data sub block.');
}
$size = intval($unpacked[1]);
if ($size > 0) {
$data->addBlock(new DataSubBlock($this->getNextBytesOrFail($size)));
}
} while ($char !== AbstractEntity::TERMINATOR);
return $data;
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\ImageDescriptor;
use Intervention\Gif\Exceptions\DecoderException;
class ImageDescriptorDecoder extends AbstractPackedBitDecoder
{
/**
* Decode given string to current instance
*
* @throws DecoderException
*/
public function decode(): ImageDescriptor
{
$descriptor = new ImageDescriptor();
$this->getNextByteOrFail(); // skip separator
$descriptor->setPosition(
$this->decodeMultiByte($this->getNextBytesOrFail(2)),
$this->decodeMultiByte($this->getNextBytesOrFail(2))
);
$descriptor->setSize(
$this->decodeMultiByte($this->getNextBytesOrFail(2)),
$this->decodeMultiByte($this->getNextBytesOrFail(2))
);
$packedField = $this->getNextByteOrFail();
$descriptor->setLocalColorTableExistance(
$this->decodeLocalColorTableExistance($packedField)
);
$descriptor->setLocalColorTableSorted(
$this->decodeLocalColorTableSorted($packedField)
);
$descriptor->setLocalColorTableSize(
$this->decodeLocalColorTableSize($packedField)
);
$descriptor->setInterlaced(
$this->decodeInterlaced($packedField)
);
return $descriptor;
}
/**
* Decode local color table existance
*
* @throws DecoderException
*/
protected function decodeLocalColorTableExistance(string $byte): bool
{
return $this->hasPackedBit($byte, 0);
}
/**
* Decode local color table sort method
*
* @throws DecoderException
*/
protected function decodeLocalColorTableSorted(string $byte): bool
{
return $this->hasPackedBit($byte, 2);
}
/**
* Decode local color table size
*
* @throws DecoderException
*/
protected function decodeLocalColorTableSize(string $byte): int
{
return (int) bindec($this->getPackedBits($byte, 5, 3));
}
/**
* Decode interlaced flag
*
* @throws DecoderException
*/
protected function decodeInterlaced(string $byte): bool
{
return $this->hasPackedBit($byte, 1);
}
}

View File

@@ -0,0 +1,162 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
use Intervention\Gif\Exceptions\DecoderException;
class LogicalScreenDescriptorDecoder extends AbstractPackedBitDecoder
{
/**
* Decode given string to current instance
*
* @throws DecoderException
*/
public function decode(): LogicalScreenDescriptor
{
$logicalScreenDescriptor = new LogicalScreenDescriptor();
// bytes 1-4
$logicalScreenDescriptor->setSize(
$this->decodeWidth($this->getNextBytesOrFail(2)),
$this->decodeHeight($this->getNextBytesOrFail(2))
);
// byte 5
$packedField = $this->getNextByteOrFail();
$logicalScreenDescriptor->setGlobalColorTableExistance(
$this->decodeGlobalColorTableExistance($packedField)
);
$logicalScreenDescriptor->setBitsPerPixel(
$this->decodeBitsPerPixel($packedField)
);
$logicalScreenDescriptor->setGlobalColorTableSorted(
$this->decodeGlobalColorTableSorted($packedField)
);
$logicalScreenDescriptor->setGlobalColorTableSize(
$this->decodeGlobalColorTableSize($packedField)
);
// byte 6
$logicalScreenDescriptor->setBackgroundColorIndex(
$this->decodeBackgroundColorIndex($this->getNextByteOrFail())
);
// byte 7
$logicalScreenDescriptor->setPixelAspectRatio(
$this->decodePixelAspectRatio($this->getNextByteOrFail())
);
return $logicalScreenDescriptor;
}
/**
* Decode width
*
* @throws DecoderException
*/
protected function decodeWidth(string $source): int
{
$unpacked = unpack('v*', $source);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode width.');
}
return $unpacked[1];
}
/**
* Decode height
*
* @throws DecoderException
*/
protected function decodeHeight(string $source): int
{
$unpacked = unpack('v*', $source);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode height.');
}
return $unpacked[1];
}
/**
* Decode existance of global color table
*
* @throws DecoderException
*/
protected function decodeGlobalColorTableExistance(string $byte): bool
{
return $this->hasPackedBit($byte, 0);
}
/**
* Decode color resolution in bits per pixel
*
* @throws DecoderException
*/
protected function decodeBitsPerPixel(string $byte): int
{
return intval(bindec($this->getPackedBits($byte, 1, 3))) + 1;
}
/**
* Decode global color table sorted status
*
* @throws DecoderException
*/
protected function decodeGlobalColorTableSorted(string $byte): bool
{
return $this->hasPackedBit($byte, 4);
}
/**
* Decode size of global color table
*
* @throws DecoderException
*/
protected function decodeGlobalColorTableSize(string $byte): int
{
return intval(bindec($this->getPackedBits($byte, 5, 3)));
}
/**
* Decode background color index
*
* @throws DecoderException
*/
protected function decodeBackgroundColorIndex(string $source): int
{
$unpacked = unpack('C', $source);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode background color index.');
}
return $unpacked[1];
}
/**
* Decode pixel aspect ratio
*
* @throws DecoderException
*/
protected function decodePixelAspectRatio(string $source): int
{
$unpacked = unpack('C', $source);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode pixel aspect ratio.');
}
return $unpacked[1];
}
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
class NetscapeApplicationExtensionDecoder extends ApplicationExtensionDecoder
{
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\PlainTextExtension;
use Intervention\Gif\Exceptions\DecoderException;
class PlainTextExtensionDecoder extends AbstractDecoder
{
/**
* Decode current source
*
* @throws DecoderException
*/
public function decode(): PlainTextExtension
{
$extension = new PlainTextExtension();
// skip marker & label
$this->getNextBytesOrFail(2);
// skip info block
$this->getNextBytesOrFail($this->getInfoBlockSize());
// text blocks
$extension->setText($this->decodeTextBlocks());
return $extension;
}
/**
* Get number of bytes in header block
*
* @throws DecoderException
*/
protected function getInfoBlockSize(): int
{
$unpacked = unpack('C', $this->getNextByteOrFail());
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode info block size.');
}
return $unpacked[1];
}
/**
* Decode text sub blocks
*
* @throws DecoderException
* @return array<string>
*/
protected function decodeTextBlocks(): array
{
$blocks = [];
do {
$char = $this->getNextByteOrFail();
$unpacked = unpack('C', $char);
if ($unpacked === false || !array_key_exists(1, $unpacked)) {
throw new DecoderException('Unable to decode text block.');
}
$size = (int) $unpacked[1];
if ($size > 0) {
$blocks[] = $this->getNextBytesOrFail($size);
}
} while ($char !== PlainTextExtension::TERMINATOR);
return $blocks;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Decoders;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Blocks\ImageData;
use Intervention\Gif\Blocks\ImageDescriptor;
use Intervention\Gif\Blocks\TableBasedImage;
use Intervention\Gif\Exceptions\DecoderException;
class TableBasedImageDecoder extends AbstractDecoder
{
/**
* Decode TableBasedImage
*
* @throws DecoderException
*/
public function decode(): TableBasedImage
{
$block = new TableBasedImage();
$block->setImageDescriptor(ImageDescriptor::decode($this->handle));
if ($block->getImageDescriptor()->hasLocalColorTable()) {
$block->setColorTable(
ColorTable::decode(
$this->handle,
$block->getImageDescriptor()->getLocalColorTableByteSize()
)
);
}
$block->setImageData(
ImageData::decode($this->handle)
);
return $block;
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
enum DisposalMethod: int
{
case UNDEFINED = 0;
case NONE = 1; // overlay each frame in sequence
case BACKGROUND = 2; // clear to background (as indicated by the logical screen descriptor)
case PREVIOUS = 3; // restore the canvas to its previous state
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
abstract class AbstractEncoder
{
/**
* Encode current source
*/
abstract public function encode(): string;
/**
* Create new instance
*/
public function __construct(protected mixed $source)
{
//
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\ApplicationExtension;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Exceptions\EncoderException;
class ApplicationExtensionEncoder extends AbstractEncoder
{
/**
* Create new decoder instance
*/
public function __construct(ApplicationExtension $source)
{
$this->source = $source;
}
/**
* Encode current source
*
* @throws EncoderException
*/
public function encode(): string
{
return implode('', [
ApplicationExtension::MARKER,
ApplicationExtension::LABEL,
pack('C', $this->source->getBlockSize()),
$this->source->getApplication(),
implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())),
ApplicationExtension::TERMINATOR,
]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\Color;
class ColorEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(Color $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
$this->encodeColorValue($this->source->getRed()),
$this->encodeColorValue($this->source->getGreen()),
$this->encodeColorValue($this->source->getBlue()),
]);
}
/**
* Encode color value
*/
protected function encodeColorValue(int $value): string
{
return pack('C', $value);
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\Color;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Exceptions\EncoderException;
class ColorTableEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(ColorTable $source)
{
$this->source = $source;
}
/**
* Encode current source
*
* @throws EncoderException
*/
public function encode(): string
{
return implode('', array_map(
fn(Color $color): string => $color->encode(),
$this->source->getColors(),
));
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\CommentExtension;
class CommentExtensionEncoder extends AbstractEncoder
{
/**
* Create new decoder instance
*/
public function __construct(CommentExtension $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
CommentExtension::MARKER,
CommentExtension::LABEL,
$this->encodeComments(),
CommentExtension::TERMINATOR,
]);
}
/**
* Encode comment blocks
*/
protected function encodeComments(): string
{
return implode('', array_map(function (string $comment): string {
return pack('C', strlen($comment)) . $comment;
}, $this->source->getComments()));
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\DataSubBlock;
class DataSubBlockEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(DataSubBlock $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return pack('C', $this->source->getSize()) . $this->source->getValue();
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\ApplicationExtension;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Exceptions\EncoderException;
class FrameBlockEncoder extends AbstractEncoder
{
/**
* Create new decoder instance
*/
public function __construct(FrameBlock $source)
{
$this->source = $source;
}
/**
* @throws EncoderException
*/
public function encode(): string
{
$graphicControlExtension = $this->source->getGraphicControlExtension();
$colorTable = $this->source->getColorTable();
$plainTextExtension = $this->source->getPlainTextExtension();
return implode('', [
implode('', array_map(
fn(ApplicationExtension $extension): string => $extension->encode(),
$this->source->getApplicationExtensions(),
)),
implode('', array_map(
fn(CommentExtension $extension): string => $extension->encode(),
$this->source->getCommentExtensions(),
)),
$plainTextExtension ? $plainTextExtension->encode() : '',
$graphicControlExtension ? $graphicControlExtension->encode() : '',
$this->source->getImageDescriptor()->encode(),
$colorTable ? $colorTable->encode() : '',
$this->source->getImageData()->encode(),
]);
}
}

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Exceptions\EncoderException;
use Intervention\Gif\GifDataStream;
class GifDataStreamEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(GifDataStream $source)
{
$this->source = $source;
}
/**
* Encode current source
*
* @throws EncoderException
*/
public function encode(): string
{
return implode('', [
$this->source->getHeader()->encode(),
$this->source->getLogicalScreenDescriptor()->encode(),
$this->maybeEncodeGlobalColorTable(),
$this->encodeFrames(),
$this->encodeComments(),
$this->source->getTrailer()->encode(),
]);
}
protected function maybeEncodeGlobalColorTable(): string
{
if (!$this->source->hasGlobalColorTable()) {
return '';
}
return $this->source->getGlobalColorTable()->encode();
}
/**
* Encode data blocks of source
*
* @throws EncoderException
*/
protected function encodeFrames(): string
{
return implode('', array_map(
fn(FrameBlock $frame): string => $frame->encode(),
$this->source->getFrames(),
));
}
/**
* Encode comment extension blocks of source
*
* @throws EncoderException
*/
protected function encodeComments(): string
{
return implode('', array_map(
fn(CommentExtension $commentExtension): string => $commentExtension->encode(),
$this->source->getComments()
));
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\GraphicControlExtension;
class GraphicControlExtensionEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(GraphicControlExtension $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
GraphicControlExtension::MARKER,
GraphicControlExtension::LABEL,
GraphicControlExtension::BLOCKSIZE,
$this->encodePackedField(),
$this->encodeDelay(),
$this->encodeTransparentColorIndex(),
GraphicControlExtension::TERMINATOR,
]);
}
/**
* Encode delay time
*/
protected function encodeDelay(): string
{
return pack('v*', $this->source->getDelay());
}
/**
* Encode transparent color index
*/
protected function encodeTransparentColorIndex(): string
{
return pack('C', $this->source->getTransparentColorIndex());
}
/**
* Encode packed field
*/
protected function encodePackedField(): string
{
return pack('C', bindec(implode('', [
str_pad('0', 3, '0', STR_PAD_LEFT),
str_pad(decbin($this->source->getDisposalMethod()->value), 3, '0', STR_PAD_LEFT),
(int) $this->source->getUserInput(),
(int) $this->source->getTransparentColorExistance(),
])));
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\Header;
class HeaderEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(Header $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return Header::SIGNATURE . $this->source->getVersion();
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\AbstractEntity;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Exceptions\EncoderException;
use Intervention\Gif\Blocks\ImageData;
class ImageDataEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(ImageData $source)
{
$this->source = $source;
}
/**
* Encode current source
*
* @throws EncoderException
*/
public function encode(): string
{
if (!$this->source->hasBlocks()) {
throw new EncoderException("No data blocks in ImageData.");
}
return implode('', [
pack('C', $this->source->getLzwMinCodeSize()),
implode('', array_map(
fn(DataSubBlock $block): string => $block->encode(),
$this->source->getBlocks(),
)),
AbstractEntity::TERMINATOR,
]);
}
}

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\ImageDescriptor;
class ImageDescriptorEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(ImageDescriptor $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
ImageDescriptor::SEPARATOR,
$this->encodeLeft(),
$this->encodeTop(),
$this->encodeWidth(),
$this->encodeHeight(),
$this->encodePackedField(),
]);
}
/**
* Encode left value
*/
protected function encodeLeft(): string
{
return pack('v*', $this->source->getLeft());
}
/**
* Encode top value
*/
protected function encodeTop(): string
{
return pack('v*', $this->source->getTop());
}
/**
* Encode width value
*/
protected function encodeWidth(): string
{
return pack('v*', $this->source->getWidth());
}
/**
* Encode height value
*/
protected function encodeHeight(): string
{
return pack('v*', $this->source->getHeight());
}
/**
* Encode size of local color table
*/
protected function encodeLocalColorTableSize(): string
{
return str_pad(decbin($this->source->getLocalColorTableSize()), 3, '0', STR_PAD_LEFT);
}
/**
* Encode reserved field
*/
protected function encodeReservedField(): string
{
return str_pad('0', 2, '0', STR_PAD_LEFT);
}
/**
* Encode packed field
*/
protected function encodePackedField(): string
{
return pack('C', bindec(implode('', [
(int) $this->source->getLocalColorTableExistance(),
(int) $this->source->isInterlaced(),
(int) $this->source->getLocalColorTableSorted(),
$this->encodeReservedField(),
$this->encodeLocalColorTableSize(),
])));
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
class LogicalScreenDescriptorEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(LogicalScreenDescriptor $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
$this->encodeWidth(),
$this->encodeHeight(),
$this->encodePackedField(),
$this->encodeBackgroundColorIndex(),
$this->encodePixelAspectRatio(),
]);
}
/**
* Encode width of current instance
*/
protected function encodeWidth(): string
{
return pack('v*', $this->source->getWidth());
}
/**
* Encode height of current instance
*/
protected function encodeHeight(): string
{
return pack('v*', $this->source->getHeight());
}
/**
* Encode background color index of global color table
*/
protected function encodeBackgroundColorIndex(): string
{
return pack('C', $this->source->getBackgroundColorIndex());
}
/**
* Encode pixel aspect ratio
*/
protected function encodePixelAspectRatio(): string
{
return pack('C', $this->source->getPixelAspectRatio());
}
/**
* Return color resolution for encoding
*/
protected function encodeColorResolution(): string
{
return str_pad(decbin($this->source->getBitsPerPixel() - 1), 3, '0', STR_PAD_LEFT);
}
/**
* Encode size of global color table
*/
protected function encodeGlobalColorTableSize(): string
{
return str_pad(decbin($this->source->getGlobalColorTableSize()), 3, '0', STR_PAD_LEFT);
}
/**
* Encode packed field of current instance
*/
protected function encodePackedField(): string
{
return pack('C', bindec(implode('', [
(int) $this->source->getGlobalColorTableExistance(),
$this->encodeColorResolution(),
(int) $this->source->getGlobalColorTableSorted(),
$this->encodeGlobalColorTableSize(),
])));
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\ApplicationExtension;
use Intervention\Gif\Blocks\DataSubBlock;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
class NetscapeApplicationExtensionEncoder extends ApplicationExtensionEncoder
{
/**
* Create new decoder instance
*/
public function __construct(NetscapeApplicationExtension $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return implode('', [
ApplicationExtension::MARKER,
ApplicationExtension::LABEL,
pack('C', $this->source->getBlockSize()),
$this->source->getApplication(),
implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())),
ApplicationExtension::TERMINATOR,
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\PlainTextExtension;
class PlainTextExtensionEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(PlainTextExtension $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
if (!$this->source->hasText()) {
return '';
}
return implode('', [
PlainTextExtension::MARKER,
PlainTextExtension::LABEL,
$this->encodeHead(),
$this->encodeTexts(),
PlainTextExtension::TERMINATOR,
]);
}
/**
* Encode head block
*/
protected function encodeHead(): string
{
return "\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
}
/**
* Encode text chunks
*/
protected function encodeTexts(): string
{
return implode('', array_map(
fn(string $text): string => pack('C', strlen($text)) . $text,
$this->source->getText(),
));
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\TableBasedImage;
class TableBasedImageEncoder extends AbstractEncoder
{
public function __construct(TableBasedImage $source)
{
$this->source = $source;
}
public function encode(): string
{
return implode('', [
$this->source->getImageDescriptor()->encode(),
$this->source->getColorTable() ? $this->source->getColorTable()->encode() : '',
$this->source->getImageData()->encode(),
]);
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Encoders;
use Intervention\Gif\Blocks\Trailer;
class TrailerEncoder extends AbstractEncoder
{
/**
* Create new instance
*/
public function __construct(Trailer $source)
{
$this->source = $source;
}
/**
* Encode current source
*/
public function encode(): string
{
return Trailer::MARKER;
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Exceptions;
class DecoderException extends RuntimeException
{
//
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Exceptions;
class EncoderException extends RuntimeException
{
//
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Exceptions;
class FormatException extends RuntimeException
{
//
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Exceptions;
class NotReadableException extends RuntimeException
{
//
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Exceptions;
class RuntimeException extends \RuntimeException
{
//
}

View File

@@ -0,0 +1,192 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\Header;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
use Intervention\Gif\Blocks\NetscapeApplicationExtension;
use Intervention\Gif\Blocks\Trailer;
class GifDataStream extends AbstractEntity
{
/**
* Create new instance
*
* @param array<FrameBlock> $frames
* @param array<CommentExtension> $comments
*/
public function __construct(
protected Header $header = new Header(),
protected LogicalScreenDescriptor $logicalScreenDescriptor = new LogicalScreenDescriptor(),
protected ?ColorTable $globalColorTable = null,
protected array $frames = [],
protected array $comments = []
) {
//
}
/**
* Get header
*/
public function getHeader(): Header
{
return $this->header;
}
/**
* Set header
*/
public function setHeader(Header $header): self
{
$this->header = $header;
return $this;
}
/**
* Get logical screen descriptor
*/
public function getLogicalScreenDescriptor(): LogicalScreenDescriptor
{
return $this->logicalScreenDescriptor;
}
/**
* Set logical screen descriptor
*/
public function setLogicalScreenDescriptor(LogicalScreenDescriptor $descriptor): self
{
$this->logicalScreenDescriptor = $descriptor;
return $this;
}
/**
* Return global color table if available else null
*/
public function getGlobalColorTable(): ?ColorTable
{
return $this->globalColorTable;
}
/**
* Set global color table
*/
public function setGlobalColorTable(ColorTable $table): self
{
$this->globalColorTable = $table;
$this->logicalScreenDescriptor->setGlobalColorTableExistance(true);
$this->logicalScreenDescriptor->setGlobalColorTableSize(
$table->getLogicalSize()
);
return $this;
}
/**
* Get main graphic control extension
*/
public function getMainApplicationExtension(): ?NetscapeApplicationExtension
{
foreach ($this->frames as $frame) {
if ($extension = $frame->getNetscapeExtension()) {
return $extension;
}
}
return null;
}
/**
* Get array of frames
*
* @return array<FrameBlock>
*/
public function getFrames(): array
{
return $this->frames;
}
/**
* Return array of "global" comments
*
* @return array<CommentExtension>
*/
public function getComments(): array
{
return $this->comments;
}
/**
* Return first frame
*/
public function getFirstFrame(): ?FrameBlock
{
if (!array_key_exists(0, $this->frames)) {
return null;
}
return $this->frames[0];
}
/**
* Add frame
*/
public function addFrame(FrameBlock $frame): self
{
$this->frames[] = $frame;
return $this;
}
/**
* Add comment extension
*/
public function addComment(CommentExtension $comment): self
{
$this->comments[] = $comment;
return $this;
}
/**
* Set the current data
*
* @param array<FrameBlock> $frames
*/
public function setFrames(array $frames): self
{
$this->frames = $frames;
return $this;
}
/**
* Get trailer
*/
public function getTrailer(): Trailer
{
return new Trailer();
}
/**
* Determine if gif is animated
*/
public function isAnimated(): bool
{
return count($this->getFrames()) > 1;
}
/**
* Determine if global color table is set
*/
public function hasGlobalColorTable(): bool
{
return !is_null($this->globalColorTable);
}
}

View File

@@ -0,0 +1,281 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif;
use ArrayIterator;
use GdImage;
use Intervention\Gif\Exceptions\EncoderException;
use IteratorAggregate;
use Traversable;
/**
* @implements IteratorAggregate<GifDataStream>
*/
class Splitter implements IteratorAggregate
{
/**
* Single frames resolved to GifDataStream
*
* @var array<GifDataStream>
*/
protected array $frames = [];
/**
* Delays of each frame
*
* @var array<int>
*/
protected array $delays = [];
/**
* Create new instance
*/
public function __construct(protected GifDataStream $stream)
{
//
}
/**
* Static constructor method
*/
public static function create(GifDataStream $stream): self
{
return new self($stream);
}
/**
* Iterator
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->frames);
}
/**
* Get frames
*
* @return array<GifDataStream>
*/
public function getFrames(): array
{
return $this->frames;
}
/**
* Get delays
*
* @return array<int>
*/
public function getDelays(): array
{
return $this->delays;
}
/**
* Set stream of instance
*/
public function setStream(GifDataStream $stream): self
{
$this->stream = $stream;
return $this;
}
/**
* Split current stream into array of seperate streams for each frame
*/
public function split(): self
{
$this->frames = [];
foreach ($this->stream->getFrames() as $frame) {
// create separate stream for each frame
$gif = Builder::canvas(
$this->stream->getLogicalScreenDescriptor()->getWidth(),
$this->stream->getLogicalScreenDescriptor()->getHeight()
)->getGifDataStream();
// check if working stream has global color table
if ($this->stream->hasGlobalColorTable()) {
$gif->setGlobalColorTable($this->stream->getGlobalColorTable());
$gif->getLogicalScreenDescriptor()->setGlobalColorTableExistance(true);
$gif->getLogicalScreenDescriptor()->setGlobalColorTableSorted(
$this->stream->getLogicalScreenDescriptor()->getGlobalColorTableSorted()
);
$gif->getLogicalScreenDescriptor()->setGlobalColorTableSize(
$this->stream->getLogicalScreenDescriptor()->getGlobalColorTableSize()
);
$gif->getLogicalScreenDescriptor()->setBackgroundColorIndex(
$this->stream->getLogicalScreenDescriptor()->getBackgroundColorIndex()
);
$gif->getLogicalScreenDescriptor()->setPixelAspectRatio(
$this->stream->getLogicalScreenDescriptor()->getPixelAspectRatio()
);
$gif->getLogicalScreenDescriptor()->setBitsPerPixel(
$this->stream->getLogicalScreenDescriptor()->getBitsPerPixel()
);
}
// copy original frame
$gif->addFrame($frame);
$this->frames[] = $gif;
$this->delays[] = match (is_object($frame->getGraphicControlExtension())) {
true => $frame->getGraphicControlExtension()->getDelay(),
default => 0,
};
}
return $this;
}
/**
* Return array of GD library resources for each frame
*
* @throws EncoderException
* @return array<GdImage>
*/
public function toResources(): array
{
$resources = [];
foreach ($this->frames as $frame) {
$resource = imagecreatefromstring($frame->encode());
if ($resource === false) {
throw new EncoderException('Unable to extract animation frames.');
}
imagepalettetotruecolor($resource);
imagesavealpha($resource, true);
$resources[] = $resource;
}
return $resources;
}
/**
* Return array of coalesced GD library resources for each frame
*
* @throws EncoderException
* @return array<GdImage>
*/
public function coalesceToResources(): array
{
$resources = $this->toResources();
// static gif files don't need to be coalesced
if (count($resources) === 1) {
return $resources;
}
$width = imagesx($resources[0]);
$height = imagesy($resources[0]);
$transparent = imagecolortransparent($resources[0]);
foreach ($resources as $key => $resource) {
// get meta data
$gif = $this->frames[$key];
$descriptor = $gif->getFirstFrame()->getImageDescriptor();
$offset_x = $descriptor->getLeft();
$offset_y = $descriptor->getTop();
$w = $descriptor->getWidth();
$h = $descriptor->getHeight();
if (in_array($this->getDisposalMethod($gif), [DisposalMethod::NONE, DisposalMethod::PREVIOUS])) {
if ($key >= 1) {
// create normalized gd image
$canvas = imagecreatetruecolor($width, $height);
if (imagecolortransparent($resource) != -1) {
$transparent = imagecolortransparent($resource);
} else {
$transparent = imagecolorallocatealpha($resource, 255, 0, 255, 127);
}
if (!is_int($transparent)) {
throw new EncoderException('Animation frames cannot be converted into resources.');
}
// fill with transparent
imagefill($canvas, 0, 0, $transparent);
imagecolortransparent($canvas, $transparent);
imagealphablending($canvas, true);
// insert last as base
imagecopy(
$canvas,
$resources[$key - 1],
0,
0,
0,
0,
$width,
$height
);
// insert resource
imagecopy(
$canvas,
$resource,
$offset_x,
$offset_y,
0,
0,
$w,
$h
);
} else {
imagealphablending($resource, true);
$canvas = $resource;
}
} else {
// create normalized gd image
$canvas = imagecreatetruecolor($width, $height);
if (imagecolortransparent($resource) != -1) {
$transparent = imagecolortransparent($resource);
} else {
$transparent = imagecolorallocatealpha($resource, 255, 0, 255, 127);
}
if (!is_int($transparent)) {
throw new EncoderException('Animation frames cannot be converted into resources.');
}
// fill with transparent
imagefill($canvas, 0, 0, $transparent);
imagecolortransparent($canvas, $transparent);
imagealphablending($canvas, true);
// insert frame resource
imagecopy(
$canvas,
$resource,
$offset_x,
$offset_y,
0,
0,
$w,
$h
);
}
$resources[$key] = $canvas;
}
return $resources;
}
/**
* Find and return disposal method of given gif data stream
*/
private function getDisposalMethod(GifDataStream $gif): DisposalMethod
{
return $gif->getFirstFrame()->getGraphicControlExtension()->getDisposalMethod();
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Traits;
use Intervention\Gif\Decoders\AbstractDecoder;
use Intervention\Gif\Exceptions\DecoderException;
trait CanDecode
{
/**
* Decode current instance
*
* @throws DecoderException
*/
public static function decode(mixed $source, ?int $length = null): mixed
{
return self::getDecoder($source, $length)->decode();
}
/**
* Get decoder for current instance
*
* @throws DecoderException
*/
protected static function getDecoder(mixed $source, ?int $length = null): AbstractDecoder
{
$classname = sprintf('Intervention\Gif\Decoders\%sDecoder', self::getShortClassname());
if (!class_exists($classname)) {
throw new DecoderException("Decoder for '" . static::class . "' not found.");
}
$decoder = new $classname($source, $length);
if (!($decoder instanceof AbstractDecoder)) {
throw new DecoderException("Decoder for '" . static::class . "' not found.");
}
return $decoder;
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Traits;
use Intervention\Gif\Encoders\AbstractEncoder;
use Intervention\Gif\Exceptions\EncoderException;
trait CanEncode
{
/**
* Encode current entity
*
* @throws EncoderException
*/
public function encode(): string
{
return $this->getEncoder()->encode();
}
/**
* Get encoder object for current entity
*
* @throws EncoderException
*/
protected function getEncoder(): AbstractEncoder
{
$classname = sprintf('Intervention\Gif\Encoders\%sEncoder', $this->getShortClassname());
if (!class_exists($classname)) {
throw new EncoderException("Encoder for '" . $this::class . "' not found.");
}
$encoder = new $classname($this);
if (!($encoder instanceof AbstractEncoder)) {
throw new EncoderException("Encoder for '" . $this::class . "' not found.");
}
return $encoder;
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Intervention\Gif\Traits;
use Intervention\Gif\Exceptions\RuntimeException;
trait CanHandleFiles
{
/**
* Determines if input is file path
*/
private static function isFilePath(mixed $input): bool
{
return is_string($input) && !self::hasNullBytes($input) && @is_file($input);
}
/**
* Determine if given string contains null bytes
*/
private static function hasNullBytes(string $string): bool
{
return str_contains($string, chr(0));
}
/**
* Create file pointer from given gif image data
*
* @throws RuntimeException
*/
private static function getHandleFromData(string $data): mixed
{
$handle = fopen('php://temp', 'r+');
if ($handle === false) {
throw new RuntimeException('Unable to create tempory file handle.');
}
fwrite($handle, $data);
rewind($handle);
return $handle;
}
/**
* Create file pounter from given file path
*/
private static function getHandleFromFilePath(string $path): mixed
{
return fopen($path, 'rb');
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2013-present Oliver Vogel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,51 @@
{
"name": "intervention/image",
"description": "PHP Image Processing",
"homepage": "https://image.intervention.io",
"keywords": [
"image",
"gd",
"imagick",
"watermark",
"thumbnail",
"resize"
],
"license": "MIT",
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io"
}
],
"require": {
"php": "^8.1",
"ext-mbstring": "*",
"intervention/gif": "^4.2"
},
"require-dev": {
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^2.1",
"squizlabs/php_codesniffer": "^3.8",
"slevomat/coding-standard": "~8.0"
},
"suggest": {
"ext-exif": "Recommended to be able to read EXIF data properly."
},
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Intervention\\Image\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}

View File

@@ -0,0 +1,91 @@
# Intervention Image
## PHP Image Processing
[![Latest Version](https://img.shields.io/packagist/v/intervention/image.svg)](https://packagist.org/packages/intervention/image)
[![Build Status](https://github.com/Intervention/image/actions/workflows/run-tests.yml/badge.svg)](https://github.com/Intervention/image/actions)
[![Monthly Downloads](https://img.shields.io/packagist/dm/intervention/image.svg)](https://packagist.org/packages/intervention/image/stats)
[![Support me on Ko-fi](https://raw.githubusercontent.com/Intervention/image/develop/.github/images/support.svg)](https://ko-fi.com/interventionphp)
Intervention Image is a **PHP image processing library** that provides a simple
and expressive way to create, edit, and compose images. It comes with a universal
interface for the two most popular PHP image manipulation extensions. You can
choose between the GD library or Imagick as the base layer for all operations.
- Simple interface for common image editing tasks
- Interchangeable driver architecture
- Support for animated images
- Framework-agnostic
- PSR-12 compliant
## Installation
You can easily install this library using [Composer](https://getcomposer.org).
Simply request the package with the following command:
```bash
composer require intervention/image
```
## Getting Started
Learn the [basics](https://image.intervention.io/v3/basics/instantiation/) on
how to use Intervention Image and more with the [official
documentation](https://image.intervention.io/v3/).
## Code Examples
```php
use Intervention\Image\ImageManager;
// create image manager with desired driver
$manager = new ImageManager(
new Intervention\Image\Drivers\Gd\Driver()
);
// open an image file
$image = $manager->read('images/example.gif');
// resize image instance
$image->resize(height: 300);
// insert a watermark
$image->place('images/watermark.png');
// encode edited image
$encoded = $image->toJpg();
// save encoded image
$encoded->save('images/example.jpg');
```
## Requirements
Before you begin with the installation make sure that your server environment
supports the following requirements.
- PHP >= 8.1
- Mbstring PHP Extension
- Image Processing PHP Extension
## Supported Image Libraries
Depending on your environment Intervention Image lets you choose between
different image processing extensions.
- GD Library
- Imagick PHP extension
- [libvips](https://github.com/Intervention/image-driver-vips)
## Security
If you discover any security related issues, please email oliver@intervention.io directly.
## Authors
This library is developed and maintained by [Oliver Vogel](https://intervention.io)
Thanks to the community of [contributors](https://github.com/Intervention/image/graphs/contributors) who have helped to improve this project.
## License
Intervention Image is licensed under the [MIT License](LICENSE).

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class ColorspaceAnalyzer extends SpecializableAnalyzer
{
//
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class HeightAnalyzer extends SpecializableAnalyzer
{
//
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class PixelColorAnalyzer extends SpecializableAnalyzer
{
public function __construct(
public int $x,
public int $y,
public int $frame_key = 0
) {
//
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class PixelColorsAnalyzer extends SpecializableAnalyzer
{
public function __construct(
public int $x,
public int $y
) {
//
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class ProfileAnalyzer extends SpecializableAnalyzer
{
//
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class ResolutionAnalyzer extends SpecializableAnalyzer
{
//
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Analyzers;
use Intervention\Image\Drivers\SpecializableAnalyzer;
class WidthAnalyzer extends SpecializableAnalyzer
{
//
}

View File

@@ -0,0 +1,217 @@
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Intervention\Image\Interfaces\CollectionInterface;
use ArrayIterator;
use Countable;
use Traversable;
use IteratorAggregate;
/**
* @implements IteratorAggregate<int|string, mixed>
*/
class Collection implements CollectionInterface, IteratorAggregate, Countable
{
/**
* Create new collection object
*
* @param array<int|string, mixed> $items
* @return void
*/
public function __construct(protected array $items = [])
{
//
}
/**
* Static constructor
*
* @param array<int|string, mixed> $items
* @return self<int|string, mixed>
*/
public static function create(array $items = []): self
{
return new self($items);
}
/**
* {@inheritdoc}
*
* @see CollectionInterface::has()
*/
public function has(int|string $key): bool
{
return array_key_exists($key, $this->items);
}
/**
* Returns Iterator
*
* @return Traversable<int|string, mixed>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
}
/**
* {@inheritdoc}
*
* @see CollectionInterface::toArray()
*/
public function toArray(): array
{
return $this->items;
}
/**
* Count items in collection
*/
public function count(): int
{
return count($this->items);
}
/**
* Append new item to collection
*
* @return CollectionInterface<int|string, mixed>
*/
public function push(mixed $item): CollectionInterface
{
$this->items[] = $item;
return $this;
}
/**
* Return first item in collection
*/
public function first(): mixed
{
if ($item = reset($this->items)) {
return $item;
}
return null;
}
/**
* Returns last item in collection
*/
public function last(): mixed
{
if ($item = end($this->items)) {
return $item;
}
return null;
}
/**
* Return item at given position starting at 0
*/
public function getAtPosition(int $key = 0, mixed $default = null): mixed
{
if ($this->count() == 0) {
return $default;
}
$positions = array_values($this->items);
if (!array_key_exists($key, $positions)) {
return $default;
}
return $positions[$key];
}
/**
* {@inheritdoc}
*
* @see CollectionInterface::get()
*/
public function get(int|string $query, mixed $default = null): mixed
{
if ($this->count() == 0) {
return $default;
}
if (is_int($query) && array_key_exists($query, $this->items)) {
return $this->items[$query];
}
if (is_string($query) && !str_contains($query, '.')) {
return array_key_exists($query, $this->items) ? $this->items[$query] : $default;
}
$query = explode('.', (string) $query);
$result = $default;
$items = $this->items;
foreach ($query as $key) {
if (!is_array($items) || !array_key_exists($key, $items)) {
$result = $default;
break;
}
$result = $items[$key];
$items = $result;
}
return $result;
}
/**
* Map each item of collection by given callback
*/
public function map(callable $callback): self
{
return new self(
array_map(
fn(mixed $item) => $callback($item),
$this->items,
)
);
}
/**
* Run callback on each item of the collection an remove it if it does not return true
*/
public function filter(callable $callback): self
{
return new self(
array_filter(
$this->items,
fn(mixed $item) => $callback($item),
)
);
}
/**
* {@inheritdoc}
*
* @see CollectionInterface::empty()
*/
public function empty(): CollectionInterface
{
$this->items = [];
return $this;
}
/**
* {@inheritdoc}
*
* @see CollectionInterface::slice()
*/
public function slice(int $offset, ?int $length = null): CollectionInterface
{
$this->items = array_slice($this->items, $offset, $length);
return $this;
}
}

View File

@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Colors;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use ReflectionClass;
use Stringable;
abstract class AbstractColor implements ColorInterface, Stringable
{
/**
* Color channels
*
* @var array<ColorChannelInterface>
*/
protected array $channels;
/**
* {@inheritdoc}
*
* @see ColorInterface::channels()
*/
public function channels(): array
{
return $this->channels;
}
/**
* {@inheritdoc}
*
* @see ColorInterface::channel()
*/
public function channel(string $classname): ColorChannelInterface
{
$channels = array_filter(
$this->channels(),
fn(ColorChannelInterface $channel): bool => $channel::class === $classname,
);
if (count($channels) == 0) {
throw new ColorException('Color channel ' . $classname . ' could not be found.');
}
return reset($channels);
}
/**
* {@inheritdoc}
*
* @see ColorInterface::normalize()
*/
public function normalize(): array
{
return array_map(
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$this->channels(),
);
}
/**
* {@inheritdoc}
*
* @see ColorInterface::toArray()
*/
public function toArray(): array
{
return array_map(
fn(ColorChannelInterface $channel): int => $channel->value(),
$this->channels()
);
}
/**
* {@inheritdoc}
*
* @see ColorInterface::convertTo()
*/
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{
$colorspace = match (true) {
is_object($colorspace) => $colorspace,
default => new $colorspace(),
};
return $colorspace->importColor($this);
}
/**
* Show debug info for the current color
*
* @return array<string, int>
*/
public function __debugInfo(): array
{
return array_reduce($this->channels(), function (array $result, ColorChannelInterface $item) {
$key = strtolower((new ReflectionClass($item))->getShortName());
$result[$key] = $item->value();
return $result;
}, []);
}
/**
* {@inheritdoc}
*
* @see ColorInterface::__toString()
*/
public function __toString(): string
{
return $this->toString();
}
}

Some files were not shown because too many files have changed in this diff Show More