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

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\ImageInterface;
class AutoEncoder extends MediaTypeEncoder
{
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
return $image->encode(
$this->encoderByMediaType(
$image->origin()->mediaType()
)
);
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class AvifEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class BmpEncoder extends SpecializableEncoder
{
public function __construct()
{
//
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Error;
use Intervention\Image\Exceptions\EncoderException;
use Intervention\Image\FileExtension;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
class FileExtensionEncoder extends AutoEncoder
{
/**
* Encoder options
*
* @var array<string, mixed>
*/
protected array $options = [];
/**
* Create new encoder instance to encode to format of given file extension
*
* @param null|string|FileExtension $extension Target file extension for example "png"
* @return void
*/
public function __construct(public null|string|FileExtension $extension = null, mixed ...$options)
{
$this->options = $options;
}
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
$extension = is_null($this->extension) ? $image->origin()->fileExtension() : $this->extension;
return $image->encode(
$this->encoderByFileExtension(
$extension
)
);
}
/**
* Create matching encoder for given file extension
*
* @throws EncoderException
*/
protected function encoderByFileExtension(null|string|FileExtension $extension): EncoderInterface
{
if (empty($extension)) {
throw new EncoderException('No encoder found for empty file extension.');
}
try {
$extension = is_string($extension) ? FileExtension::from(strtolower($extension)) : $extension;
} catch (Error) {
throw new EncoderException('No encoder found for file extension (' . $extension . ').');
}
return $extension->format()->encoder(...$this->options);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\EncodedImageInterface;
class FilePathEncoder extends FileExtensionEncoder
{
/**
* Create new encoder instance to encode to format of file extension in given path
*
* @return void
*/
public function __construct(protected ?string $path = null, mixed ...$options)
{
parent::__construct(
is_null($path) ? $path : pathinfo($path, PATHINFO_EXTENSION),
...$options
);
}
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
return $image->encode(
$this->encoderByFileExtension(
is_null($this->path) ? $image->origin()->fileExtension() : pathinfo($this->path, PATHINFO_EXTENSION)
)
);
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class GifEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @return void
*/
public function __construct(public bool $interlaced = false)
{
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class HeicEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class Jpeg2000Encoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class JpegEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public bool $progressive = false,
public ?bool $strip = null
) {
//
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Error;
use Intervention\Image\Drivers\AbstractEncoder;
use Intervention\Image\Exceptions\EncoderException;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\MediaType;
class MediaTypeEncoder extends AbstractEncoder
{
/**
* Encoder options
*
* @var array<string, mixed>
*/
protected array $options = [];
/**
* Create new encoder instance
*
* @param null|string|MediaType $mediaType Target media type for example "image/jpeg"
* @return void
*/
public function __construct(public null|string|MediaType $mediaType = null, mixed ...$options)
{
$this->options = $options;
}
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
$mediaType = is_null($this->mediaType) ? $image->origin()->mediaType() : $this->mediaType;
return $image->encode(
$this->encoderByMediaType($mediaType)
);
}
/**
* Return new encoder by given media (MIME) type
*
* @throws EncoderException
*/
protected function encoderByMediaType(string|MediaType $mediaType): EncoderInterface
{
try {
$mediaType = is_string($mediaType) ? MediaType::from($mediaType) : $mediaType;
} catch (Error) {
throw new EncoderException('No encoder found for media type (' . $mediaType . ').');
}
return $mediaType->format()->encoder(...$this->options);
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class PngEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @return void
*/
public function __construct(public bool $interlaced = false, public bool $indexed = false)
{
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class TiffEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Encoders;
use Intervention\Image\Drivers\SpecializableEncoder;
class WebpEncoder extends SpecializableEncoder
{
/**
* Create new encoder object
*
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}