likely starting points stubbed out

This commit is contained in:
Joby Elliott 2020-09-30 19:21:22 -06:00
parent 3870cb25e1
commit e95b1d7413
13 changed files with 256 additions and 3 deletions

View file

@ -17,7 +17,7 @@ A 1.0 release will not be made until the following drivers are available and sol
### Transforms
A 1.0 release will be made available once the following transforms are available and solidly tested across all drivers:
A 1.0 release will be made available once the following transforms are available and solidly tested across all drivers. These are basically what I see as the bare minimum for such a library to be useful.
* rotate (in 90 degree increments)
* mirror-h
@ -25,8 +25,8 @@ A 1.0 release will be made available once the following transforms are available
* max-width
* max-height
* fit (basically an alias for max-width and max-height)
* cover
* crop
* cover (scale to cover a box, then crop excess)
* crop (crop toward the center to a given size)
* cover-crop (convenience transform, combining cover and crop, useful for thumbnails)
The following transforms are also on my mind as possibilities, but may or may not make it into 1.0:
@ -34,3 +34,7 @@ The following transforms are also on my mind as possibilities, but may or may no
* grayscale
* colorize (needs to function consistently though)
* overlay (i.e. for watermarking)
* blur
* hue
* saturation
* brightness

25
composer.json Normal file
View file

@ -0,0 +1,25 @@
{
"name": "jobyone/image-transform",
"description": "A tightly-focused library for performing a very limited set of simple image transformations. This library's purpose is to eschew the standard kitchen sink approach to PHP image libraries in favor of high performance, wide driver support, and a dead simple API.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Joby Elliott",
"email": "joby@byjoby.com"
}
],
"autoload": {
"psr-4": {
"ByJoby\\ImageTransform\\": "src/"
}
},
"suggest": {
"ext-imagick": "to use the Imagick implementation",
"ext-gmagick": "to use the Gmagick implementation"
},
"require": {
"php": ">=7.1",
"ext-gd": "*"
}
}

20
composer.lock generated Normal file
View file

@ -0,0 +1,20 @@
{
"_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": "6eb3038135c5de665d7b86ae7ecb3427",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"ext-gd": "*"
},
"platform-dev": [],
"plugin-api-version": "1.1.0"
}

10
src/DriverInterface.php Normal file
View file

@ -0,0 +1,10 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform;
interface DriverInterface
{
public function source(string $source);
public function originalWidth(): int;
public function originalHeight(): int;
}

View file

@ -0,0 +1,25 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
use ByJoby\ImageTransform\DriverInterface;
abstract class AbstractDriver implements DriverInterface
{
protected $source;
public function source(string $source)
{
// set source
$this->source = $source;
// validate file
if (!is_file($this->source)) {
throw new \Exception("Image file doesn't exist: " . htmlentities($this->source));
}
if (!exif_imagetype($this->source)) {
throw new \Exception("Invalid image file: " . htmlentities($this->source));
}
// get height/width
list($this->originalWidth, $this->originalHeight) = getimagesize($this->source);
}
}

10
src/Drivers/GDDriver.php Normal file
View file

@ -0,0 +1,10 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
/**
* This driver uses PHP's built-in GD libary. This is by far the slowest
* driver, but support is basically universal.
*/
class GDDriver extends AbstractDriver
{}

View file

@ -0,0 +1,9 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
/**
* This driver uses the Gmagick extension
*/
class GmagickDriver extends AbstractDriver
{}

View file

@ -0,0 +1,13 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
/**
* This driver is designed to use exec() and your OS's binary Imagick
* installation to do all of its image transforms. This will likely be
* the highest-performance driver of all in most situations, but does
* require that you have CLI Imagick installed, and that your server allows
* PHP's exec() function to use it.
*/
class ImagickCLIDriver extends AbstractDriver
{}

View file

@ -0,0 +1,9 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
/**
* This class uses the ImageMagick extension
*/
class ImagickDriver extends AbstractDriver
{}

31
src/Image.php Normal file
View file

@ -0,0 +1,31 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform;
class Image
{
protected $src, $driver;
protected $transforms = [];
public function __construct(string $src, DriverInterface $driver)
{
$this->src = $src;
$this->driver = clone $driver;
$this->driver->source($src);
}
public function transform(TransformInterface $transform)
{
$this->transforms[] = $transform;
}
public function originalWidth(): int
{
return $this->driver->originalWidth();
}
public function originalHeight(): int
{
return $this->driver->originalHeight();
}
}

View file

@ -0,0 +1,52 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform;
interface TransformInterface
{
/**
* Whether this transformer changes the contents of the image. Used when
* optimizing transformation order.
*/
const CHANGES_CONTENT = false;
/**
* Whether this transformer changes the size of the image. Used when
* optimizing transformation order.
*/
const CHANGES_SIZE = false;
/**
* Whether this transformer changes the colors of the image. Used when
* optimizing transformation order.
*/
const CHANGES_COLOR = false;
/**
* Whether or not applying this transform multiple times will always
* produce the same result. Used with optimizing transformation order.
*/
const TRANSFORM_STABLE = true;
/**
* Optionally return an array of other transforms that perform the
* operations necessary to perform this single transform.
*
* If this method returns anything, the Image object will add the
* Transformers returned here, and this object will be discarded.
*
* This allows new Transformers to be constructed by composing
* existing ones.
*
* @return array
*/
public function chain(): array;
/**
* Return whether or not this transformation will impact the given
* Image. Should return true if it's possible, as it will be
* skipped if it returns false.
* @return bool
*/
public function willTransform(Image $image): bool;
}

View file

@ -0,0 +1,19 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
use ByJoby\ImageTransform\Image;
use ByJoby\ImageTransform\TransformInterface;
abstract class AbstractTransform implements TransformInterface
{
public function chain(): array
{
return [];
}
public function willTransform(Image $image): bool
{
return true;
}
}

View file

@ -0,0 +1,26 @@
<?php
/* image-transform | https://github.com/jobyone/image-transform | MIT License */
namespace ByJoby\ImageTransform\Drivers;
use ByJoby\ImageTransform\Image;
class MaxWidth extends AbstractTransform
{
const CHANGES_SIZE = true;
protected $size;
public function __construct(int $size)
{
$this->size = $size;
}
public function willTransform(Image $image): bool
{
if ($image->originalWidth() <= $this->size) {
return false;
} else {
return true;
}
}
}